简体   繁体   English

抽象方法声明 - 虚拟?

[英]Abstract method declaration - virtual?

On MSDN I have found that it is a error to use "virtual" modifier in an abstract method declaration. 在MSDN上,我发现在抽象方法声明中使用“虚拟”修饰符是错误的。 One of my colleagues, who should be pretty experienced developer, though uses this in his code: 我的一位同事应该是非常有经验的开发人员,但在他的代码中使用了这个:

public abstract class BusinessObject   
{
    public virtual void Render(){}
    public virtual void Update(){}
}

Also it correct or not? 它也正确与否?

It can make sense if the abstract class is providing an optional point where inherited classes can change the behaviour. 如果抽象类提供了一个可选的点,继承的类可以改变行为,那么这是有意义的。

So this way the inherited classes will not be forced to implement it but they can if they need to. 因此,这种方式不会强制继承的类实现它,但如果需要,它们可以。

Usually this method is called by the abstract class: 通常这个方法由抽象类调用:

public AddFoo(Foo f)
{
   // ...
   OnAddedFoo(f);
}

Here it makes sense to have OnAddedFoo as virtual and not abstract . OnAddedFoo作为virtual而非abstract是有意义的。

I guess the MSDN means you can't use the virtual and abstract modifier on a method at the same time. 我想MSDN意味着你不能同时在方法上使用virtualabstract修饰符。

You can either do 你可以这样做

public virtual void Render() {}

which means that an inheriting class can override this method. 这意味着继承类可以覆盖此方法。

Or you can do 或者你可以做到

public abstract void Render();

which means that an inheriting class must override this method. 这意味着继承类必须覆盖此方法。

Those aren't abstract methods, they're empty default ones. 那些不是抽象的方法,它们是空的默认方法。 The difference is you don't HAVE to override them (if you don't, nothing will happen). 区别在于你没有必须覆盖它们(如果你不这样做,什么都不会发生)。

It might help your understanding to see them formatted like: 它可能有助于您理解它们的格式如下:

public abstract class BusinessObject   
{
    public virtual void Render()
    {

    }

    public virtual void Update()
    {

    }
}

You probably refer to this sentence 你可能会参考这句话

You cannot use the virtual modifier with the static, abstract, private or override modifiers. 您不能将virtual修饰符与static,abstract,private或override修饰符一起使用。

This mean that you can not use this in method declaration a method can't be at the same time virtual and abstract. 这意味着你不能在方法声明中使用它,一个方法不能同时虚拟和抽象。

So the usage that you have presented is fine, because the class is abstract this mean that you can have there some abstract methods (with out implementation that have to be implemented by child class) and virtual method (with implementation that can be override in child class). 所以你提出的用法很好,因为这个类是抽象的,这意味着你可以有一些抽象的方法(没有必须由子类实现的实现)和虚方法(可以在子节点中覆盖的实现)类)。

Follwing code means that you must ovveride this method in inherited class and you can't put some logic in abstract method body: Follwing代码意味着您必须在继承的类中保留此方法,并且您不能在抽象方法体中放置一些逻辑:

public abstract void Render();

But if you declare virtual method you can put some logic inside it and can ovveride 但是如果你声明虚方法,你可以在其中加入一些逻辑并且可以使用

public virtual void Render()
{
     // you can put some default logic here       
}

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

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