class Grandfather
{
public void print()
{
System.out.println("Grandfather") ;
}
};
class Father extends Grandfather
{
public void print()
{
System.out.println("father") ;
}
public void funFather()
{
this.print() ;
super.print() ;
}
};
class Child extends Father{ // 定义继承关系
public void print()
{
System.out.println("child") ;
}
public void funChild()//避免覆写
{
this.print() ;
super.print() ;
}
};
public class test3{
public static void main(String args[])
{
Child s = new Child() ;
s.funFather() ;//此处通过继承调用父类函数中的 this.print与super.print
s.funChild();//此处直接调用子类类 this.print与super.print
}
};
输出为
child
Grandfather
child
father
--------------------------------------------------------------------------------------------------------------------------------------------
class Father{
private void print()
{
System.out.println("Father") ;
}
public void fun2()
{
this.print() ; // 调用print()方法
}
};
class Child extends Father{ // 定义继承关系
void print(){ // 覆写父类中的方法
System.out.println("Child") ;
}
public void fun()
{
this.print() ;
}
};
public class test3
{
public static void main(String args[])
{
Child s = new Child() ;
s.fun() ;
s.fun2();
}
};
输出为
Child
Father