结论:不会
原因:
(1)准备父类如下:
<span style="white-space:pre"> </span>package extend_test;
<span style="white-space:pre"> </span>public class father_class {
public int father_a;
public father_class() {
System.out.println("父类构造函数");// TODO Auto-generated constructor stub
father_a=1;
}
<span style="white-space:pre"> </span>}
(2)准备子类如下:
<span style="white-space:pre"> </span>package extend_test;
<span style="white-space:pre"> </span>public class son_cladd extends father_class {
public int son_b;
public son_cladd() {
System.out.println("子类构造");// TODO Auto-generated constructor stub
son_b=2;
}
public void test() {
System.out.println("");
System.out.println("子类test函数输出结果如下");
System.out.println("子类对象this="+" "+this);
System.out.println("显式调用子类对象super.tostring()方法="+" "+super.toString());
System.out.println("判断super和this是否指向堆中同一对象="+" "+super.equals(this));
}
<span style="white-space:pre"> </span>}
(3)main函数如下:
<span style="white-space:pre"> </span>package extend_test;
<span style="white-space:pre"> </span>public class main_test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
son_cladd son_cladd=new son_cladd();
son_cladd.test();
System.out.println("");
System.out.println("main函数输出结果如下:");
System.out.println("子类对象hashcode为"+son_cladd);
father_class father_class=son_cladd;
System.out.println("父类对象hashcode为"+father_class);
System.out.println(father_class==son_cladd);
}
<span style="white-space:pre"> </span>}
输出结果:
分析:
1)可知子类实例化对象时会先调用父类的构造方法,具体逻辑大家应该都清楚
2)System.out.println(super);编译错误,可见super不存在test方法的局部变量表中,System.out.println(this);则可以,因为当调用每个类中的方法时都会首先将一个指向调用该方法的实例的引用 this 放于该方法栈帧中的局部变量表中,System.out.println(this);调用this.toString()方法,返回该实例的hashcode。而调用System.out.println(super.toString());则可通过编译。super是一个关键字,编译器根据此关键字执行父类的方法而不是根据多态规则执行子类的tostring的方法,即super可以调用被子类覆盖或者隐藏的方法或者变量。