類和物件部分知識總結

yanweiding發表於2020-10-24

1.類和物件基礎知識

class Person{      //建立一個Person類(型)
    //以下為成員屬性
    public int age;    //例項成員變數
    public String name;
    public static int count;   //靜態成員變數
    public final int SIZE=10;   //final是常量的意思  是物件,在堆上
    public static final int SIZE1=10; //有static修飾,在方法區

    //以下為成員方法
    public void eat(){     //非靜態方法
        int a=10;   //區域性變數,棧上,方法結束後變數隨之消失
        System.out.println("吃飯");
    }

    public static void fun(){   //靜態方法,內部不能呼叫例項成員方法,不能對例項資料成員初始化
       count =10;
        System.out.println("靜態方法");
    }
}

public class TestDemo{
    public static void main(String[] args) {
        Person person1=new Person();//物件的例項化
        person1.age=10;
        System.out.println(person1.age);
        person1.eat();
        Person.fun();//靜態的方法通過類名訪問,不依賴物件
        System.out.println(Person.count);
        System.out.println(Person.SIZE1);//屬於類本身,通過類訪問
        Person person2=new Person();
        person2.name="sy";
        System.out.println(person2.name);
        System.out.println(person2.SIZE);

    }
}

執行結果執行結果
使用 類名. 成員訪問物件的欄位時,“訪問” 既包含讀, 也包含寫,如果沒有顯式設定初始值, 那麼會被設定一個預設的初始值。
預設值規則:
對於各種數字型別, 預設值為 0.
double 0.0
float 0.0f
long 0L
對於 boolean 型別, 預設值為 false,
對於引用型別(String, Array, 以及自定製類), 預設值為 null

2.重寫方法(toString)
在類內部 游標位置alt+insert
如果insert和delete重合,就fn,alt,insert 三個鍵一起按下

class Person{      
    public int age; 
    public String name;
    public static int count;  
    public final int SIZE=10;   
    public static final int SIZE1=12;
   
    public void eat(){    
        int a=10;  
         count =5; 
        System.out.println("吃飯");
    }

    public static void fun(){  
        System.out.println("靜態方法");
    }

    @Override  //重寫
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

public class TestDemo{
    public static void main(String[] args) {
        Person person=new Person();
        person.age=10;
        System.out.println(person.age);
        person.eat();
        Person.fun();
        System.out.println(Person.count);
        System.out.println(Person.SIZE1);
        System.out.println(person);  
        System.out.println(person.name);
    }
}

執行結果在這裡插入圖片描述
無重寫函式會列印這個 @後面時是地址的雜湊值在這裡插入圖片描述
*3.setter and getter *

class Person{
    private int age;   //類外不可以訪問private修飾的屬性或者方法
    private String name;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

public class TestDemo{
    public static void main(String[] args) {
Person person=new Person();
person.setAge(4);
        System.out.println(person.getAge());
        person.setName("ad");
        System.out.println(person.getName());
    }
}

4.構造方法
先為物件分配記憶體,再呼叫合適的構造方法,才會產生一個物件
支援過載
this 代表當前物件的引用
靜態方法的內部不能用this

class Person{
    private int age;
    private String name;

  public Person(){   //不帶有引數的構造方法
      System.out.println(1);
  }
public Person(String name){  //帶有一個引數的構造方法
    this.name = name;
    System.out.println(this.name);
}
    public Person(int age, String name) {     //用alt+insert
        this.age = age;
        this.name = name;
        System.out.println(this.age);
        System.out.println(this.name);
    }
}

public class TestDemo{
    public static void main(String[] args) {
        Person person=new Person();  //呼叫不帶有引數的構造方法
        Person person1=new Person("s");
        Person person2=new Person(5,"se");
    }
}

在這裡插入圖片描述

編譯器會預設給一個不帶引數的構造方法

5.this關鍵字
this.data 當前物件從成員
this.func() 當前物件的方法
this() 呼叫當前的構造方法,必須放在第一行,只能調一個

class Person {
    private int age;
    private String name;

    public Person() {
        this("RT");  //呼叫帶有一個引數的構造方法
        System.out.println(1);
    }

    public Person(String name) {
        this.name = name;
        System.out.println("f");
        System.out.println(this.name);
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

    public  class TestDemo {
        public static void main(String[] args) {
            Person person = new Person();
            System.out.println(person);
        }
    }

執行結果在這裡插入圖片描述

class Person{
    public int age;
    public String name;

    public Person(String name){
        this.name=name;
    }
    
    @Override  //重寫
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

public class TestDemo{
    public static void main(String[] args) {
        Person person=new Person("st");
        System.out.println(person);
    }
}

在這裡插入圖片描述

相關文章