java中子類繼承父類呼叫利用繼承方法時呼叫this
阿新 • • 發佈:2019-01-01
package cn.itcast.test1;
class Parent{
public Object getObject(){
return this;
}
}
class Son extends Parent{
}
public class Test1 {
public static void main(String[] args) {
Son son = new Son();
Son tem = (Son) son.getObject();
if(son==tem)
System.out.println("their this are the same!");
else
System.out.println("their this are the different!");
}
}
class Parent{
public Object getObject(){
return this;
}
}
class Son extends Parent{
}
public class Test1 {
public static void main(String[] args) {
Son son = new Son();
Son tem = (Son) son.getObject();
if(son==tem)
System.out.println("their this are the same!");
else
System.out.println("their this are the different!");
}
}
結果:
their this are the same!
可以看出子類繼承父類掉用父類的方法是中的this是子類。