this()方法與this屬性

“this屬性” 這說法不知道對不對,這裏也不用理它

我們在一個java文件中,一般只用到了這個this.***來取得一個類屬性;而對this()方法用的卻很少用到,因爲這個方法只能用在構造函數中。

下面是個例子:

public class TestThis {
    
    
int x;
    
int y;
    
int z;
    
    
public TestThis(int x, int y) {
        
this.x = x;
        
this.y = y;
    }


    
public TestThis(int x, int y, int z) {
        
this(x,y);
        
this.z = z;
    }


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章