简体   繁体   English

如何从子类访问超类对象?

[英]How to access the superclass object from the subclases?

I have been asked to write a program in java which has a class named 1.College(it has 'collegeName' and 'address' as its data members) 2.Department which inherits college(and has 'departmentName' and 'hodName' as its data members) 3. FaultyMember which inherits Department(it has 'facultyMember', 'facultyMemberQualification' and 'yearsOfExperience' as its data members). 我被要求用Java编写一个程序,该程序具有一个名为1.College的类(它的数据成员具有'collegeName'和'address')2.继承了College的部门(并且具有'departmentName'和'hodName'作为3.继承部门的FaultyMember(它的数据成员具有'facultyMember','facultyMemberQualification'和'yearsOfExperience')。

I want user to enter the name of the college and its address. 我希望用户输入学院的名称及其地址。 And then ask the user to input the no of departments in the college then the name of the each department and the corresponding HOD of the departments. 然后要求用户输入学院中的部门编号,然后输入每个部门的名称以及相应的部门HOD。 Now, as the user inputs the name of the first department the program also asks the no of faculty members in each departments and takes input of the faculty member name, qualification,and years of experience. 现在,当用户输入第一个部门的名称时,程序还会询问每个部门中教员的人数,并输入教员的姓名,资格和工作年限。

The problem which I am facing is that in my qestion the super class 'Department' needs to access the subclass 'FacultyMember'. 我面临的问题是,在我的问题中,超级类“部门”需要访问子类“ FacultyMember”。

How to solve the problem. 如何解决问题。

I have written the code so far as... 到目前为止,我已经编写了代码。

Hi without going into the design semantic of your senario. 您好,无需深入探讨您的senario的设计语义。 Answer to the question is. 问题的答案是。

A parent class has know obligation to know about its child. 父母阶级有义务了解其子女。 Hence you can't access child class fields inside a parent. 因此,您无法访问父级内部的子级字段。

About the design of the senario. 关于senario的设计。 Kindly read about "Has a" and "is a" relationship. 请阅读有关“具有”和“是”的关系。

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

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