如何在java中为一个对象赋予多个角色?

6gpjuf90  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(321)

学生类和教师类继承自人类,由于继承不灵活,对象也会是二者之一。为了解决这个问题,我增加了一个抽象的类角色,使人与角色和学生、教师之间的联系从角色类继承而来,这样一个示例既可以是学生又可以是教师,问题是使教师类的示例也可以是学生,或者学生也可以在主类中教如何测试?

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);
        }
    }
}
lc8prwob

lc8prwob1#

尝试以下操作打印角色:

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

然后在学生和教师类中添加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}
djmepvbi

djmepvbi2#

我会像你一样测试它,但是继承的层次结构看起来不错。为什么不给personrole一个构造函数和属性,这样子级就可以在构造函数中使用super()并重用一些代码呢?

相关问题