問題陳述:學習環境包含具有共同屬性的學生和教職員工,因為他們都是人。在某些機構中,學生也可能同時在那里擔任教職員工。面向物件編程中上述內容的含義是:
Student繼承PersonStaff繼承Person- 在某些情況下,一個
Student物件和一個Staff物件都應該建立在同一個Person實體上(即共享同一個父類實體)
如何使用 OOP 實作串列中的最后一個含義?(最好是 C#、Java、C 或 Python)
uj5u.com熱心網友回復:
我認為繼承不是解決您問題的正確方法。是否有必要Student并且Staff是不同的類?
學生或作業人員只是 的實體似乎更合適Person,并且有一個role可能包含"Student"或包含"Staff"兩者的成員屬性。
uj5u.com熱心網友回復:
使用介面 IPerson(解釋繼承更廣泛)你可以在 Java 中做這樣的事情(它很接近但可能沒有雪茄):
public interface IPerson
{
default String getName() {
return getPerson().getName();
};
default void setName(String name) {
getPerson().setName(name);
}
IPerson getPerson();
}
public class Person implements IPerson
{
private String name;
@Override
public String getName()
{
return name;
}
@Override
public void setName(String name)
{
this.name = name;
}
public IPerson getPerson() {
return this;
}
}
public class Staff implements IPerson
{
private Person person;
private String socialInsuranceNumber;
public String getSocialInsuranceNumber()
{
return socialInsuranceNumber;
}
public void setSocialInsuranceNumber(String socialInsuranceNumber)
{
this.socialInsuranceNumber = socialInsuranceNumber;
}
public Staff(Person person)
{
super();
this.person = person;
}
@Override
public IPerson getPerson()
{
return person;
}
}
public class Student implements IPerson
{
private Person person;
private String matriculationNumber;
public String getMatriculationNumber()
{
return matriculationNumber;
}
public void setMatriculationNumber(String matriculationNumber)
{
this.matriculationNumber = matriculationNumber;
}
public Student(Person person)
{
super();
this.person = person;
}
@Override
public IPerson getPerson()
{
return person;
}
public static void main(String[] args) {
Person peterP = new Person();
peterP.setName("Peter");
Staff peterStaff = new Staff(peterP);
peterStaff.setSocialInsuranceNumber("123");
Student peterStudent = new Student(peterP);
peterStudent.setMatriculationNumber("456");
peterStudent.setName("Dr. Peter");
System.out.println(peterStaff.getName());
}
}
在不使用介面的情況下,您可以覆寫所有 getter、setter 等并將其委托給 Person(忽略子類中的欄位,也不好。這是偽裝成繼承的組合):
public class Student extends Person
{
private Person person;
private String matriculationNumber;
public String getMatriculationNumber()
{
return matriculationNumber;
}
public void setMatriculationNumber(String matriculationNumber)
{
this.matriculationNumber = matriculationNumber;
}
public Student(Person person)
{
super();
this.person = person;
}
@Override
public String getName()
{
return person.getName();
}
@Override
public void setName(String name)
{
person.setName(name);
}
}
uj5u.com熱心網友回復:
你只能繼承java中的單個類,你不能擁有class WorkingStudent extends Student, Staff. 但是你可以實作多個介面,所以我會建議你一個介面的解決方案。首先讓我們定義學生功能。學生必須學習,所以:
public interface Student {
void study();
}
現在對于員工來說,他顯然必須作業,所以:
public interface Staff {
void work();
}
一個非常簡單的 person 類:
public class Person {
private final String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
普通學生只是學習或不學習,誰知道呢,但讓我們假設他學習。他是一個人,也做學生的事情。
public class NormalStudent extends Person implements Student {
public NormalStudent(String name) {
super(name);
}
@Override
public void study() {
System.out.println(getName() " studies a lot.");
}
}
還有你的普通員工,他們沒有盡職盡責。他是一個人,他做員工的事情。
public class NormalStaff extends Person implements Staff {
public NormalStaff(String name) {
super(name);
}
@Override
public void work() {
System.out.println(getName() " does whatever the staff does");
}
}
現在對于同時也是作業人員的學生:
public class WorkingStudent extends NormalStudent implements Staff {
public WorkingStudent(String name) {
super(name);
}
@Override
public void work() {
System.out.println(getName() " has finished studying, but does not go to the party and instead does his obligations as a staff member of his university.");
System.out.println(getName() " has a bright future ahead of him. Probably.");
}
}
WorkingStudent 是 NormalStudent,也是 Person(他從 NormalStudent 繼承 Student 的功能和 Person 的屬性),并實作 Staff 從而獲得相應的功能。
還有一個小例子:
NormalStudent james = new NormalStudent("James");
james.study();
WorkingStudent george = new WorkingStudent("George");
george.study();
george.work();
NormalStaff john = new NormalStaff("John");
john.work();
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/414348.html
標籤:
