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

关于继承的疑惑,该如何处理

2012-01-13 
关于继承的疑惑classGrandparent{Grandparent(){System.out.println( Grandparent )}}classParentexten

关于继承的疑惑
class   Grandparent
{
        Grandparent()
        {
                System.out.println( "Grandparent ");
        }
}

class   Parent   extends   Grandparent  
{
        Parent()
        {
                System.out.println( "Parent ");
        }
}

public   class   Child   extends   Parent
{
        public   static   void   main(String[]   args)
        {
                Child   c   =   new   Child();
                System.out.println( "Child ");
               
        }
}


父类的构造函数不是应该被子类覆盖了吗?为什么输出结果是

Grandparent
Parent
Child

明明没用super阿

[解决办法]
子类对象依赖于父类对象,打个比方说,有个父亲和一个孩子,现在你说你只想要这个孩子而不要这个父亲,这是不可能的,因为没有父亲就不可能有孩子,所以想要孩子出生,首先要有父亲,不能脱离父亲而直接生出孩子。

热点排行