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


当前回答

抽象方法是一种必须实现才能生成具体类的方法。声明在抽象类中(任何具有抽象方法的类都必须是抽象类),并且必须在具体类中实现。

虚拟方法是一种可以在派生类中使用重写重写的方法,替换超类中的行为。如果不覆盖,则获得原始行为。如果你这样做,你总会得到新的行为。这与不能重写但可以隐藏原始方法的非虚拟方法相反。这是使用新修改器完成的。

请参见以下示例:

public class BaseClass
{
    public void SayHello()
    {
        Console.WriteLine("Hello");
    }


    public virtual void SayGoodbye()
    {
        Console.WriteLine("Goodbye");
    }

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


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


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

当我实例化DerivedClass并调用SayHello或SayGoodbye时,我会得到“Hi There”和“See you later”。如果我打电话给HelloGoodbye,我会收到“你好”和“再见”。这是因为SayGoodbye是虚拟的,可以被派生类替换。SayHello只是隐藏的,所以当我从基类调用它时,我会得到原始方法。

抽象方法是隐式虚拟的。它们定义了必须存在的行为,更像是一个接口。

其他回答

在这里,我写了一些示例代码,希望这可能是一个非常实际的例子,可以从非常基本的层面上看到接口、抽象类和普通类的行为。如果您想将其用作演示,也可以在github中找到此代码作为项目:https://github.com/usavas/JavaAbstractAndInterfaceDemo

public interface ExampleInterface {

//    public void MethodBodyInInterfaceNotPossible(){
//    }
    void MethodInInterface();

}

public abstract class AbstractClass {
    public abstract void AbstractMethod();

    //    public abstract void AbstractMethodWithBodyNotPossible(){
    //
    //    };

    //Standard Method CAN be declared in AbstractClass
    public void StandardMethod(){
        System.out.println("Standard Method in AbstractClass (super) runs");
    }
}

public class ConcreteClass
    extends AbstractClass
    implements ExampleInterface{

    //Abstract Method HAS TO be IMPLEMENTED in child class. Implemented by ConcreteClass
    @Override
    public void AbstractMethod() {
        System.out.println("AbstractMethod overridden runs");
    }

    //Standard Method CAN be OVERRIDDEN.
    @Override
    public void StandardMethod() {
        super.StandardMethod();
        System.out.println("StandardMethod overridden in ConcreteClass runs");
    }

    public void ConcreteMethod(){
        System.out.println("Concrete method runs");
    }

    //A method in interface HAS TO be IMPLEMENTED in implementer class.
    @Override
    public void MethodInInterface() {
        System.out.println("MethodInInterface Implemented by ConcreteClass runs");

    //    Cannot declare abstract method in a concrete class
    //    public abstract void AbstractMethodDeclarationInConcreteClassNotPossible(){
    //
    //    }
    }
}

抽象函数没有实现,只能在抽象类上声明。这迫使派生类提供实现。

虚拟函数提供默认实现,它可以存在于抽象类或非抽象类上。

例如:

public abstract class myBase
{
    //If you derive from this class you must implement this method. notice we have no method body here either
    public abstract void YouMustImplement();

    //If you derive from this class you can change the behavior but are not required to
    public virtual void YouCanOverride()
    { 
    }
}

public class MyBase
{
   //This will not compile because you cannot have an abstract method in a non-abstract class
    public abstract void YouMustImplement();
}

抽象函数不能有主体,必须由子类重写

虚函数将有一个主体,并且可以或不可以被子类重写

只有抽象类才能有抽象成员。从抽象类继承的非抽象类必须重写其抽象成员。抽象成员是隐式虚拟的。抽象成员不能提供任何实现(抽象在某些语言中称为纯虚拟)。

从面向对象的常规视图:关于抽象方法:当你把一个抽象方法放在父类中时,实际上你在对子类说:嘿,注意你有一个这样的方法签名。如果你想使用它,你应该实现你自己的!关于虚拟函数:当你在父类中放置一个虚拟方法时,你会对派生类说:嘿,这里有一个功能可以为你做些什么。如果这对你有用,就使用它。如果没有,重写它并实现你的代码,甚至你可以在代码中使用我的实现!这是关于通用OO中这两个概念之间不同的一些哲学