java 如何将子类参数传递给超类私有变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12945320/
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
how do I pass subclass parameters into the superclass private variables?
提问by reeltempting
I am confused on how to get parameters from new object instances to also flow into the super class to update the private fields in teh super class.
我对如何从新对象实例获取参数以流入超类以更新超类中的私有字段感到困惑。
So I am in an advanced Java class and I have homework that requires a "Person" Super Class and a "Student" subclass that extends Person.
所以我在高级 Java 类中,我有作业需要一个“Person”超类和一个扩展 Person 的“Student”子类。
The Person class stores the student name BUT it is the Student class constructor that accepts the Person name.
Person 类存储学生姓名,但接受 Person 姓名的是 Student 类构造函数。
assume no method in Person to make a variable method update...like subClassVar = setSuperClassVar();
假设 Person 中没有任何方法可以使变量方法更新...例如 subClassVar = setSuperClassVar();
EX:
前任:
public class Person
{
private String name; //holds the name of the person
private boolean mood; //holds the mood happy or sad for the person
private int dollars; //holds their bank account balance
}
class Student extends Person //I also have a tutor class that will extend Person as well
{
private String degreeMajor //holds the var for the student's major they have for their degree
Public Student(String startName, int startDollars, boolean startMood, String major)
{
degreeMajor = major; // easily passed to the Student class
name = startName; //can't pass cause private in super class?
mood = startMood; //can't pass cause private in super class?
dollars = startDollars; // see above comments
// or I can try to pass vars as below as alternate solution...
setName() = startName; // setName() would be a setter method in the superclass to...
// ...update the name var in the Person Superclass. Possible?
setMood() = startMood; // as above
// These methods do not yet exist and I am only semi confident on their "exact"...
// ...coding to make them work but I think I could manage.
}
}
The instructions for the homework were a bit vague in terms of how much changing to the superclass of Person I am allowed to make so if you all believe a good solid industry accepted solution involves changing the superclass I will do that.
作业的说明在我被允许对 Person 的超类进行多少更改方面有点含糊,所以如果你们都相信一个好的可靠的行业公认的解决方案涉及更改超类,我会这样做。
Some possible examples I see would be to make the private vars in Person class "protected" or to add setMethods() in the person class and then call them in the sub class.
我看到的一些可能的例子是使 Person 类中的私有变量“受保护”或在 person 类中添加 setMethods() 然后在子类中调用它们。
I am also open to general concept education on how to pass subclass contstructor parameters to a super class...and if possible do that right in the constructor portion of the code.
我也愿意接受关于如何将子类构造函数参数传递给超类的一般概念教育……如果可能的话,请在代码的构造函数部分正确执行此操作。
Lastly, I did search around but most of the similiar questions were really specific and complicated code....I couldnt find anything straight forward like my example above...also for some reason the forum post did not clump all of my code together so sorry for the confusing read above.
最后,我确实进行了搜索,但大多数类似的问题都是非常具体和复杂的代码......我找不到像上面的例子那样直接的东西......也出于某种原因,论坛帖子没有将我的所有代码聚集在一起很抱歉上面的混乱阅读。
Thanks all.
谢谢大家。
回答by Ted Hopp
First, you need to define a constructor for Person
:
首先,您需要为 定义一个构造函数Person
:
public Person(String startName, int startDollars, boolean startMood)
{
name = startName;
dollars = startDollars;
mood = startMood;
}
Then you can pass data up from the Student
constructor using super(...)
:
然后,您可以Student
使用super(...)
以下方法从构造函数向上传递数据:
public Student(String startName, int startDollars, boolean startMood, String major)
{
super(startName, startDollars, startMood);
. . .
}
Alternatively, you can define setters in the Person
class and invoke them from the Student
constructor.
或者,您可以在Person
类中定义 setter并从Student
构造函数调用它们。
public class Person
{
private String name; //holds the name of the person
private boolean mood; //holds the mood happy or sad for the person
private int dollars; //holds their bank account balance
public void setName(String name) {
this.name = name;
}
// etc.
}