2

我有两个接口:

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

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

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

  1. 只实现学生(并且没有老师的属性)
  2. 只实现教师(并且没有学生的属性)
  3. 实现这两个接口,没有冗余的通用属性(名称)

任何想法 ?

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

4

6 回答 6

5

我会提取一个通用接口:

interface IPerson
{
    string Name { get; }
}

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

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

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

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

于 2013-02-14T11:09:31.567 回答
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

如果属性的实现是相同的,Student并且LearningTeacher您可以在内部创建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 不支持多重继承。您只能实现多个接口。

于 2013-02-14T11:11:26.063 回答
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; }
}
于 2013-02-14T11:16:41.100 回答
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;
于 2013-02-14T11:09:57.340 回答
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; }
    }

}
于 2013-02-14T11:12:43.037 回答
0

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

interface IPerson
{
    string Name { get; }

}


 interface ILearn
{
    Learn(  EducationLevel )

}

interface ITeach

{
      Teach(  Department  )

}
于 2013-02-14T14:09:06.993 回答