如何与QUOT;正常"重写基类中的方法? [英] How to "properly" override a base class method?

查看:175
本文介绍了如何与QUOT;正常"重写基类中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我重写基类的方法,比我这个方法的实现等,我似乎有3个选择。



1)调用base.Method( ),然后提供我的实现。



2)提供我的实现,然后调用base.Method()



3)只需提供我的实现。



最近在使用我已经意识到介绍,因为该库无法按预期实现该方法的是一些错误的库。我不知道这是不好的图书馆,或一些错误的,我理解的一部分。



我举一个例子。

 公共类ViewManager {
公共虚拟无效自定义(){
Pre​​pareBaseView();
}
}

公共类PostViewManager {
公共覆盖无效的自定义(){
base.Customize();
PreparePostView();
}
}


公共类PreViewManager {
公共覆盖无效的自定义(){
PreparePreView();
base.Customize();
}
}


公共类CustomViewManager {
公共覆盖无效的自定义(){
PrepareCustomView();
}
}



在这里我的问题是,一个子类怎么会知道(不考虑看看基类实现)的订单(或选择)正在被父类的预期?
是否有办法在父类可以强制执行三名候补之一,所有的派生类?


解决方案

怎么可能一个子类知道(没有考虑看看基类实现)的订单(或选择)正在被父类的预期?




有没有办法知道这个当你继承和覆盖方法。正确的文件是真的在这里唯一的选择。




有没有在父类可以强制执行三名候补之一,所有推导的方法类?




这里唯一的选择是避免这个问题。相反,让子类覆盖的方法,它可以声明非虚,并呼吁在适当的地方虚方法。例如,如果要强制子类先打电话给你的版本,你可以这样做:

 公共类BaseClass的{
酒店的公共void方法()//非虚
{
//完成所需的工作

//调用虚现在...方法
本。 OnMethod();
}

受保护的虚拟无效OnMethod()
{//什么都不做
}
}

子类就可以覆盖OnMethod,并提供售后服务办法的作品出现这种情况的功能。



这是必需的原因在于,虚拟方法被设计为允许一个子类完全替换父类的实现。这是故意的。如果要避免这种情况,最好是使该方法非虚。


Whenever i override a method of a base class, other than my implementation of this method, i seem to have 3 choices.

1) Call base.Method(), and then provide my implementation.

2) Provide my implementation and then call base.Method()

3) Just provide my implementation.

Recently while using a library i have realized few bugs that were introduced because of not implementing the method as expected by the library. I am not sure if that is bad on part of library, or something wrong in my understanding.

I will take one example.

public class ViewManager {
     public virtual void Customize(){
        PrepareBaseView();
     }
}

public class PostViewManager {
     public override void Customize(){
        base.Customize();
        PreparePostView();
     }
}


public class PreViewManager {
     public override void Customize(){
        PreparePreView();
        base.Customize();
     }
}


public class CustomViewManager {
     public override void Customize(){
        PrepareCustomView();
     }
}

My question here is that how could a child class know (without taking a look at base class implementation) which order (or option) is being expected by the parent class? Is there a way in which parent class could enforce one of the three alternates to all the deriving classes?

解决方案

how could a child class know (without taking a look at base class implementation) which order (or option) is being expected by the parent class?

There is no way to "know" this when you are subclassing and overriding a method. Proper documentation is really the only option here.

Is there a way in which parent class could enforce one of the three alternates to all the deriving classes?

The only option here is to avoid the issue. Instead of allowing the subclass to override the method, it can be declared non-virtual, and call a virtual method in the appropriate place. For example, if you want to enforce that subclasses "call your version first", you could do:

public class BaseClass {
    public void Method() // Non-virtual
    {
          // Do required work

          // Call virtual method now...
          this.OnMethod();
    }

    protected virtual void OnMethod()
    { // Do nothing
    }
 }

The subclasses can then "override" OnMethod, and provide functionality that happens after "method"'s work.

The reason this is required is that virtual methods are designed to allow a subclass to completely replace the implementation of the parent class. This is done on purpose. If you want to prevent this, it's better to make the method non-virtual.

这篇关于如何与QUOT;正常"重写基类中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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