目前,我正在使用ASP.NET MVC 4和代码优先方法研究C#。我是Visual Basic Developer,现在我想启动C#。而且,现在我遇到了必须管理多重继承的情况。但是,我想的Class是不可能的。因此,我应该如何管理这些类:

//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 中。因为,我必须将其用于其他类,但这些不是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 {
}

或者,您可以创建一个接口(interface):
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; }
    }
}

关于C#多重继承,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18438196/

10-11 01:53