java中所有函数都是虚函数_关于Java:虚拟函数与纯虚函数之间的区别是什么?...

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:

C++ Virtual/Pure Virtual Explained

虚函数和纯虚函数有什么区别?

CPP中的纯虚函数与Java中的抽象方法是一样的。

stackoverflow.com/questions/1306778/…副本

可以在派生类中重写虚函数。纯虚函数必须在派生类中重写。具体来说,除非至少有一个派生类重写了该虚函数,否则不能用纯虚函数实例化类。

维基百科:

A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class that is not abstract. Classes containing pure virtual methods are termed"abstract;" they cannot be instantiated directly, and a subclass of an abstract class can only be instantiated directly if all inherited pure virtual methods have been implemented by that class or a parent class. Pure virtual methods typically have a declaration (signature) and no definition (implementation).

那么,在Java术语中,这是否意味着一个虚拟函数是简单的"(实例)方法",一个纯粹的虚拟是"抽象方法"?

@是的,但是需要在派生类中实现该方法。这是"纯虚拟"和"虚拟"的区别

是的,Java中的所有方法都是虚拟的(除了那些标记为EDCOX1,0,显然),所以在讨论Java时,这个术语有点多余。然而,在C++和C语言等语言中,它不是冗余的,默认情况下方法不是虚拟的。

纯虚函数是一个必须由派生类重新定义的虚函数,因此通常在基类中没有实现(尽管可以提供它,但派生类通过作用域解析运算符调用它;感谢@mark ransom和@toolbox指出它)。

如果一个类有纯虚方法,它就不能被实例化,因此从抽象类(即具有纯虚方法的类)派生的任何类都必须定义这种方法才能被实例化。

例子:

#include

using std::cout;

using std::endl;

class BaseClass

{

public:

// A"normal" virtual function which provides a default implementation

virtual void OnlyVirtual()

{

cout<

}

// A"normal" pure virtual function: must be redefined, no default

// implementation

virtual void PureVirtual()=0;

// A pure virtual function that provides a default implementation that can

// be called only explicitly via scope-resolution operator

virtual void PureVirtualWImpl()=0;

};

void BaseClass::PureVirtualWImpl()

{

cout<

}

class Derived0 : public BaseClass

{

public:

// Define the pure virtual function

virtual void PureVirtual()

{

cout<

}

// notice that, even if there's an implementation of PureVirtualWImpl in

// BaseClass, since it's marked as pure, Derived0 cannot still be

// instantiated

};

class Derived1 : public Derived0

{

public:

// PureVirtual is already defined by the parent class Derived0

// I must define also PureVirtualWImpl if I want to instantiate this class

virtual void PureVirtualWImpl()

{

cout<

}

};

class Derived2 : public Derived1

{

public:

// Obviously I can redefine the"normal" virtual function

virtual void OnlyVirtual()

{

cout<

}

// Redefine PureVirtual

virtual void PureVirtual()

{

cout<

}

// Just for fun I can redefine PureVirtualWImpl to call the base normally-

// unaccessible implementation

virtual void PureVirtualWImpl()

{

BaseClass::PureVirtualWImpl();

}

};

void TestClass(BaseClass & C)

{

C.OnlyVirtual();

C.PureVirtual();

C.PureVirtualWImpl();

}

int main()

{

// BaseClass b; //

// Derived0 d0; //

Derived1 d1;

Derived2 d2;

TestClass(d1);

TestClass(d2);

}

输出:

BaseClass::OnlyVirtual

Derived0::PureVirtual

Derived1::PureVirtualWImpl

Derived2::OnlyVirtual

Derived2::PureVirtual

BaseClass::PureVirtualWImpl

实际上,可以在基类中提供纯虚拟函数的实现。派生类被强制重写它,但它仍然可以使用范围运算符调用。

这不一定是真的。如果需要,您可以为纯虚拟函数提供实现,这在实践中并不常见。如果您希望在派生版本中使用实现细节,但仍然希望强制所有派生类提供实现,那么这种情况可能很有用。

@toolbox@markransom如果永远都不使用纯虚拟函数的基本实现,那么为什么还要使用它呢?

@雅各布:以东十一〔一〕号

@马克·兰索姆:修好了,谢谢。

@蓝调啊是的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值