因为子类已经继承了父类的所有方法,子类也未去重写这些方法,这些方法实际上已经属于子类,那么使用时,如果我们不写this或super,类也不报错;使用this或super也正确。那我们什么也不写的那种情况到底默认是哪个?
看看下面的代码
package com.java.three;
public class Father {
public void printTheInfomationOfThisMan(){
System.out.println(this.toString());
}
public String name="父亲name";
public int age=55;
public String sex="男";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Father [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
package com.java.three;
public class Son extends Father{
public String ambution;
public String getAmbution() {
return ambution;
}
//使用this给变量赋值
public void setAmbution(String ambution) {
this.ambution = ambution;
}
//使用super给变量赋值
public void superSetInfoToSon(String name,int age){
super.setName(name);
super.setAge(age);
}
//使用this给变量赋值
public void thisSetInfoToSon(String name,int age){
this.setName(name);
this.setAge(age);
}
//这是我们要研究的重点,看看这种方式到底改变的是this的变量还是super的变量
//不适用this,也不使用super,给变量赋值
public void setInfoToSonWithoutNeither(String name,int age){
name=name;
age=age;
}
public void printTheInfomationOfSon(){
System.out.println(this.toStringOfSon());
}
public void printTheInfomationOfFather(){
System.out.println(this.toStringOfFather());
}
//注意下面关键字this的使用
public String toStringOfSon() {
return "Son [ambution=" + this.ambution + ", name=" + this.name + ", age=" + this.age
+ ", sex=" + this.sex + "]";
}
//注意下面关键字super的使用
public String toStringOfFather() {
return "Son [ambution=" + ambution + ", name=" + super.name + ", age=" + super.age
+ ", sex=" + super.sex + "]";
}
}
package com.java.three;
public class Testmain {
public static void main(String ssd[]){
// Father ff=new Father();
// ff.setName("父亲");
// ff.setAge(55);
// ff.printTheInfomationOfThisMan();
Son ss=new Son();
System.out.println("----------------this--start-----------------------");
ss.thisSetInfoToSon("给儿子命名this", 26);使用this给变量赋值
ss.setAmbution("亿万富翁");//子类自己的方法
//ss.printTheInfomationOfThisMan();//继承父类的方法
ss.printTheInfomationOfSon();//子类自己的方法,打印自己的变量this
ss.printTheInfomationOfFather();//子类自己的方法,打印父类的变量super
System.out.println("----------------this--end-----------------------");
System.out.println("----------------super--start-----------------------");
ss.superSetInfoToSon("给儿子命名super", 25);//使用super给变量赋值
ss.printTheInfomationOfSon();//子类自己的方法,打印自己的变量this
ss.printTheInfomationOfFather();//子类自己的方法,打印父类的变量super
System.out.println("----------------super--end-----------------------");
System.out.println("");
System.out.println("----------------注意!!!-----------------------");
System.out.println("----------------neither--start-----------------------");
//注意下面我们设置的信息是("给儿子命名neither", 18)
ss.setInfoToSonWithoutNeither("给儿子命名neither", 18);//不适用this,也不使用super,给变量赋值
ss.printTheInfomationOfSon();//子类自己的方法,打印自己的变量this
ss.printTheInfomationOfFather();//子类自己的方法,打印父类的变量super
System.out.println("----------------neither--end-----------------------");
}
}
控制台打印:
----------------this--start-----------------------
Son [ambution=亿万富翁, name=给儿子命名this, age=26, sex=男]
Son [ambution=亿万富翁, name=给儿子命名this, age=26, sex=男]
----------------this--end-----------------------
----------------super--start-----------------------
Son [ambution=亿万富翁, name=给儿子命名super, age=25, sex=男]
Son [ambution=亿万富翁, name=给儿子命名super, age=25, sex=男]
----------------super--end-----------------------
----------------注意!!!-----------------------
----------------neither--start-----------------------
Son [ambution=亿万富翁, name=给儿子命名super, age=25, sex=男]
Son [ambution=亿万富翁, name=给儿子命名super, age=25, sex=男]
----------------neither--end-----------------------
结论:子类继承的变量就是自己的,无论用this,或是用super都一样。
还有一个问题我没看明白,希望高手解答:
我们预想应该输出的("给儿子命名neither", 18)这个信息无论用this取,还是用super取,都取不出来!
不知为什么,欢迎技术大牛给我留言。