Java,如何让上层类的变量保持为null;如何“隐藏”来自子类的变量?
我有四节课。 Person
类,以及另外三个类,Student
、Professor
、Tutor
,每个类都扩展了 Person 类。 Person 类有 2 个变量 studentID
和 staffID
。但是,只有学生可以拥有 studentID != null
,导师和教授可以拥有 staffID != null
。现在,在创建新对象 Student 时,如何确保无论 StaffID 是什么,始终保持为 null? StaffID 必须保留在 Person 类中,因此不能移动它。
I have four classes. Class Person
, and three more, Student
, Professor
, Tutor
, each of which extends class Person. Class Person has 2 variables studentID
and staffID
. However, only student can have studentID != null
, and Tutors and Professors can have staffID != null
. Now when creating new object Student, how can I make sure that no matter what staffID always stays null? staffID must remain in class Person, so no moving it around.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
继承的要点是,只有对父类有效的值和函数(成员)才会放置在父类中,而任何特定于子类的成员都会放置在子类中。
不要滥用面向对象,将
staffID
移动到您的子类中。我的建议是创建一个包含
staffID
的Staff
类,让它继承Person
,然后拥有Professor
和Tutor
继承自它。然后将studentID
移动到Student
类中。这样,只有Professor
和Tutor
可以访问staffID
而Student
则不能,反之亦然学生ID
。所有类仍然可以分配给Person
类型。The whole point of inheritance is that only values and functions (members) that are valid for a parent class are placed in the parent class, and any child-specific members are placed in the child class.
Don't abuse OO, move
staffID
into your child class.My suggestion would be to create a
Staff
class that containsstaffID
, have it inheritPerson
, then haveProfessor
andTutor
inherit from it. Then movestudentID
into theStudent
class. That way onlyProfessor
andTutor
have access tostaffID
andStudent
does not, and vice versa forstudentID
. All classes can still be assigned to aPerson
type.创建接口来限制操作:
创建您的 Person 类: 使用
接口创建您的 Student 和 Staff 子类以定义唯一允许的操作:
现在创建您的 Tutors 和 Professors:
Student 类的对象没有可以影响 StaffId 的操作,并且 Staff 类的对象(包括 Tutor 和 Professor 子类)没有可以影响 StudentId 的操作。根据需要添加其他操作(常见的可以直接进入Person)。
作为奖励,您可以使用接口来更好地定义方法,如下所示:
Create interfaces to restrict operations:
Create your Person class:
Create your Student and Staff subclasses with the Interfaces to define the only allowed operations:
And now create your Tutors and Professors:
Objects of the Student class don't have operations that can affect the staffId, and objects of the Staff class (including the Tutor and Professor subclasses) don't have operations that can affect the studentId. Add other operations as necessary (common ones can go into Person directly).
As a bonus, you can use the interfaces to better define methods, like this:
studentID
应该是Student
的成员,而不是Person
的成员。同样,Professor 和 Tutor 都应该扩展一个 Faculty 类,该类具有一个 staffId ,而 staffId 又扩展于 PersonThe
studentID
should be a member ofStudent
notPerson
. LikewiseProfessor
andTutor
should both extend a classFaculty
which has astaffId
which in turn extendsPerson
将此变量设为私有并创建 getter 和 setter。
根据类实现不同的方法行为。
对于stasfs - 保护设置studentId
对于学生 - 保护设置工作人员 ID。
Make this variables private and create getter and setter.
Depending of class implement different methods behaviors.
For stasfs - protect setting studentId
for student - protect setting staffId.
不要扩展 Person 类。使用组合来代替。
如果您想要多态性,请创建一个 Blammy 接口,为学生、教授和导师提供通用接口。学生 ID 和员工 ID 内容可能是也可能不是 Blammy 界面的一部分。
每个类(学生、教授和导师)都将包含 Person 的私有实例,并代理他们想要公开的任何 Person 功能。
学生将有一个 StudentId 数据成员,教授和导师将有一个 StaffId 数据成员。教授和导师的 getStudentId() 将始终返回 null,而学生的 get StaffId 也将始终返回 null。
don't extend the Person class. Use composition instead.
If you want polymorphism, create a Blammy interface that provides a common interface for Student, Professor, and Tutor. The student ID and staff Id stuff could or could not be part of the Blammy interface.
Each class, Student, Professor, and Tutor would contain a private instance of Person and proxy to any Person functionality they wanted to expose.
Student would have a studentId data member and Professor and Tutor would have a staffId data member. The getStudentId() for profesor and tutor would always return null and the get staffId for student would also always return null.
将变量设为私有,创建 getter 和 setter。然后让构造函数初始化这两个变量,并简单地为教师的学生 ID 传递 null,为学生的 StaffID 传递 null。
构造函数--->
当你初始化时---->
make the variable private, create getters and setters. Then make your constructor initialize both variables, and simple pass null for student ID for teachers, and null for staffID for students.
constructor --->
when you initialize ---->