天天看點

課時41|封裝-構造方法,this關鍵字

構造方法的作用:

1.負責對象初始化工作,為對象的屬性賦初值;

2.建立對象時,其類的構造方法確定在使用者操作對象之前,系統保證初始化的進行。

構造方法的文法規則:

1.構造方法名與類名相同;

2.沒有傳回類型;

3.方法實作主要為字段賦初值。

構造方法的調用:

1.new操作符:建立新的對象時就會調用相應類的構造方法;

2.Java系統保證每個類都有構造方法。

Constuctor1:預設無參的構造方法

public class Constractor1{
    public static void main(String args[]){
        Person p=new Person();
        p.setName("Steven");
        System.out.println(p.getName());
        p.setAge();
        System.out.println(p.getAge());
        p.setCity("Beijing");
        System.out.println(p.getCity());

    }
}
//When a class has no construct method,System will set a default construct method without parameter. 
class Person{
    private String name;
    private int age;
    private String city;
    //default construct method has no parameters  
    //construct method with parameters.Force us to give enough parameters.
    public Person(){
        //String name,int age,String city
        this.name=name;
        this.age=age;
        this.city=city;
    };
     //set,get name
    public void setName(String cname){
        name=cname;
    }
    public String getName(){
        return name;
    }
    //set,get age
    public void setAge(int cage){
        age=cage;
    }
    public int getAge(){
        return age;
    }
    //set,get city
    public void setCity(String ccity){
        city=ccity;
    }
    public String getCity(){
        return city;
    }  

}   
           

Constructor2:設定參數的構造方法

public class Constractor2{
    public static void main(String args[]){
        //Transfer parameters as well as new an object
        Person p=new Person("Steven",,"Beijing");
        //1.Crate space in heap and give the default value(null)
        //2.Do assignment(if there is)
        //3.Call the method and transfer the parameters
        System.out.println(p.getName());
        System.out.println(p.getAge());
        System.out.println(p.getCity());

    }
}

//When a class has no construct method,System will set a default construct method without parameter. 
class Person{
    private String name;
    private int age;
    private String city;
    //default construct method has no parameters  
    //construct method with parameters.Force us to give enough parameters.
    public Person(String name,int age,String city){
        this.name=name;
        this.age=age;
        this.city=city;
    };
     //set,get name
    public void setName(String cname){
        name=cname;
    }
    public String getName(){
        return name;
    }
    //set,get age
    public void setAge(int cage){
        age=cage;
    }
    public int getAge(){
        return age;
    }
    //set,get city
    public void setCity(String ccity){
        city=ccity;
    }
    public String getCity(){
        return city;
    }  

}   
           

This 關鍵字:調用方法的目前類内的對象

1. 在類的方法中使用this()關鍵字,表示調用此方法的對象引用;

2. this可以看作是一個變量,它的值就是目前對象的引用;

3. 使用this可以處理方法中成員變量和形參同名的問題;

public class IotekTeacher{
    private String name;
    private int age;
    public IotekTeacher(String name,int age){
    this.name=name;
    this.age=age;
    }
}
           

4 當在方法内需要用到調用該方法的對象時,就可以用this;

5 在類的構造方法中可以調用this(【參數清單】)調用該類的指定構造方法。(一個類裡面可以有多個構造方法)

方法中可以調用方法。上述兩個方法屬于同一個類時,被調用的方法前預設有一個this。可以寫也可以不寫。

pubic String toString(){
    System.out.println(this.getAge());
    return "名字:"+this.name+",今年:"+this.age;
}
           

構造方法可以調用構造方法,對this()的調用必須放在首條語句。

定義了帶參數的構造方法後,無參的構造方法依然存在。

public Person(){
        System.out.println("無參的構造方法");
    }

    public Person(String name,int age,String city){
        this.name=name;
        this.age=age;
        this.city=city;
    }
           

New 一個對象時,提示錯誤:non-static variable this cannnot be referenced from a static context.“非靜态變量不可以從靜态上下文中引用”

原因:要new的對象可能在靜态上下文

解決方法:自習檢查要new的對象的屬性