在Head First In Java的构造器与垃圾收集器这一章的父类的构造函数在对象的生命中所扮演的角色这一小节中,有这样一段话:
在构造函数中使用super()调用父类的构造函数的部分,要记得子类可能会根据父类的状态来继承方法。完整的对象需要也是完整的父类核心,这也是为什么父类构造函数必须执行的原因。就算Animal上有些变量时Hippo不会用到的,但是Hippo可能会用到某些继承下来的方法必须读取Animal的实例变量。
怎样理解完整的对象需要也是完整的父类核心?
还有:这也是为什么父类构造函数必须执行的原因。看完也不明白为什么父类构造函数必须执行的原因。。?
求解。:)
巴扎黑2017-04-17 14:24:04
Understand this way:
Java's inheritance is an "is-a" concept, which means that the subclass is the parent class (but the parent class is not the subclass). The subclass inherits all inheritable elements of the parent class by default, except that it cannot inherit the constructor of the parent class (because: Java's constructor name is the class name, but Java's method inheritance uses the duplicate name mechanism, so the subclass can never inherit Inherits the constructor of the parent class). Therefore, when constructing a subclass, the constructor of the parent class must be executed first, whether explicitly or implicitly.