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


当前回答

抽象函数(方法):

● 抽象方法是用关键字abstract声明的方法。

● 它没有身体。

● 它应该由派生类实现。

● 如果方法是抽象的,那么类应该是抽象的。

虚拟函数(方法):

● 虚方法是用关键字virtual声明的方法,它可以通过使用override关键字由派生类方法重写。

● 是否重写它取决于派生类。

其他回答

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

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");
        }
    }
}

图.-命题的传统三重分类。

在道义逻辑(义务和许可的研究)中,每个命题都是强制性的(“必须”运算符)、可选的(“可能”运算符)或不允许的(“不得”运算符),任何命题都不属于这三类中的一类。

此外,允许(“可能”运算符)命题是强制性或可选的命题,不允许(“未必”运算符)的命题是不允许或可选的,非可选(“必须或不得”运算符)是强制性或不允许的命题。

特别是,强制性命题是允许的,而不允许的命题是不允许的。

将这些运算符应用于命题“方法被覆盖”会产生以下命题:

抽象(纯)/具体方法:该方法必须被重写/不能被重写;virtual/real(final)方法:该方法可以被重写/不能被重写。

特别是,抽象方法是虚拟的,而真实方法是具体的。

抽象函数:

它只能在抽象类中声明。它只包含方法声明不是抽象类中的实现。它必须在派生类中重写。

虚拟功能:

它可以在抽象类和非抽象类中声明。它包含方法实现。它可能被覆盖。

我在一些地方看到抽象方法的定义如下**

“必须在子类中实现抽象方法”

**我觉得是这样。

如果子类也是抽象的,则不必在子类中实现抽象方法。。

1) 抽象方法不能是私有方法。2) 抽象方法不能在同一抽象类中实现。

我会说。。如果我们要实现一个抽象类,您必须重写基础抽象类中的抽象方法。因为使用重写关键字实现抽象方法。类似于虚拟方法。

虚拟方法不必在继承类中实现。

                                 ----------CODE--------------

public abstract class BaseClass
{
    public int MyProperty { get; set; }
    protected abstract void MyAbstractMethod();

    public virtual void MyVirtualMethod()
    {
        var x = 3 + 4;
    }

}
public abstract class myClassA : BaseClass
{
    public int MyProperty { get; set; }
    //not necessary to implement an abstract method if the child class is also abstract.

    protected override void MyAbstractMethod()
    {
        throw new NotImplementedException();
    }
}
public class myClassB : BaseClass
{
    public int MyProperty { get; set; }
    //You must have to implement the abstract method since this class is not an abstract class.

    protected override void MyAbstractMethod()
    {
        throw new NotImplementedException();
    }
}

当您希望继承者根据需要扩展功能时,基本上可以使用虚拟方法。

当您希望继承者实现功能时,可以使用抽象方法(在这种情况下,他们别无选择)