簡體   English   中英

如何在 java 中為 object 賦予多個角色?

[英]How to give more than one role for an object in java?

學生和老師 class 繼承自人 class,因為 inheritance 不靈活,ZA8CFDE6331BD59EB2AC96F89111111C4 將是其中之一。 為了解決這個問題,我添加了一個抽象的 class 角色,並在人與角色之間建立關聯,學生和老師繼承自角色 class,這樣一個實例就可以同時扮演學生和老師的角色。問題是讓老師的實例 ZA2F2ED4F8EBC2CBB4C21A29DZ 也可以是學生還是學生也教如何在主class中測試?

類圖

public class Person {

    private int ssn;
    private String name;
    private List<PersonRole> roles;

    public Person(int ssn, String name) {
        this.ssn = ssn;
        this.name = name;
        roles = new ArrayList<PersonRole>();
    }

    public int getSsn() {
        return ssn;
    }

    public String getName() {
        return name;
    }
   }

/////////////// PersonRole /////////////////

public abstract class PersonRole {
    private List<PersonRole> learning;
    private List<PersonRole> teaching;

    public PersonRole(List<PersonRole> learning, List<PersonRole> teaching) {
        this.learning = learning;
        this.teaching = teaching;
    }
     public List<PersonRole> getLearning() {
        return learning;
    }
    public List<PersonRole> getTeaching() {
        return teaching;
    }
    
    public void addAsStudent(PersonRole p) {
        learning.add(p);
    }
    public void addAsTeacher(PersonRole p) {
        learning.add(p);
    }
    }

/////////////  Teacher //////////////

public class Teacher extends PersonRole {

    private String faultyName;
    private int yearsOfExperence;

    public Teacher(String faultyName, int yearsOfExperence) {
        super();
        this.faultyName = faultyName;
        this.yearsOfExperence = yearsOfExperence;
    }

    public String getFaultyName() {
        return faultyName;
    }

    public int getYearsOfExperence() {
        return yearsOfExperence;
    }

}

///////////  Student  ////////////////

public class Student extends PersonRole {

    private String collegeName;
    private String major;

    public Student(String collegeName, String major) {
        super();
        this.collegeName = collegeName;
        this.major = major;
    }

    public String getCollegeName() {
        return collegeName;
    }

    public String getMajor() {
        return major;
    }

}


////// Main ///////////////

public class Main {

    public static void main(String[] args) {
        PersonRole p1 = new Student("univ of Michigan", "CS");
        PersonRole p3 = new Student("Iowa state univ", "managemnt");
        PersonRole p2 = new Teacher("computer science", 3);
        PersonRole p4 = new Teacher("Management", 2);

        Person person = new Person(1042327867, "Mike Rose");

        person.addRole(p1);
        person.addRole(p2);
        person.addRole(p3);
        person.addRole(p4);

        for (PersonRole c: roles) {
            System.out.println(c);
        }
    }
} 

我會像您一樣對其進行測試,但 inheritance 的層次結構看起來不錯。 為什么不給 PersonRole 一個構造函數和屬性,以便孩子們可以在他們的構造函數中使用 super() 並重用一些代碼?

試試這個來打印你的角色:

List<PersonRole> roles = person.getRole();
for(PersonRole role: roles) {
    System.out.println(role);
}

然后在 Student 和 Teacher 類中添加一個 toString 方法:

public String toString() {
    return "Student {" + "collegeName=" + collegeName + ", major=" + major + '}';
}
public String toString() {
    return "Teacher {" + "faultyName=" + faultyName + ", yearsOfExperence=" + yearsOfExperence + '}';
}

結果將是這樣的:

Student {collegeName=maharashi univ, major=compro}
Teacher {faultyName=computer science, yearsOfExperence=3}
Student {collegeName=Iowa state univ, major=managemnt}
Teacher {faultyName=Management, yearsOfExperence=2}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM