防止被继承的方法 [英] Preventing methods from being inherited

查看:124
本文介绍了防止被继承的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类Foo说是具体的,包含30个方法,这是有关它的子类。

I have a base class Foo that is concrete and contains 30 methods which are relevant to its subclasses.

现在我遇到这种情况是只有特定基类,我想创建一个不能被继承的方法,这可能吗?

Now I've come across a situation that is specific only to the base class,and I want to create a method that cannot be inherited, is this possible?

Class Foo 
{
   /* ... inheritable methods ... */

   /* non-inheritable method */
   public bool FooSpecificMethod()
   { 
      return true;
   } 
}

Class Bar : Foo
{
    /* Bar specific methods */
}

var bar = new Bar();
bar.FooSpecificMethod(); /* is there any way to get this to throw compiler error */

修改

我不知道如果我本来清楚的。

I'm not sure if I was clear originally.

我明白继承的原则,我理解了里氏替换原则。在这种情况下,有一个例外,随着非继承案只涉及,所以我不想创建一个uninheritedFoo子类。

I do understand the principles of inheritance, and I understand the Liskov substitution principle. In this case there is a single exception that ONLY deals with the 'un-inherited' case, and so I did not want to create an 'uninheritedFoo' subclass.

我是问,如果它在技术上是可能创造这样一种情况
foo.FooSpecificMethod()是一个有效的,可公开访问的方法,但subclassoffoo.FooSpecificMethod( )抛出一个编译器错误。

I was asking if it is technically possible to create a situation where foo.FooSpecificMethod() is a valid and publicly accessible method, but subclassoffoo.FooSpecificMethod() throws a compiler error.

基本上我想在未密封类密封的方法。

Essentially I want a sealed method on an unsealed class.

推荐答案

我会重新考虑这个需求。

I would rethink the need for this.

如果您使用的是继承,你是暗示吧是一个富。如果酒吧始终是一个富,即对富的工作方法也应该在酒吧工作。

If you are using inheritance, you are suggesting that "Bar" IS A "Foo". If "Bar" is always a "Foo", methods that work on "Foo" should also work on "Bar".

如果不是这种情况下,我将返工这是一个私有方法。公开,酒吧应该永远是一个Foo。

If this isn't the case, I would rework this as a private method. Publically, Bar should always be a Foo.


只是借此更进一步 -

Just to take this one step further -

如果你能做到这一点,事情会变得非常复杂。你可以有情况下:

If you could do this, things would get very complicated. You could have situations where:

Foo myBar = new Bar(); // This is legal
myBar.FooSpecificMethod(); // What should this do?  
                           // It's declared a Foo, but is acutally a Bar



实际上,你可以强制此使用反射行为,虽然。我认为这是一个坏主意,但FooSpecificMethod()可以检查这个类型,如果不是typeof运算(富),抛出一个异常。这将是非常混乱,而且有一个非常难闻的气味。

You can actually force this behavior using reflection, though. I think it's a bad idea, but FooSpecificMethod() could check the type of this, and if it isn't typeof(Foo), throw an exception. This would be very confusing, and have a very bad smell.


在响应编辑到问题的编辑:

Edit in response to question's edit:

有没有办法让编译器执行你的要求。如果你真的想强制编译器检查,并防止这种情况,你真的应该考虑美孚密封类。你可以使用其他扩展方法不是在这种情况下继承。

There is no way for the compiler to enforce what you are asking. If you really want to force the compiler to check this, and prevent this, you really should consider making Foo a sealed class. You could use other extension methods than subclassing in this case.

例如,你可能要考虑使用的事件或委托延长行为,而不允许对象进行子类。

For example, you might want to consider using events or delegates to extend the behavior instead of allowing the object to be subclasses.

试图做你办成什么基本上是试图阻止继承的主要目标。

Trying to do what you are accomplishing is basically trying to prevent the main goals of inheritance.

这篇关于防止被继承的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆