简体   繁体   English

如何使不同子类的实例具有其共同父类 class 的相同实例?

[英]How do I make instances of different child classes have the same instance of their common parent class?

Problem statement: A learning environment contains both students and staffs who have attributes in common because they are both people .问题陈述:学习环境包含具有共同属性的学生教职员工,因为他们都是 In some institutions a student may also be working as a staff there at the same time.在某些机构中,学生也可能同时在那里担任教职员工 The implications of the above stated in Object Oriented Programming is: Object 面向编程中所述的上述含义是:

  • Student inherits Person Student Person
  • Staff inherits Person Staff Person
  • In some instances, a Student object and a Staff object should both be built on the same Person instance(Ie share the same parent class instance)在某些情况下, Student object 和Staff object 都应该构建在同一个Person实例上(即共享同一个父 class 实例)

How do I implement the last implication on the list using OOP?如何使用 OOP 实现列表中的最后一个含义? (C#, Java , C++, or Python preferably) (C#、 Java 、C++ 或 Python 最好)

I don't think that inheritence is the right solution to your problem.我认为继承不是解决您问题的正确方法。 Is it necessary that Student and Staff are different classes? StudentStaff必须是不同的班级吗?

It seems that it would be more appropriate that a student or a staff member are just instances of Person , and there is a member property role which may contain "Student" or "Staff" or both.学生或工作人员只是Person的实例似乎更合适,并且有一个可能包含"Student""Staff"或两者的成员属性role

With interface IPerson(interpreting inherit a litle broader) you could do somithing like this in Java (it's close but possibly no cigar):使用接口 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());

    }

}

Without using an interface you could override all getters, setters and alike and delegate it to the Person(ignoring the fields in the subclass, not nice either. This is composition disguised as inheritence):在不使用接口的情况下,您可以覆盖所有 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);
    }

}

You can only inherit single class in java, you can't have class WorkingStudent extends Student, Staff .您只能在 java 中继承单个 class,您不能拥有class WorkingStudent extends Student, Staff But you can implement multiple intefaces, so i will propose you a solution with interfaces.但是你可以实现多个接口,所以我会建议你一个接口的解决方案。 First let's define the student functionality.首先让我们定义学生功能。 A student has to study, so:学生必须学习,所以:

    public interface Student {
        
        void study();
    }

Now for the staff, he has to work obviously, so:现在对于员工来说,他显然必须工作,所以:

    public interface Staff {

        void work();
    }

A very simple person class:一个很简单的人class:

    public class Person {

        private final String name;


        public Person(String name) {
            this.name = name;
        }

        public String getName() {
            return this.name;
        }
    }

The average student just studies, or not, who knows, but let's assume he does.普通学生只是学习或不学习,谁知道呢,但让我们假设他学习。 He is a Person, and also does Student stuff.他是一个人,也做学生的事情。

    public class NormalStudent extends Person implements Student {

        public NormalStudent(String name) {
            super(name);
        }

        @Override
        public void study() {
            System.out.println(getName() + " studies a lot.");
        }
    }

And your average staff, who does not do his job very dutyfully.还有你的普通员工,他们没有尽职尽责。 He is a Person, and he does Staff thingies.他是一个人,他做员工的事情。

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

Now for the student, who is also a staff member:现在对于同时也是工作人员的学生:

    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 is a NormalStudent, also a Person(he inheriths the functionality for a Student from NormalStudent and the properties of a Person), and implements Staff thus receiving the corresponding functionality. WorkingStudent 是 NormalStudent,也是 Person(他从 NormalStudent 继承 Student 的功能和 Person 的属性),并实现 Staff 从而获得相应的功能。

And a small example:还有一个小例子:

    NormalStudent james = new NormalStudent("James");
    james.study();
    WorkingStudent george = new WorkingStudent("George");
    george.study();
    george.work();
    NormalStaff john = new NormalStaff("John");
    john.work();

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

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