抽象方法和虚拟方法有什么区别?在哪些情况下,建议使用抽象方法或虚拟方法?哪一种是最好的方法?


当前回答

我通过对以下课程(从其他答案)进行一些改进,使这一点更简单:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestOO
{
    class Program
    {
        static void Main(string[] args)
        {
            BaseClass _base = new BaseClass();
            Console.WriteLine("Calling virtual method directly");
            _base.SayHello();
            Console.WriteLine("Calling single method directly");
            _base.SayGoodbye();

            DerivedClass _derived = new DerivedClass();
            Console.WriteLine("Calling new method from derived class");
            _derived.SayHello();
            Console.WriteLine("Calling overrided method from derived class");
            _derived.SayGoodbye();

            DerivedClass2 _derived2 = new DerivedClass2();
            Console.WriteLine("Calling new method from derived2 class");
            _derived2.SayHello();
            Console.WriteLine("Calling overrided method from derived2 class");
            _derived2.SayGoodbye();
            Console.ReadLine();
        }
    }


    public class BaseClass
    {
        public void SayHello()
        {
            Console.WriteLine("Hello\n");
        }
        public virtual void SayGoodbye()
        {
            Console.WriteLine("Goodbye\n");
        }

        public void HelloGoodbye()
        {
            this.SayHello();
            this.SayGoodbye();
        }
    }


    public abstract class AbstractClass
    {
        public void SayHello()
        {
            Console.WriteLine("Hello\n");
        }


        //public virtual void SayGoodbye()
        //{
        //    Console.WriteLine("Goodbye\n");
        //}
        public abstract void SayGoodbye();
    }


    public class DerivedClass : BaseClass
    {
        public new void SayHello()
        {
            Console.WriteLine("Hi There");
        }

        public override void SayGoodbye()
        {
            Console.WriteLine("See you later");
        }
    }

    public class DerivedClass2 : AbstractClass
    {
        public new void SayHello()
        {
            Console.WriteLine("Hi There");
        }
        // We should use the override keyword with abstract types
        //public new void SayGoodbye()
        //{
        //    Console.WriteLine("See you later2");
        //}
        public override void SayGoodbye()
        {
            Console.WriteLine("See you later");
        }
    }
}

其他回答

虚拟方法:

虚拟意味着我们可以超越它。虚拟函数有一个实现。当我们继承类时可以重写虚拟函数并提供我们自己的逻辑。我们可以在实现子类中的函数(可以说是阴影)。

抽象方法

抽象意味着我们必须重写它。抽象函数没有实现,必须在抽象类中。它只能声明。这迫使派生类提供它的实现。抽象成员是隐式虚拟的。在某些语言中,抽象可以称为纯虚拟。公共抽象类BaseClass{ 受保护的抽象void xAbstractMethod();公共虚拟void xVirtualMethod(){变量x=3+4;}}

答案已经提供了很多次,但关于何时使用每一个的问题是设计时的决定。我认为,尝试将通用方法定义捆绑到不同的接口中,并将它们拉到适当抽象级别的类中是一种很好的做法。当最好定义一个实现一组简洁接口的非抽象类时,将一组通用的抽象和虚拟方法定义转储到一个类中会使该类不可实例化。一如既往,这取决于什么最适合您的应用程序特定需求。

抽象函数不能具有功能。你基本上是说,任何子类都必须给出自己版本的这个方法,但是它太通用了,甚至无法在父类中实现。

一个虚拟函数,基本上就是说,看,这里的功能对于子类来说可能不够好,也可能不够好。因此,如果它足够好,请使用此方法,如果不好,请重写我,并提供您自己的功能。

抽象函数或方法是由类公开的公共“操作名称”,其目的与抽象类一起,主要是在对象设计中针对对象必须实现的结构提供一种形式的约束。

事实上,从其抽象类继承的类必须为该方法提供一个实现,通常编译器在不实现时会引发错误。

使用抽象类和方法非常重要,主要是为了避免在设计类时关注实现细节,从而导致类结构与实现过于相关,从而在它们之间协作的类之间创建依赖关系和耦合。

虚拟函数或方法只是一种模拟类的公共行为的方法,但我们可以在继承链中自由修改它,因为我们认为子类可能需要为该行为实现一些特定的扩展。

它们都代表了面向对象范式中的一种多元化。

我们可以一起使用抽象方法和虚拟函数来支持一个好的继承模型。

我们为解决方案的主要对象设计了一个很好的抽象结构,然后通过定位那些更易于进一步专门化的对象来创建基本实现,我们将这些对象作为虚拟对象,最后我们专门化我们的基本实现,最终“覆盖”继承的虚拟对象。

必须始终重写抽象函数。

因此:

抽象函数-当继承者必须提供自己的实现时虚拟-由继承人决定