目前我正在学习 C# 和 ASP.NET MVC 4代码优先方法。我是 Visual Basic 开发人员,现在我想开始 C#。而且,现在我遇到了必须管理多重继承的情况。但是,对于Class i来说这是不可能的。那么,我应该如何管理我拥有的这些课程:
//I have the Following Person Class which Hold Common Properties
//and a Type of Person e.g : Student, Faculty, Administrative
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Type { get; set; }
}
//This is my Student Class, which is Derived from Person
public class Student : Person
{
public DateTime DateOfBirth { get; set; }
public DateTime EnrollmentDate { get; set; }
public string Remarks { get; set; }
public bool Approved { get; set; }
public DateTime ApprovedDate { get; set; }
public int ApprovedUserId { get; set; }
}
//This is my Faculty Class, which is also Derived from Person
public class Faculty : Person
{
public DateTime HiredDate { get; set; }
public bool Approved { get; set; }
public DateTime ApprovedDate { get; set; }
public int ApprovedUserId { get; set; }
}
我想要做的是 Approved、ApprovedDate 和 ApprovedUserId 也很常见。我想指定这些属性,例如:
public class Approve {
public bool Approved { get; set; }
public DateTime ApprovedDate { get; set; }
public int ApprovedUserId { get; set; }
}
并且,想要使用类似:
public class Student : Person, Approve
{
public DateTime DateOfBirth { get; set; }
public DateTime EnrollmentDate { get; set; }
public string Remarks { get; set; }
}
而且,我不能把那些东西放进去PERSON。因为,我必须将其用于其他类,但那些不是人。
那么,我该如何实现这一目标...
请给我一个上述情况的例子。
请帮忙。并且,非常感谢您。
一种可能的解决方案是修改您的层次结构:
public class PersonWithApprove : Person { // TODO: replace with non disgusting name
public bool Approved { get; set; }
// etc...
}
public class Student : PersonWithApprove {
}
public class Faculty : PersonWithApprove {
}
或者你可以创建一个界面:
public interface IApprove {
bool Approved { get; set; }
// etc
}
public class Student : Person, IApprove {
}
你也可能离开课堂Approve
因此,并拥有具有该类型属性的类:
public class Student : Person {
Approve _approve = new Approve();
public Approve Approve {
get { return _approve; }
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)