[英]Returning data from a sub-class
I have a subclass "shopStaff" within class "staff". 我在“员工”类中有一个子类“ shopStaff”。 I have a meathod getPerson which I need to send the data set in staff and the data set in shopStaff as a single string.
我有一个Meathod getPerson,我需要将在staff中的数据集和在shopStaff中的数据集作为单个字符串发送。
This is my code 这是我的代码
// in staff //员工
public String getPerson()
{
return format(name) + format(yearJoined);
}
// in shopStaff //在shopStaff中
public String getPerson()
{
super.getPerson();
return format(department);
}
however, when I invoke the getPerson meathod in the subclass it only returns the department information, not the name and yearjoined (that I only set in the superclass. 但是,当我在子类中调用getPerson肉类时,它仅返回部门信息,而不返回名称和年份(我仅在超类中设置)。
I thought when I used the super. 我想当我使用超级。 meathod it would return everything from the class above in the hierarchy.
Meathod它将返回层次结构中以上类的所有内容。 Is this not the case?
不是吗? If not could anyone tell me how I access the information set in the superclass?
如果没有人可以告诉我如何访问超类中的信息集?
Thanks 谢谢
CJ CJ
Your return value from super.getPerson()
is not returned with the return
statement in your current getPerson()
method. super.getPerson()
返回值不会与当前getPerson()
方法中的return
语句一起return
。 It is just lost in your code. 它只是丢失在您的代码中。
You need to change your shopStaff
method to return the super class values with your subclass return statement: - 您需要更改
shopStaff
方法,以使用子类return语句返回超类值:-
public String getPerson()
{
return super.getPerson() + format(department);
}
When you call: 你打电话时:
super.getPerson();
The return is discarded as it is not stored anywhere. 该退货被丢弃,因为它没有存储在任何地方。 You'd want to do this:
您想这样做:
//in shopStaff
public String getPerson() {
return super.getPerson() + format(department);
}
public String getPerson()
{
String fromSuper=super.getPerson();
return fromSuper + format(department);
}
Just calling super will not get data unless you read and use it in subclass. 除非您在子类中读取和使用它,否则仅调用super不会获得数据。
May be something like this will work: 可能是这样的事情将工作:
public String getPerson()
{
String person = super.getPerson();
return person+format(department);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.