简体   繁体   English

隐藏的继承c#

[英]Hidden Inherritance c#

In c#, is it possible to prevent the programmer to inherit a parent class, but allow child classes to be inherited from (perhaps just during the writing of the code, not during runtime)? 在c#中,是否可以防止程序员继承父类,但允许子类继承(也许只是在编写代码时,而不是在运行时)?

For example: 例如:

//Class which should only allow 'RangedAbility' and 'MeleeAbility' to inherit. 
//No other classes are to be allowed to inherit from Ability
public abstract class Ability{ 
    public virtual void foo(){}
}

public class RangedAbility : Ability{
     //an empty class
}
public class MeleeAbility : Ability{
     //an empty class
}

public class myAbility : MeleeAbility{
     public override void foo(){ /*some code*/}
} 
//attempting to inherit from `Ability` instead of `MeleeAbility` or instead of 'RangedAbility' 
//should result in programmer facing an error in IDE such as Visual Studio.

Using "sealed" on Ability will not work (it is abstract, as well as RangedAbility , MeleeAbility need to inherit from it). Ability上使用“密封”将不起作用(它是抽象的, RangedAbilityMeleeAbility需要从中继承)。 Reflection approach to "simmulate" inheritance by those two child classes would be very tedious and had to be done in both MeleeAbility and RangedAbility . 这两个子类“模仿”继承的反射方法将非常繁琐,并且必须在MeleeAbilityRangedAbility中都RangedAbility Abandoning the use of Ability completely, and forcing RangedAbility and MeleeAbility to implement an interface doesn't seem like a good option either, because they share methods that would be better-off written once through a "hidden" parent class. 完全放弃使用Ability ,而强制RangedAbilityMeleeAbility来实现接口似乎也不是一个好选择,因为它们共享的方法最好通过“隐藏的”父类编写一次。

The reason for doing such code is that I need to determine at runtime what kind of behavior to expect from 'myAbility' type. 进行此类代码的原因是,我需要在运行时确定对“ myAbility”类型的预期行为。 It is done through IsSubclassOf(typeof(MeleeAbility)) or IsSubclassOf(typeof(RangedAbility)) , which allows to determine expected behavior without needing an actual instance of myAbility . 它通过IsSubclassOf(typeof(MeleeAbility))IsSubclassOf(typeof(RangedAbility))无需确定myAbility的实际实例即可确定预期的行为。

public abstract class Ability
{
  internal Ability()
  {
  }

  public virtual void foo(){}
}

Because the only constructor for Ability is internal only a derived class from within the same assembly can be constructed (constructors that don't explicitly build on a base constructor are implicitly : base() ). 因为Ability的唯一构造函数是internal所以只能构造同一程序集中的派生类(未在基本构造函数上显式构建的构造函数是隐式的: base() )。 Any attempt for a class in another assembly to derive directly from Ability will not have a valid constructor. 任何试图使另一个程序集中的类直接从Ability派生的尝试都将没有有效的构造函数。

(On a related note, you might find an internal abstract property that your own classes override to return different values as neater method of determining which you have than doing explicit IsSubClassOf or even is tests. Or better again, providing whatever functionality turns on such a test through an internal abstract method). (在相关说明中,您可能会发现一个内部抽象属性,您自己的类将其覆盖以返回不同的值,这比确定显式的IsSubClassOf甚至is测试更整洁地确定了您拥有的IsSubClassOf 。或者,再次提供更好的功能来提供这样的功能,通过内部抽象方法进行测试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM