2012-02-17 89 views
7

我可以为继承我的基类的类设置规则。例如。 Person : BaseClass,我想Person执行iSomeKindOfInterface,如果Person没有实现接口,它是不允许从BaseClass继承。继承规则类

我知道这是posibble在通用基础类,你可以做以下

public BaseClass<T> 
    where T : iSomeKinfOfInterface 

回答

10

您可以实现该接口中的基础类,并迫使继承类提供实现:

public interface ISomeInterface 
{ 
    void DoSomething(); 
} 

public abstract class BaseClass : ISomeInterface 
{ 
    public abstract void DoSomething(); 
} 

public class Person : BaseClass 
{ 
    public override void DoSomething() 
    { 
     ... 
    } 
} 
2

声明您的课程为

abstract BaseClass : ISomeKinfOfInterface 
+0

不能使用抽象类。我正在谈论的类是winforms用户控件。当我使用抽象基类时,我不能在Visual Studio设计器中少用几个用户控件... – Captain0 2012-02-17 10:15:42

+0

使用泛型基类时,我在设计器中得到的错误与此相同http://stackoverflow.com/questions/3933218 /当使用抽象类时,表单设计器会在通用抽象用户控件上出现错误 – Captain0 2012-02-17 10:24:00