zoukankan      html  css  js  c++  java
  • 夜谈Java类的定义

    标题图

    • 女孩:谈Java了,好耶?

    • 男孩:夜谈一下,Java的类的定义~

    • 女孩:那谈Java的类的什么呢?

    • 男孩:类的定义,对象的定义,类中的方法,构造方法,this关键字,方法的重载,Java中的类的访问权限,set和get方法,static关键字~

    面向对象设计思想

    设计思想的变化

    例如:学生行为

    对于结构化程序设计:起床,洗脸,吃饭,下课,放学
    对于面向对象设计,类,类中的方法
    面向对象的基本概念.png

    OOP的特点

    面向对象的特征
    (1)封装
    (2)继承
    (3)多态

    类的定义:

    格式:

    class Circle{
    //类声明,类的命名第一个字母大写以便与对象区别
    int r;
    //成员变量,或称为类的属性
    ……
    void print(){
    //成员方法
        
    int r=3;
    //局部变量
       
    System.out.println(r);
    }
    
    }
    //整个大括号里的称为类的主体部分
    

    类的定义

    类是一种复杂的数据类型
    它是将数据和与数据相关的操作封装在一起的集合体

    类分为两部分:类的声明和类的主体。
    格式:

    <类 声明>
    {
    <类主体>
    }

    主体的部分

    类主体部分是由成员变量和方法组成,不可以在类中直接写表达式语句,它必须放到一个方法里。

    对象的概念

    对象是类的实例化

    public static void main(String [] args){
    Circle circle=new Circle ();//类的实例化
    circle.r=1;
    ...
    circle.print();//对象的方法调用
    }
    

    对象的创建

    格式:

    类名 对象名

    为声明的对象分配内存,用new和类的构造方法为声明的对象分配内存,如果类中没有构造方法,系统会默认调用类的构造方法。

    Student student = new Student();

    对象的使用

    格式
    对象名.成员变量名
    对象名.方法名

    “.”这点为英文的点。
    如:

    student.name
    student.print();

    类中的方法

    在某个方法里的变量为局部变量,在类中的,方法外的变量为成员变量。

    如果成员变量和局部变量同名时,则成员变量隐藏。

    方法中有同名的局部变量访问时,用this关键字表示。

    构造方法是一种特殊的方法,构造方法的名要与类的名相同,而且没有类型,构造方法可以重载。

    this关键字为当前对象,不出现含有static中。

    Student(String name, int age){
    this.name = name;
    this.age = age;
    }
    

    类的定义

    1.定义一个Person类,主要功能是输出姓名,身高,体重,类中有两个构造方法,其中一个构造方法无参数用于初始化,另一个构造方法是有参数的,可传值

    1. 定义Person类的对象,通过对象成员方法输出对象的信息
    public class Person {
     String name;
     double height;
     double weight;
     Person(){
      name = "jeskson";
      height = 170;
      weight = 100;
     }
     Person(String name, double height, double weight){
      this.name = name;
      this.height = height;
      this.weight = weight;
     }
     void print(){
        System.out.println(this.name);
        System.out.println(this.height);
        System.out.println(this.weight);
       }
       public static void main(String[] args){
        Person student = new Person();
        student.print();
    
        Person stu = new Person("jeskson",180,100);
        stu.print();
    
       }
    }
    
    public class Circle {
     //成员变量
     int r;
     final double PI=3.14;
    
     //成员方法
     double area(){
      return PI*r*r;
     }
    
       //成员方法
       double zhou(){
        return PI*2*r;
       }
    
       public static void main(String[] args){
        Circle c1;//定义对象
        //为对象申请内存空间,自动调用构造方法,如果没有写构造方法,系统会自动产生一个无参的
        c1 = new Circle();
        c1.r=1;//为对象的变量赋值
        System.out.println(c1.area());//输出对象成员方法的返回值
        System.out.println(c1.zhou());
       }
    
    }
    
    public class Circle {
     //成员变量
     int r;
     final double PI=3.14;
    
     //无参的构造方法
     Circle(){
    
     }
    
     //带参的构造方法
     Circle(int r){
      this.r = r;
     }
    
     //成员方法
     double area(){
      return PI*r*r;
     }
    
       //成员方法
       double zhou(){
        return PI*2*r;
       }
    
       public static void main(String[] args){
        Circle c1;//定义对象
        //为对象申请内存空间,自动调用构造方法,如果没有写构造方法,系统会自动产生一个无参的
        c1 = new Circle();
        c1.r=1;//为对象的变量赋值
        System.out.println(c1.area());//输出对象成员方法的返回值
        System.out.println(c1.zhou());
    
        Circle c2 = new Circle(2);//调用带参数的构造方法
        System.out.println(c2.area());//输出对象成员方法的返回值
        System.out.println(c2.zhou());
       }
    
    }
    

    Java中的类的访问权限

    成员变量的声明

    【修饰符】类型 变量名

    常用的修饰符关键字有:public、 private、 protected 、 friendly、static 和final。

    类:private,friendly,protected,public
    包中所有类:friendly,protected,public
    包外的子类:protected,public
    所有类:public

    public变量.png

    private变量.png

    protected变量.png

    默认访问.png

    类中的set和get方法

    set方法是用来修改成员变量的值
    get方法是用来获取成员变量的值

    static关键字

    使用static修饰的成员变量为静态变量;用static修饰的类,为静态类,用来修饰方法,则为静态方法。静态变量是归类所有,静态变量直接通过类调用,而不用实例化对象。静态方法也是用类名直接调用的。静态方法只能操作静态变量而不能使用实例变量。

    图片

    public class Account {
     private  String name;
     private double yu;
     private int year;
     private double rate=0.025;
     
     public Account() {
       yu=10;
       year=1;
     }
     
     public Account(double yu, int year) {
       this.yu = yu;
       this.year = year;
     }
     
     public void save(double yu){
       this.yu+=yu;
     }
    
     public void feach(double yu){
       this.yu-=yu;
     }
    
     public double total(){
       return this.yu+this.rate*this.year*this.yu;
     }
    
     public static void main(String[] args) {
       Account zhangshan=new Account(1000, 3);
       zhangshan.save(1000);
       System.out.println(zhangshan.total());
     }
    }
    

    图片

    package hh;
    
    public class Worker {
    private String ID;
    private String name;
    private double basic;
    private double bonus;
    
    public Worker() {
     name="jeskson";
     basic=1900;
     bonus=100;
     ID="1024";
    }
    
    public Worker(String ID, String name, double basic, double bonus) {
     super();
     this.ID = ID;
     this.name = name;
     this.basic = basic;
     this.bonus = bonus;
    }
    
    public String getID() {
     return ID;
    }
    
    public void setID(String iD) {
     ID = iD;
    }
    
    public String getName() {
     return name;
    }
    
    public void setName(String name) {
     this.name = name;
    }
    
    public double getBasic() {
     return basic;
    }
    
    public void setBasic(double basic) {
     this.basic = basic;
    }
    
    public double getBonus() {
     return bonus;
    }
    
    public void setBonus(double bonus) {
     this.bonus = bonus;
    }
    
    public double getTax(){
     if (this.basic+this.bonus<=1600){
      return 0;
    }
    return (this.basic+this.bonus-1600)*0.15;
    }
    
    public double getSalary(){
     if (this.basic+this.bonus<=1600){
      return this.basic+this.bonus;
     }
     return this.basic+this.bonus-this.getTax();
    }
    
    void print(){
     System.out.println("工人姓名:"+this.name);
     System.out.println("所得税:"+this.getTax());
     System.out.println("实际工资:"+this.getSalary());
    }
    
    public static void main(String[] args) {
     Worker jack=new Worker("1024", "jeskson", 1400, 100);
     jack.print();
    }
    }
    
    public class Student {
     String name;
     int age;
     double height;
     double weight;
     static Strin monitor;
     public Student() {
    
     }
     public Student(String name, int age, double height, double weight){
      this.name = name;
      this.age = age;
      this.height = height;
      this.weight = weight;
     }
     void print(){
      System.out.println(name+" "+age+" "+height+" "+weight+" "+monitor);
     }
     static void printmonitor(){
      System.out.println(monitor);
     }
     public static void main(String[] args){
      Student.monitor = "jeskson";
      Student stu = new Student("dashu",20,179,100);
      stu.print();
    
     }
    }
    

    小礼物走一走 or 点赞

    图片

  • 相关阅读:
    十年经验手把手教你选购翡翠
    眼睛视力
    玻璃
    前端小技巧
    儿童牙齿矫正
    MySQL的JDBC驱动源码解析
    书海杂谈
    电子设备
    股市国家队
    影视
  • 原文地址:https://www.cnblogs.com/dashucoding/p/9388518.html
Copyright © 2011-2022 走看看