一、This关键字三种用法(主要作用于本类)
1.使用this关键字引用成员变量
2.使用this关键字在自身构造方法内部引用其它构造方法
3.使用this关键字代表自身类的对象
public class DoThis {
//类中的成员变量
private String a ;
//类中不带入参的构造函数
private DoThis() {
//this用法一:必须写在方法第一行,this()形式调用本类中的构造方法
this("abcd");
System.out.println("<<<<<<<<<我是构造方法不带入参<<<<<<<<<<");
}
//类中带int入参的构造函数
private DoThis(int n) {
System.out.println("<<<<<<<<<我是构造方法带int入参<<<<<<<<<<");
//this用法二:this代表自身对象
System.out.println(this);
}
//类中带String入参的构造函数
private DoThis(String a) {
//this用法三:this.a的形式引用本类的成员变量
this.a = a;
System.out.println(this.a);
}
public static void main(String[] args) {
DoThis doThis = new DoThis(1);
}
}
二、Super关键字三种用法(主要是作用于父类)
1、super.xxx;(xxx为变量名或对象名)
super.name; //name为继承的父类中的成员变量
2、super.xxx(); (xxx为方法名)
super.introduct(); //直接访问并调用父类中的实例方法
3、super();
调用父类的构造方法