简体   繁体   English

如何在 java 中为 object 赋予多个角色?

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

the student and teacher class inherited from person class, because inheritance is not flexible the object will either be one of the two.学生和老师 class 继承自人 class,因为 inheritance 不灵活,ZA8CFDE6331BD59EB2AC96F89111111C4 将是其中之一。 to solve this i added an abstract class Role and make association between person and role and student and teacher inherits from Role class so that an instance can has a role of both student and teacher.The question is to make instance of the Teacher class can also be a student or a student also teaches how to test in the main class?为了解决这个问题,我添加了一个抽象的 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);
        }
    }
} 

I would test it exactly as you would but the hierarchy of inheritance looks good.我会像您一样对其进行测试,但 inheritance 的层次结构看起来不错。 Why not give PersonRole a constructor and attributes so that the children can use super() in their constructors and reuse some code?为什么不给 PersonRole 一个构造函数和属性,以便孩子们可以在他们的构造函数中使用 super() 并重用一些代码?

Try this to print your roles:试试这个来打印你的角色:

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

Then add a toString method in the Student and Teacher classes:然后在 Student 和 Teacher 类中添加一个 toString 方法:

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

The result will be this:结果将是这样的:

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