[英]java abstract class, Constructor not called in super-class, Why?
Below is a simple example. 以下是一个简单的示例。 I have two abstract classes,
A
and B
, one concrete class, C
, which includes the abstract method. 我有两个抽象类
A
和B
,一个具体类C
,其中包括抽象方法。
When I create a new C
, I expected to see the constructors of A
and B
being called. 当我创建一个新的
C
,我希望看到A
和B
的构造函数被调用。 Any idea why they are not called? 知道为什么不叫他们吗?
//Q.java
class Q {
abstract class A {
A() {
System.out.println("in A");
}
public abstract void sayHi();
}
abstract class B extends A {
B() {
super();
System.out.println("in B");
}
}
class C {
C() {
super();
System.out.println("in C");
}
public void sayHi() {
System.out.println("Hi!");
}
}
Q() {
C Ccc = new C();
}
public static void main(String[] args) {
Q z = new Q();
}
}
Because the super class of c
is Object
因为
c
的超类是Object
Further class names start with upper case letters A,B,C,etc
进一步的类名称以大写字母
A,B,C,etc
开头
If you want to see both a
and b
invoked do this: 如果要同时看到
a
和b
,请执行以下操作:
class c extends b
Your class c
doesn't extend class b
or a
, so it can't call their constructors using super()
and calls Object
constructor instead. 您的类
c
不会扩展类b
或a
,因此它无法使用super()
调用其构造函数,而只能调用Object
构造函数。
Try this : 尝试这个 :
class c extends b {
public c(){
super();
//...
}
}
you should extends the abstract classes from class c
. 您应该从
c
类扩展抽象类。
class c extends b {
Without extending b
explicitely, it is equivalent to 在不显式扩展
b
,它等效于
class c extends Object {
and, hence call to super
invokes Object
constructor. 因此,对
super
调用将调用Object
构造函数。
Just make: 只需使:
class c extends b{
It will work now.. you have not extended b in c.. 现在可以使用了。.您还没有在c中扩展b。
See, we have the super class Object which all classes extend implicitly. 看,我们有一个超类Object,所有类都隐式扩展。 so if we have
public class A{}
then it is equivalent to writing public class A extends Object{}
. 因此,如果我们有
public class A{}
,则相当于编写public class A extends Object{}
。 But if you want to extend another class of your own then you have to explicitly mention it as public class B extends A{}
else java will treat Object as your super class. 但是,如果您想扩展自己的另一个类,则必须在
public class B extends A{}
明确提及它,否则Java会将Object视为您的超类。 (NOTE: even it this case Object is a super class to 'B' but this is because it is a super class to 'A' and hence 'B'). (注意:即使在这种情况下,Object也是'B'的超类,但这是因为它是'A'且因此是'B'的超类)。 So if you don't explicitly mention the 'B extends A' then class B will have Object as its only super class.
因此,如果您未明确提及“ B扩展A”,则类B将把Object作为其唯一的超类。 More over you dont need mention super() in the sub class constructor.
而且,您不需要在子类构造函数中提及super()。 This too is called implicitly.
这也被隐式调用。
C
and B
are not related in any way. C
和B
没有任何关系。 super class of C is java.lang.Object
by default C的超类默认为
java.lang.Object
C
should extend B
like C
应当延伸B
像
class C extends B{ .. }
In such a case, it will call the super class constructors. 在这种情况下,它将调用超类的构造函数。
Also, you do not need to call super()
explicitly as it is implicitly there. 另外,您不需要显式调用
super()
因为它隐式存在于此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.