this의 간략한 역할

1805 단어 클래스 및 객체
public class Demo{
    public static void main(String []args){
        Cat cat1=new Cat(" ",5);
        // ,this 
        // name,age
        Master master1=new Master(cat1," "16);
    }
}
class Cat{
    String name;
    int age;


    public Cat(String name,int age){
        this.name=name;
        this.age=age;
    }
    public void showInfo(){
        System.out.print(" :"+this.name);
    }
}
class Master{
    String name;
    int age;
    Cat cat
    public Master(Cat cat,String name,int age){
        this.name=name;
        this.age=age;
        this.cat=cat;
    }
}

여기의this는 대상의 구성원 변수를 가리키며 외부에서 사용할 수 없고 클래스 정의 내에서만 사용할 수 있다.

좋은 웹페이지 즐겨찾기