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


当前回答

抽象函数:

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

虚拟功能:

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

其他回答

C#中没有调用虚拟类的内容。

对于函数

抽象函数仅具有签名,驱动器类应使用功能重写。虚拟功能将保留驱动器类根据需要可以覆盖或不覆盖的部分功能

你可以决定你的要求。

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

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

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

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

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

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

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

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

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

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

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

**我觉得是这样。

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

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

必须始终重写抽象函数。

因此:

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