C#多重继承

Nir*_*edi 12 c# inheritance multiple-inheritance c#-4.0

目前我正在使用代码优先方法学习ASP.NET MVC 4的C#.我是Visual Basic开发人员,现在我想启动C#.而且,现在我遇到了我要管理多重继承的情况.但是,我认为这是不可能的.那么,我应该如何管理这些课程:

//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; }
}
Run Code Online (Sandbox Code Playgroud)

我想要的是Approved,ApprovedDate和ApprovedUserId也很常见.我想指定那些属性,如:

 public class Approve {
    public bool Approved { get; set; }
    public DateTime ApprovedDate { get; set; }
    public int ApprovedUserId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

并且,想要使用像:

public class Student:Person,Approve
{
    public DateTime DateOfBirth { get; set; }
    public DateTime EnrollmentDate { get; set; }
    public string Remarks { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

并且,我不能将这些东西放在PERSON中.因为,我要将它用于另一个类,但那些不是人.

然后,我如何实现这一目标......

请给我一个上述情况的例子.

请帮忙.而且,非常感谢你提前.

Pao*_*sco 22

一种可能的解决方案是修改您的层次结构:

public class PersonWithApprove : Person { // TODO: replace with non disgusting name
    public bool Approved { get; set; }
    // etc...
}

public class Student : PersonWithApprove {
}

public class Faculty : PersonWithApprove {
}
Run Code Online (Sandbox Code Playgroud)

或者您可以创建一个界面:

public interface IApprove {
    bool Approved { get; set; }
    // etc
}

public class Student : Person, IApprove {
}
Run Code Online (Sandbox Code Playgroud)

您也可以这样离开类Approve,并使用具有该类型属性的类:

public class Student : Person {
    Approve _approve = new Approve();
    public Approve Approve {
        get { return _approve; }
    }
}
Run Code Online (Sandbox Code Playgroud)


Dmi*_*nko 6

恕我直言,在这里使用接口是一个很好的例子,类似的东西:

  // Interfaces:

  // General person
  public interface IPerson {
    int Id { get; set; }
    string FirstName { get; set; }
    string LastName { get; set; }
    string Type { get; set; }
  }

  // Approvable person
  public interface IApprovable {
    bool Approved { get; set; }
    DateTime ApprovedDate { get; set; }
    int ApprovedUserId { get; set; }
  } 

  // Student is a IPerson + IApprovable
  public interface IStudent: IPerson, IApprovable {
    DateTime DateOfBirth { get; set; }
    DateTime EnrollmentDate { get; set; }
  }

  // So classes will be

  public class Approve: IApprovable {
    ... //TODO: Implement IApprovable interface here
  } 

  public class Faculty: IPerson, IApprovable {
    public DateTime HiredDate { get; set; }

    ... //TODO: Implement IPerson interface here
    ... //TODO: Implement IApprovable interface here
  }

  public class Student: IStudent {
    public string Remarks { get; set; }

    ... //TODO: Implement IStudent interface here
  }
Run Code Online (Sandbox Code Playgroud)


mat*_*dev 5

简答

请考虑使用接口,它允许多重继承,并且可以使用interface关键字声明.

答案很长

从C#中的多个基类继承是非法的.类可以只有1个基类,而它们可以实现任意数量的接口.这有几个原因,但主要归结为多重继承在类层次结构中引入了更多的复杂性.

接口用于声明必须由类实现的一组通用功能(方法和属性).

要修改现有代码以使用接口(而不是多重继承),可以执行以下操作:

public interface IApprove // Defines a set of functionality that a class must implement.
{
    // All these properties must be inherited as public when implemented.
    bool Approved { get; set; } // Property declaration.
    DateTime ApprovedDate { get; set; }
    int ApprovedUserId { get; set; }
}

public class Student : Person, IApprove
{
    public DateTime DateOfBirth { get; set; }
    public DateTime EnrollmentDate { get; set; }
    public string Remarks { get; set; }

    #region IApprove Implementation

    private bool _approved; // Private variable that is accessed through the 'Approved' property of the 'IApprove' interface.
    public bool Approved // Defines 'Approved' inherited from IApprove
    {
        get { return _approved; }
        set { _approved = value; }
    }

    private DateTime _approvedDate;
    public DateTime ApprovedDate // Defines 'ApprovedDate' inherited from IApprove.
    {
        get { return _approvedDate; }
        set { _approvedDate = value; }
    }

    private int _approvedUserId;
    public int IApprove.ApprovedUserId // Alternative syntax to define an interfaces property.
    {
        get { return _approvedUserId; }
        set { _approvedUserId = value; }
    }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

这种方法抽象了IApprove接口的实现,并且像多继承一样,允许用户操作实现IApprove但其具体类型未知(或不相关)的对象.

有关C#中接口用法的更多信息,请参阅:

http://msdn.microsoft.com/en-us/library/87d83y5b.aspx