简体   繁体   English

java抽象类,构造器未在超类中调用,为什么?

[英]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. 我有两个抽象类AB ,一个具体类C ,其中包括抽象方法。

When I create a new C , I expected to see the constructors of A and B being called. 当我创建一个新的C ,我希望看到AB的构造函数被调用。 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: 如果要同时看到ab ,请执行以下操作:

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不会扩展类ba ,因此它无法使用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. CB没有任何关系。 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.

相关问题 如何确保从子类(Java)中的方法在抽象超类中调用某些方法 - How to ensure a certain methods gets called in abstract super-class from method in sub-class (Java) 如果超类没有一个默认构造函数,为什么此Java代码中的子类具有默认构造函数? - Why does the subclass in this Java code have a default constructor if the super-class does not have one? 调用超类构造函数的“找不到符号”错误消息 - “Cannot find symbol” error message calling super-class constructor 关于在JAVA中将子类强制转换为超类 - About casting sub-class to super-class in JAVA 在 Java 中,您可以将子类的对象存储为超类类型,为什么要这样做? - In java you're able to store objects of a subclass as a super-class type, why would you do this? Java继承层次结构 - 实现超类正在实现的接口 - Java inheritance hierarchy — implementing an interface a super-class is implementing 为什么添加一个超级类会破坏我的Spring bean? - Why does adding a super-class break my Spring beans? 在Java中,为什么超类方法不能从子类实例访问受保护的或私有的方法/变量? - In Java, why can't a super-class method access protected or private methods/variables from a sub-class instance? 为什么在Java抽象类中需要Protected构造函数 - Why is the need of Protected constructor in Abstract class in java Java抽象类,抽象的构造函数 - Java abstract class, abstract constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM