如何从超类继承构造函数到子类
如何将超类的构造函数继承到子类?
How to inherit the constructor from a super class to a sub class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何将超类的构造函数继承到子类?
How to inherit the constructor from a super class to a sub class?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
构造函数不是继承的,您必须在子类中创建一个新的、原型相同的构造函数,该构造函数映射到超类中与其匹配的构造函数。
以下是其工作原理的示例:
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:
超类构造函数不能在扩展类中继承。尽管可以在扩展类构造函数中使用 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.
默认构造函数——没有参数(声明的或隐含的)的公共构造函数——默认被继承。您可以尝试以下代码作为示例:
如果您想从超类显式调用构造函数,则需要执行以下操作:
唯一的警告是 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:
If you want to explicitly call a constructor from a super class, you need to do something like this:
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 关键字(向下滚动子类构造函数)。如果我理解你的问题,你可能想调用超类构造函数?
值得注意的是,如果您没有显式调用超类构造函数,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.
假设您有
一个扩展 KKSSocket 的名为 KKSUDPSocket 的子类,
那么
您只需将参数传递到构造函数链上,就像对超类的方法调用一样,但使用 super(...) 引用超类构造函数,并且传入给定的参数。
Say if you have
then a sub-class named KKSUDPSocket extending KKSSocket could have:
and
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.
您继承类属性,而不是类构造函数。事情是这样的:
如果超类中没有添加构造函数,则编译器将添加一个无参数构造函数。每当创建子类的新实例时,都会隐式调用此默认构造函数。这里子类可能有构造函数,也可能没有构造函数,都可以。
如果超类中提供了构造函数,编译器将查看它是无参数构造函数还是带参数构造函数。
如果没有参数,那么编译器将为任何子类实例化调用它。这里子类可能有也可能没有构造函数,一切都可以。
如果父类中的 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 .