通过两个不同的接口访问同一对象

时间:2013-02-14 11:07:19

标签: c# object

我有两个界面:

interface IStudent
{
    string Name { get; }
    string EducationLevel { get; }
}

interface ITeacher
{
    string Name { get; }
    string Department { get; }
}

你如何在C#中设计它,以便一个对象可以

  1. 仅实施学生(并且没有教师的属性)
  2. 仅实施教师​​(并且没有结果的属性)
  3. 实现两个接口,没有commone属性(Name)的冗余
  4. 有什么想法吗?

    我可以直接实现两个接口的2个类。但对于一个既是老师又是学生的人,我将有两个不同的对象:我只想要一个!

6 个答案:

答案 0 :(得分:5)

我会提取一个通用界面:

interface IPerson
{
    string Name { get; }
}

interface IStudent : IPerson
{
    string EducationLevel { get; }
}

interface ITeacher : IPerson
{
    string Department { get; }
}

考虑为什么两个接口具有相同的属性。他们都有名字,为什么他们有名字?因为他们都是一个人。所以你有你的共同界面。

实现两个接口的类只需要实现Name一次,因为它是IPerson的一部分。

答案 1 :(得分:3)

以下是完全合法的:

public class LearningTeacher : ITeacher, IStudent
{
    public string EducationLevel
    {
        get { return /*...*/; }
    }

    public string Department
    {
        get { return /*...*/; }
    }

    public string Name
    {
        get { return /*...*/; }
    }
}

Name属性是ITeacher.Name IStudent.Name的实现。

如果StudentLearningTeacher中的属性实现相同,则可以使LearningTeacher内部有Student个实例和Teacher实例,使其成为一名装饰者。像这样,你不必重复实现:

public class LearningTeacher : ITeacher, IStudent
{
    Teacher _teacher;
    Student _student;

    public LearningTeacher(string name, string educationalLevel,
                           string department)
    {
        _student = new Student(name, educationalLevel);
        _teacher = new Teacher(name, department);
    }

    public string EducationLevel
    {
        get { return _student.EducationLevel; }
    }

    public string Department
    {
        get { return _teacher.Department; }
    }

    public string Name
    {
        get { return _student.Name; }
    }
}

这是唯一的方法,因为.NET不支持多重继承。您只能实现多个接口。

答案 2 :(得分:1)

怎么样,

interface IPerson
{
    string Name { get; }
}

interface IStudent : IPerson
{
    string EducationLevel { get; }
}

interface ITeacher : IPerson
{
    string Department { get; }
}

您可以在这样的实现中使用这些接口,

abstract class Person : IPerson
{
    public virtual string Name { get; set; }
}

class Student : Person, IStudent
{
    public string EducationLevel { get; set; }
}

class Teacher : Person, ITeacher
{
    public string Department { get; set; }
}

class StudentTeacher : Person, IStudent, ITeacher
{
    public string EducationLevel { get; set; }
    public string Department { get; set; }
}

答案 3 :(得分:0)

interface IPerson
{
    string Name { get; }
}

interface IStudent : IPerson
{
    string EducationLevel { get; }
}

interface ITeacher : IPerson
{
    string Department { get; }
}

IPerson P1 = SomeStudent;
IPerson P2 = SomeTeacher;
IStudent S = P1 as IStudent;
ITeacher T = P2 as ITeacher;

答案 4 :(得分:0)

这个实现适合你吗?

interface IStudent
{
    string Name { get; }
    string EducationLevel { get; }
}

interface ITeacher
{
    string Name { get; }
    string Department { get; }
}

public class Person: IStudent, ITeacher
{
    public string EducationLevel
    {
        get { return String.Empty; }
    }

    public string Name
    {
        get { return String.Empty; }
    }

    public string Department
    {
        get { return String.Empty; }
    }

}

答案 5 :(得分:0)

我们能否区分这些实体不是在本体论上而是在认识论上,即通过各自的思维行为模式来区分这些实体?

interface IPerson
{
    string Name { get; }

}


 interface ILearn
{
    Learn(  EducationLevel )

}

interface ITeach

{
      Teach(  Department  )

}