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

发布于 2024-08-22 20:18:34 字数 23 浏览 6 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

年华零落成诗 2024-08-29 20:18:34

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

以下是其工作原理的示例:

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);
    }
}

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);
    }
}
迷你仙 2024-08-29 20:18:34

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

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.

逆夏时光 2024-08-29 20:18:34

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

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");
        }
    }
}

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

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");
        }
    }
}

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

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.

心意如水 2024-08-29 20:18:34

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

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

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

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.

顾北清歌寒 2024-08-29 20:18:34

假设您有

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

一个扩展 KKSSocket 的名为 KKSUDPSocket 的子类,

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

那么

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

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

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:

/**
 * @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.

永不分离 2024-08-29 20:18:34

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

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

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

如果没有参数,那么编译器将为任何子类实例化调用它。这里子类可能有也可能没有构造函数,一切都可以。

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

这样您就可以确保继承的类属性始终被实例化。

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 .

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) .

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文