[英]C# Multiple Inheritance
目前我正在研究 C# with ASP.NET MVC 4 with Code First Approach 。 我是 Visual Basic 開發人員,現在我想開始 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 {
}
或者你可以創建一個接口:
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; }
}
}
恕我直言,在這里使用接口是一個很好的例子,就像這樣:
// 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
}
簡答
考慮改用接口,它允許多重繼承並且可以使用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
}
這種方法抽象了IApprove接口的實現,並且像多重繼承一樣,允許用戶對實現IApprove 的對象進行操作,但它們的具體類型是未知的(或不相關的)。
有關 C# 中接口用法的更多信息,請參閱:
您可以使用復合模式
public class Student:Person
{
public Approve App { get; set; }
public DateTime DateOfBirth { get; set; }
public DateTime EnrollmentDate { get; set; }
public string Remarks { get; set; }
}
考慮以下示例,它使用兩個接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
////////
////////
/// Multiple Inheritance With Interfaces
public interface Interface1
{
void func1();
void fun();
}
public interface Interface2
{
void func2();
void fun();
}
public class MyTestBaseClass : Interface1, Interface2
{
void Interface1.func1()
{
Console.WriteLine("From MyInterface1 Function()");
return;
}
void Interface2.func2()
{
Console.WriteLine("From MyInterface2 Function()");
return;
}
void Interface1.fun()
{
Console.WriteLine("fun1()");
}
void Interface2.fun()
{
Console.WriteLine("fun2()");
}
public static void Main()
{
MyTestBaseClass myclass = new MyTestBaseClass();
((Interface1)myclass).func1();
((Interface2)myclass).func2();
}
}
使用裝飾器設計模式的一些基本示例:
public class Class1
{
public void Method1()
{
Console.write($"Class1: Method1, MyInt: {MyInt}");
}
public int MyInt { get; set; }
}
public class Class2
{
public void Method2()
{
Console.write($"Class2: Method2, MyInt: {MyInt}");
}
public int MyInt { get; set; }
}
public class MultipleClass
{
private Class1 class1 = new Class1();
private Class2 class2 = new Class2();
public void Method1()
{
class1.Method1();
}
public void Method2()
{
class2.Method2();
}
private int _myInt;
public int MyInt
{
get { return this._myInt; }
set
{
this._myInt = value;
class1.MyInt = value;
class2.MyInt = value;
}
}
}
演示:
MultipleClass multipleClass = new MultipleClass();
multipleClass.Method1(); //OUTPUT: Class1: Method1, MyInt: 1
multipleClass.Method2(); //OUTPUT: Class2: Method2, MyInt: 1
multipleClass.MyInt = 1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.