this關鍵字:
1.返回調用當前方法的對象的引用
例:
public class Boy {
public getBoy() {
return this;
}
public static void main(String[] args) {
Boy boy1 = new Boy();
Boy boy11 = boy1.getBoy(); // this代表boy1
Boy boy2 = new Boy();
Boy boy22 = boy2.getBoy(); // this代表boy2
}
}
2.在構造方法中調用當前類中的其它構造方法
例:
public class Boy {
public Boy() {
this(8); //調用其它構造方法
}
public Boy(int i) {
}
}
3.當方法參數名和成員變量名相同時,用於區分參數名和成員變量名
super用法:
1.調用父類的構造方法
2.調用父類的方法和屬性