简体   繁体   English

构造函数在Java中调用继承的构造函数

[英]Constructor calling inherited constructor in java

How would I go about doing this in java? 我将如何在Java中执行此操作?

I need the constructor in my subclass to call the inherited constructor in my superclass. 我需要子类中的构造函数在我的超类中调用继承的构造函数。 is there a special syntax that i need to use in order to do this? 我需要使用一种特殊的语法来做到这一点吗?

I've used extends to inherit from the Person class. 我曾经使用过扩展继承自Person类。 How would i go about using super()? 我将如何使用super()?

Here is my class: 这是我的课:

public class Student extends Person
{
   protected int id;

   public Student()
   {
   // how do i call the inherited constructor here?
   }

}

super(arg1, arg2, etc);

在未显式调用super()的任何构造函数的开头都有一个隐式调用(不带参数)。

Exactly like that: super() . 就像这样: super() But I assume you need to call a method with arguments since there's an implicit call to super() . 但是我假设您需要使用参数来调用方法,因为存在对super()的隐式调用。 Add arguments, like this: super(arg0, arg1, arg2, etc); 添加如下参数: super(arg0, arg1, arg2, etc); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM