以下是完全合法的:
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 不支持多重继承。您只能实现多个接口。