首先让我们来看以下this和super都有什么功能:
this: 访问本类对象成员变量
this.变量名 调用本类普通方法
super: 访问本类对象当中的父类对象成员变量
super.变量名 调用本类对象当中的父类普通方法
新建一个Cat类继承Animal类,并且在该类中定义一个showNameAge()的方法书写代码如下:
运行后我们发现输出为:丫丫今年2岁了!,所以this调用的是本类对象的成员变量
2.验证super可以访问本类对象当中的父类对象成员变量
将上述cat类的showNameAge()方法中的this改为super,运行程序,输出结果为:咪咪今年四岁了!故而验证了super可以访问本类对象当中的父类对象成员变量
3.this可以调用本类普通方法
在验证1中的cat类中,加入一个方法名为testThis()的方法,代码如下
然后在Test类中,将无用代码注释,创建cat对象,并调用testThis方法,代码如下
运行输出结果为丫丫今年2岁了!进而验证了this可以调用本类普通方法
4. super调用本类对象当中的父类普通方法
针对验证3中的cat类,将showNameAge()方法改为如下
并运行结果为:咪咪今年4岁了!我们通过super调用了Animal类的getName()方法,所以验证了super调用本类对象当中的父类普通方法
5. this可以通过本类构造方法调用本类其他构造
在cat类中写两个构造方法,代码如下
将其他代码注释,然后在Test类中,将其余代码注释,书写代码如下:
Cat cat2=new Cat("豆豆",4);
运行结果为:利用this调用了本类的空参构造方法
可以验证this可以通过本类构造方法调用本类其他构造
6super可以通过本类构造方法调用父类构造
将cat类修改如下:
将Animal的构造方法书写如下:
运行可以看到两条信息: 父类的空参构造方法被调用了!
父类的满参构造方法被调用了!
因此验证了super可以通过本类构造方法调用父类构造
this: 访问本类对象成员变量
this.变量名 调用本类普通方法
this.方法名(参数) 本类构造方法调用本类其他构造本类(构造方法第一行this(参数))
super.变量名 调用本类对象当中的父类普通方法
super.方法名(参数) 本类构造方法调用父类构造本类构造方法第一行super(参数)
然后我们将super和this对比着来验证:验证this可以访问本类对象成员新建一个Animal类书写如下代码
public class Animal { public String name="咪咪"; public int age=4; }
新建一个Cat类继承Animal类,并且在该类中定义一个showNameAge()的方法书写代码如下:
public class Cat extends Animal { private String name="丫丫"; private int age=2; public void showNameAge(){ System.out.println(this.name+"今年"+this.age+"岁了!"); } }然后新建测试类Test,创建cat对象,并调用showNameAge()方法,运行
public class Test { public static void main(String[] args) { Cat cat=new Cat(); cat.showNameAge(); } }
运行后我们发现输出为:丫丫今年2岁了!,所以this调用的是本类对象的成员变量
2.验证super可以访问本类对象当中的父类对象成员变量
将上述cat类的showNameAge()方法中的this改为super,运行程序,输出结果为:咪咪今年四岁了!故而验证了super可以访问本类对象当中的父类对象成员变量
3.this可以调用本类普通方法
在验证1中的cat类中,加入一个方法名为testThis()的方法,代码如下
public class Cat extends Animal { private String name="丫丫"; private int age=2; public void showNameAge(){ System.out.println(this.name+"今年"+this.age+"岁了!"); } public void testThis(){ this.showNameAge(); } }
然后在Test类中,将无用代码注释,创建cat对象,并调用testThis方法,代码如下
public class Test { public static void main(String[] args) { Cat cat=new Cat(); cat.testThis(); } }
运行输出结果为丫丫今年2岁了!进而验证了this可以调用本类普通方法
4. super调用本类对象当中的父类普通方法
针对验证3中的cat类,将showNameAge()方法改为如下
public void showNameAge(){ System.out.println(super.getName()+"今年"+super.getAge()+"岁了!"); } public Cat(){ System.out.println("利用this调用了本类的空参构造方法"); } public Cat(String name, int age) { this(); }
并运行结果为:咪咪今年4岁了!我们通过super调用了Animal类的getName()方法,所以验证了super调用本类对象当中的父类普通方法
5. this可以通过本类构造方法调用本类其他构造
在cat类中写两个构造方法,代码如下
将其他代码注释,然后在Test类中,将其余代码注释,书写代码如下:
Cat cat2=new Cat("豆豆",4);
运行结果为:利用this调用了本类的空参构造方法
可以验证this可以通过本类构造方法调用本类其他构造
6super可以通过本类构造方法调用父类构造
将cat类修改如下:
public class Cat extends Animal { private static String name; private static int age; public Cat(){ super(name,age); } public Cat(String name, int age) { super(); } }
将Animal的构造方法书写如下:
public Animal(){ System.out.println("父类的空参构造方法被调用了!"); } public Animal(String name,int age){ System.out.println("父类的满参构造方法被调用了!"); this.name=name; this.age=age; }在Test类中书写代码如下:
Cat cat2=new Cat("豆豆",4); Cat cat=new Cat();
运行可以看到两条信息: 父类的空参构造方法被调用了!
父类的满参构造方法被调用了!
因此验证了super可以通过本类构造方法调用父类构造