如何从超类继承构造函数到子类 [英] how to inherit Constructor from super class to sub class

查看:29
本文介绍了如何从超类继承构造函数到子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

这篇关于如何从超类继承构造函数到子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆