虚拟的实际用途是什么?只是为了实现运行时绑定或它意味着更多 [英] What is practical use of virtual? Just for achieving run time binding or it means more than it

查看:61
本文介绍了虚拟的实际用途是什么?只是为了实现运行时绑定或它意味着更多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行时绑定的用途是什么?我的意思不是它的定义,它的实用优势



我尝试过:



what is use of runtime binding? i mean not it's definition, it's practical advantage

What I have tried:

public class Base
{
  public virtual void SomeMethod()
  {
  }
}

public class Derived : Base
{
  public override void SomeMethod()
  {
  }
}

...

Base d = new Derived();
d.SomeMethod();



它将调用derive类方法。但需要什么呢。如果我可以通过创建派生类实例来做到这一点。并声明基类方法是密封的。



i可以像这样编码




it will call derive class method. but what is need of it. if i can do it by creating derivd class instance. and declaring base class method as sealed.

i can code like this

public class Base
{
  public virtual void SomeMethod()
  {
  }
}

public class Derived : Base
{
  public override void SomeMethod()
  {
  }
}

...

Derved d = new Derived();
d.SomeMethod();



它将调用基类以及派生类方法。

如果我将其声明为密封然后就可以了...




it will called base class as well as derived class methods.
if i declared it as sealed then it will be ok...

public class Base
{
  public virtual void SomeMethod()
  {
  }
}

public class Derived : Base
{
  public override void SomeMethod()
  {
  }
}

...

Derived d = new Derived();
d.SomeMethod();  //it will called derived method

base b=new base();
b.somemethod(); //it will call base method









那么虚拟需要什么? for runtimebinding?如果是,那么什么是rutime绑定的使用或运行时绑定的实际优势是什么?





then what is need of virtual ? for runtimebinding ? if yes then what is use of rutime binding or what is practical advantage of run time binding?

推荐答案

Quote:

我可以通过创建派生类实例来实现。并将基类方法声明为密封。

i can do it by creating derivd class instance. and declaring base class method as sealed.



如果这样做,则无法创建派生类 - 密封类是一个特别是 不能 用作基础,它不能继承。


If you do that, you cannot create a derived class - a sealed class is one that specifically cannot be used as a base, it cannot be inherited from.

Quote:

它将调用基类以及派生类方法。

it will called base class as well as derived class methods.



不,它不会:因为你已经重写了基类方法,所以将调用最新方法。

如果你这样做:


No, it won't: Because you have overridden the base class method, it will call the "latest" method.
And if you do this:

Derived d = new Derived();
d.SomeMethod();
Base b = new Base();
b.SomeMethod();
b = d;
b.SomeMethod();

它将调用Derived方法,然后调用Base方法,然后调用Derived方法。



virtual 关键字告诉编译器派生类 可以 覆盖该方法。如果省略它,则不能在派生类方法定义中使用覆盖

It will call the Derived method, then the Base method, then the Derived method.

The virtual keyword tells the compiler that a derived class can override the method. If you omit it, you can't use override in the derived class method definition.


它的最佳用例是抽象 - 您不需要知道派生类是什么来获得正确的方法。我喜欢使用的一个典型示例是Vehicle基类,它带有一个抽象(它只是在基类中没有定义的虚拟)的int GetWheels()。源自车辆的自行车有2个轮子。一辆汽车有4辆,一辆面包车有6辆。轮胎店在车上预订,需要知道有多少轮胎。它可以在车辆上调用GetWheels,并且知道在不知道它是什么类型的车辆的情况下需要多少轮胎。
Its best use case is for abstraction - you don't need to know what the derived class is to get the correct method. A classic example I like to use is that of a Vehicle base class, with an abstract (which is just virtual that is not defined in the base class) of int GetWheels(). A bike, derived from vehicle, has 2 wheels. A car has 4, and a van has 6. The tyre shop books in a vehicle, and needs to know how many tyres. It can just call GetWheels on the vehicle, and know how many tyres will be needed without knowing, as yet, what sort of vehicle it is.


这篇关于虚拟的实际用途是什么?只是为了实现运行时绑定或它意味着更多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆