将方法从抽象更改为基础中的虚拟方法是对子类进行重大更改 [英] Does changing a method from abstract to virtual in a base a breaking change for child classes

查看:72
本文介绍了将方法从抽象更改为基础中的虚拟方法是对子类进行重大更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类,它实现了虚拟和抽象方法的混合。由于存在默认实现,因此创建为抽象的方法之一应该是虚拟的。我的问题是,因为在任何切除的子类中,所有方法都已准备好覆盖,将方法从抽象更改为虚拟是一个重大改变吗?见下文。



会改变这个。

I have a abstract class that implements a mix of virtual and abstract methods. One of the methods that was created as abstract really should be virtual as there is a default implementation. My question is since the method is all ready overwritten in any excising child classes would changing the method from abstract to virtual be a breaking change? See below.

Would changing this.

public abstract class MyBaseClass
{
public MyBaseClass();

public abstract void DoTask();
}





到此



To this

public abstract class MyBaseClass
{
public MyBaseClass();

public virtual void DoTask()
{
//Do defauld task or override me.
}
}



会影响所有从MyBaseClass继承的子类吗?



我尝试了什么:



我尝试用最基本的形式测试这个,但我担心的是一个更复杂的场景下面我没想到?




affect any child classes all ready inheriting from MyBaseClass?

What I have tried:

I tried testing this in it's most basic form, but my concern is a scenario more complex then below that I'm not thinking of?

public abstract class Class1
    {
        public string s;
        public Class1() { }

        public virtual void DoTask();
    }

public abstract class Class1
    {
        public string s;
        public Class1() { }

        public virtual void DoTask()
        {
            s = "in class one";
        }
    }
    public class Class2: Class1
    {
        public Class2()
        {

        }
        public override void DoTask()
        {
            s = "in class two";
        }
    }

推荐答案

没有。从虚拟变为抽象将是一个重大变化,因为它增加了对所有派生类实现该方法的要求。如果他们不这样做,他们将不再编译。

但是从抽象变为虚拟正在降低要求你可以实现这一点,但你不必 - 这不是一个突破性的变化。
No. Changing from Virtual to Abstract would be a breaking change, as it adds a requirement on all derived classes to implement the method. If they don't, they would no longer compile.
But changing from Abstract to Virtual is lowering the requirements to "you may implement this, but you don't have to" - which isn't a breaking change.


这篇关于将方法从抽象更改为基础中的虚拟方法是对子类进行重大更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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