首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java 之承袭

2012-12-28 
java 之继承public class Test{public static void main(String[] args){TestA ta new TestC()ta.show(

java 之继承
public class Test  
{  
    public static void main(String[] args)  
    {  
        TestA ta = new TestC();  
        ta.show();  
    }  
}  
 
class TestA  
{  
    public TestA()  
    {  
        System.out.println("TestA");  
    }  
      
    public void show()  
    {  
        System.out.println("TestA.show");  
    }  
}  
 
class TestB extends TestA  
{  
    public TestB()  
    {  
        System.out.println("TestB");  
    }  
      
    public void show()  
    {  
        System.out.println("TestB.show");  
    }  
}  
 
class TestC extends TestB  
{  
    public TestC()  
    {  
        System.out.println("TestC");  
    }  



注:调用B的show()方法,因为自身没有show方法,所以调用直接父类的show,如果直接父类没有,则调用父类的show。
结果:
TestA  
TestB  
TestC  
TestB.show 

热点排行