【问题标题】:C# Multiple InheritanceC# 多重继承
【发布时间】:2013-08-28 14:13:27
【问题描述】:

目前我正在使用 Code First Approach 使用 ASP.NET MVC 4 研究 C#。我是 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; }
}

而且,想用like:

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

而且,我不能把这些东西放在 PERSON 里面。因为,我必须将它用于另一个类,但那些不是人。

那么,我该如何实现呢……

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

请帮忙。并且,非常感谢您。

【问题讨论】:

  • 在C#中,可以继承一个类,实现多个接口,必须把类放在首位。
  • 或者在Student类中使用Approve作为属性

标签: c# c#-4.0 inheritance multiple-inheritance


【解决方案1】:

你可以使用复合模式

    public class Student:Person
    {
        public Approve App { get; set; }
        public DateTime DateOfBirth { get; set; }
        public DateTime EnrollmentDate { get; set; }
        public string Remarks { get; set; }
    }

【讨论】:

    【解决方案2】:

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

    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; }
        }
    }
    

    【讨论】:

      【解决方案3】:

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

        // 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
        }
      

      【讨论】:

        【解决方案4】:

        简答

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

        长答案

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

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

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

        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# 中接口使用的更多信息,请参阅:

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

        【讨论】:

          【解决方案5】:

          考虑下面的例子,它使用了两个接口:

          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();
              }
          
          }
          

          【讨论】:

            【解决方案6】:

            使用 Decorator 设计模式的一些基本示例:

            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;
            

            【讨论】:

              猜你喜欢
              • 2017-03-31
              • 2012-05-14
              • 2011-01-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-05-10
              相关资源
              最近更新 更多