Java 如何从超类继承构造函数到子类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2319817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:05:53  来源:igfitidea点击:

how to inherit Constructor from super class to sub class

javainheritanceconstructorsuperclass

提问by Anil

How to inherit the constructor from a super class to a sub class?

如何从超类继承构造函数到子类?

回答by Andrew Hare

Constructors are not inherited, you must create a new, identically prototyped constructor in the subclass that maps to its matching constructor in the superclass.

构造函数不是继承的,您必须在子类中创建一个新的、原型相同的构造函数,该构造函数映射到超类中的匹配构造函数。

Here is an example of how this works:

这是一个如何工作的示例:

class Foo {
    Foo(String str) { }
}

class Bar extends Foo {
    Bar(String str) {
        // Here I am explicitly calling the superclass 
        // constructor - since constructors are not inherited
        // you must chain them like this.
        super(str);
    }
}

回答by RichardOD

Read about the super keyword(Scroll down the Subclass Constructors). If I understand your question, you probably want to call a superclass constructor?

阅读super 关键字(向下滚动子类构造函数)。如果我理解您的问题,您可能想调用超类构造函数?

It is worth noting that the Java compiler will automatically put in a no-arg constructor call to the superclass if you do not explicitly invoke a superclass constructor.

值得注意的是,如果您不显式调用超类构造函数,Java 编译器将自动对超类进行无参数构造函数调用。

回答by Chris Dennett

Say if you have

说如果你有

/**
 * 
 */
public KKSSocket(final KKSApp app, final String name) {
    this.app = app;
    this.name = name;
    ...
}

then a sub-class named KKSUDPSocket extending KKSSocket could have:

那么扩展 KKSSocket 的名为 KKSUDPSocket 的子类可能具有:

/**
 * @param app
 * @param path
 * @param remoteAddr
 */
public KKSUDPSocket(KKSApp app, String path, KKSAddress remoteAddr) {
    super(app, path, remoteAddr);
}

and

/**
 * @param app
 * @param path
 */
public KKSUDPSocket(KKSApp app, String path) {
    super(app, path);
}

You simply pass the arguments up the constructor chain, like method calls to super classes, but using super(...) which references the super-class constructor and passes in the given args.

您只需将参数向上传递到构造函数链,就像对超类的方法调用一样,但是使用 super(...) 引用超类构造函数并传入给定的参数。

回答by MCory

Default constructors -- public constructors with out arguments (either declared or implied) -- are inherited by default. You can try the following code for an example of this:

默认构造函数——没有参数(声明或隐含)的公共构造函数——默认继承。您可以尝试以下代码作为示例:

public class CtorTest {
    public static void main(String[] args) {
        final Sub sub = new Sub();
        System.err.println("Finished.");
    }

    private static class Base {
        public Base() {
            System.err.println("In Base ctor");
        }
    }

    private static class Sub extends Base {
        public Sub() {
            System.err.println("In Sub ctor");
        }
    }
}

If you want to explicitly call a constructor from a super class, you need to do something like this:

如果要从超类中显式调用构造函数,则需要执行以下操作:

public class Ctor2Test {
    public static void main(String[] args) {
        final Sub sub = new Sub();
        System.err.println("Finished.");
    }

    private static class Base {
        public Base() {
            System.err.println("In Base ctor");
        }

        public Base(final String toPrint) {
            System.err.println("In Base ctor.  To Print: " + toPrint);
        }
    }

    private static class Sub extends Base {
        public Sub() {
            super("Hello World!");
            System.err.println("In Sub ctor");
        }
    }
}

The only caveat is that the super() call must come as the first line of your constructor, else the compiler will get mad at you.

唯一的警告是 super() 调用必须作为构造函数的第一行出现,否则编译器会生你的气。

回答by ManishS

Superclass constructor CAN'T be inherited in extended class. Although it can be invoked in extended class constructor's with super() as the first statement.

超类构造函数不能在扩展类中继承。尽管它可以在扩展类构造函数中使用 super() 作为第一条语句调用。

回答by Java Main

You inherit class attributes, not class constructors .This is how it goes :

您继承类属性,而不是类构造函数。它是这样的:

If no constructor is added in the super class, if no then the compiler adds a no argument contructor. This default constructor is invoked implicitly whenever a new instance of the sub class is created . Here the sub class may or may not have constructor, all is ok .

如果没有在超类中添加构造函数,如果没有,则编译器添加一个无参数构造函数。每当创建子类的新实例时,都会隐式调用此默认构造函数。这里的子类可能有也可能没有构造函数,一切正常。

if a constructor is provided in the super class, the compiler will see if it is a no arg constructor or a constructor with parameters.

如果超类中提供了构造函数,编译器将查看它是无参数构造函数还是带参数构造函数。

if no args, then the compiler will invoke it for any sub class instanciation . Here also the sub class may or may not have constructor, all is ok .

如果没有 args,则编译器将为任何子类实例化调用它。这里的子类也可能有也可能没有构造函数,一切都很好。

if 1 or more contructors in the parent class have parameters and no args constructor is absent, then the subclass has to have at least 1 constructor where an implicit call for the parent class construct is made via super (parent_contractor params) .

如果父类中有 1 个或多个构造函数有参数并且没有 args 构造函数不存在,则子类必须至少有 1 个构造函数,其中通过 super (parent_contractor params) 对父类构造进行隐式调用。

this way you are sure that the inherited class attributes are always instanciated .

这样你就可以确定继承的类属性总是被实例化。