Java - 扩展类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19501638/
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
Java - Extending Classes
提问by Sameer Anand
I have a Meeting class and I want to create a BusinessMeeting class which extends Meeting.
我有一个 Meeting 类,我想创建一个扩展 Meeting 的 BusinessMeeting 类。
This is my Meeting class:
这是我的会议类:
public class Meeting {
private String name;
private String location;
private int start, stop;
public Meeting(String name, String location, int start, int stop) {
this.name = name;
this.location = location;
this.start = start;
this.stop = stop;
}
This is my BusinessMeeting class:
这是我的 BusinessMeeting 课程:
public class BusinessMeeting extends Meeting {
public BusinessMeeting() {
super();
}
}
In my BusinessMeeting class, I am getting the error:
在我的 BusinessMeeting 课程中,我收到错误消息:
constructor Meeting in class Meeting cannot be applied to given types; required: String,String,int,int found: no arguments
类 Meeting 中的构造函数 Meeting 不能应用于给定类型;要求:字符串,字符串,整数,整数发现:无参数
I am not really sure why I am getting that error message. Shouldn't the BusinessMeeting class inherit all of the variables from my Meeting class?
我不确定为什么会收到该错误消息。BusinessMeeting 类不应该从我的 Meeting 类继承所有变量吗?
采纳答案by flavian
public class Meeting {
public Meeting(String name, String location, int start, int stop) {
this.name = name;
this.location = location;
this.start = start;
this.stop = stop;
}
}
Now say you want to extend your BusinessMeeting
class with a businessId
.
现在说你想BusinessMeeting
用businessId
.
public class BusinessMeeting {
private Integer businessId;
public BusinessMeeting(String name, String location, int start, int stop, int business) {
// because super calls one of the super class constructors(you can overload constructors), you need to pass the parameters required.
super(name, location, start, stop);
businessId = business;
}
}
回答by alfasin
The constructor you created accepts 4 arguments and you call it without any.
If you want it to be initialized without parameters as well - add an empty constructor to Meeting
:
您创建的构造函数接受 4 个参数,您可以不带任何参数调用它。如果您还希望它在没有参数的情况下进行初始化 - 添加一个空的构造函数到Meeting
:
public Meeting(){}
回答by nhgrif
By declaring a constructor, your Meeting
class does not have a 0 argument constructor, which is what you're trying to call with super();
. You need to call a super constructor that matches the available constructors in your super class.
通过声明构造函数,您的Meeting
类没有 0 参数构造函数,这就是您尝试使用super();
. 您需要调用与超类中可用的构造函数相匹配的超构造函数。
To fix this, add a 0-argument constructor to Meeting
class, or call the 4-argument constructor you already have.
要解决此问题,请向Meeting
类添加一个 0 参数构造函数,或调用您已有的 4 参数构造函数。
回答by Scott
As alfasin stated, you have created a constructor. The standardstates that this constructor is created if no other constructor is defined. You must create a parameterless constructor yourself if you would like to call super()
正如 alfasin 所说,您已经创建了一个构造函数。该标准规定,如果没有其他的构造函数定义这个构造函数创建。如果您想调用 super(),您必须自己创建一个无参数构造函数
回答by Zapateus
You should add constructor with empty parameters in meeting class or you can call super(name,location,start,stop) instead of super() in BussinessMeeting class
您应该在会议类中添加带有空参数的构造函数,或者您可以在 BussinessMeeting 类中调用 super(name,location,start,stop) 而不是 super()
回答by Sage
You are calling super class constructor with empty argument:
您正在使用空参数调用超类构造函数:
public BusinessMeeting() {
super();
}
But your super class has the constructor: Meeting(String, String, int, int)
.
但是您的超类具有构造函数:Meeting(String, String, int, int)
.
With super(), the superclass no-argumentconstructor is called(If it exists). With super(parameter list), the superclass constructor with a matching parameter listis called. So either you have to add an constructor with empty argument to Meeting e.g., public Meeting(){}
or you need to call super(String, String, int, int)
使用super(),调用超类无参数构造函数(如果存在)。使用super(parameter list),调用具有匹配参数列表的超类构造函数。因此,要么您必须向会议添加一个带有空参数的构造函数,例如,public Meeting(){}
要么您需要调用super(String, String, int, int)
public class Meeting {
// your variable declaration
public Meeting(String name, String location, int start, int stop) {
// your assignment
}
public Meeting(){}
}
public class BusinessMeeting extends Meeting {
public BusinessMeeting()
{
// super(); un-comment if you need to check by commenting below
super("name", "loc", 1, -1);
}
}
Check out: Using the Keyword super
退房:使用关键字 super
回答by Stefan Freitag
Yes and no. It depends on how you declare the variables.
是和否。这取决于您如何声明变量。
Private variables in the super class are not accessible by the subclass. Usually you add protected getter and setter-Methods to the superclass. Those methods can be used in the subclass to access the variables. (a sometimes sloppy way would be to declare the variables themselve as protected)
子类无法访问超类中的私有变量。通常,您将受保护的 getter 和 setter-Methods 添加到超类。可以在子类中使用这些方法来访问变量。(有时草率的方法是将变量本身声明为受保护的)
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. Java Inheritance Webpage
子类不继承其父类的私有成员。但是,如果超类具有访问其私有字段的公共或受保护方法,则子类也可以使用这些方法。Java 继承网页
But concerning the error you mentioned:
You are calling super()
. This means you are calling the default constructor of the superclass. As you have written your own constructor in the superclass, there is no default constructor. Latter one is only created when you do not define a constructor.
但是关于您提到的错误:您正在调用super()
. 这意味着您正在调用超类的默认构造函数。由于您在超类中编写了自己的构造函数,因此没有默认构造函数。后一个仅在您未定义构造函数时创建。
Hence, when calling the constructor of the superclass - which takes in your case four arguments - you have to pass these arguments via super()
like this:
因此,当调用超类的构造函数时——在你的例子中它接受四个参数——你必须通过super()
这样的方式传递这些参数:
super("a name", "a location", 0, 1)
super("a name", "a location", 0, 1)