public class Test extends Test2
{
Test() { }
public void testMethod(){
System.out.println("method in Test----son");
}
public static void main(String[] args)
{
Test t=new Test();
t.f();
}
}
class Test2
{
Test2() { }
public void testMethod(){
System.out.println("method in Test2----dad");
}
public void f(){
testMethod();
this.testMethod();
}
}
**********************************************************************************************************
What is the output? This is tricky if you do not know the exact meaning of "this".
*********************************************************************************************************
OUTPUT:
E:/MyDoc/Exercises>java Test
method in Test----son
method in Test----son
.
本文通过一个具体的Java代码示例介绍了在类继承结构中如何使用this关键字来调用当前类的方法。通过父类和子类中重写方法的方式展示了this关键字的具体行为,并解释了运行时输出结果。
136

被折叠的 条评论
为什么被折叠?



