比较:接口方法 vs 虚方法 vs 抽象方法 [英] Comparison : interface methods vs virtual methods vs abstract methods

查看:27
本文介绍了比较:接口方法 vs 虚方法 vs 抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每种方法的优缺点是什么?

What are the advantages and disadvantages of each of these?

  • 接口方法
  • 虚拟方法
  • 抽象方法

什么时候应该选择什么?做出这一决定时应牢记哪些要点?

When one should choose what? What are the points one should keep in mind when making this decision?

推荐答案

虚拟和抽象几乎相同.虚拟方法在基类中有一个可以选择性地被覆盖的实现,而抽象方法没有并且必须在子类中被覆盖.否则它们是相同的.在它们之间进行选择取决于情况.如果您有基本实现,则使用虚拟.如果您不这样做,并且您需要每个后代为自己实现它,您就选择抽象.

Virtual and abstract are almost the same. A virtual method has an implementation in the base class that can optionally be overridden, while an abstract method hasn't and must be overridden in a child class. Otherwise they are the same. Choosing between them depends on the situation. If you got a base implementation, you use virtual. If you don't, and you need every descendant to implement it for itself, you choose abstract.

接口方法是在类实现的接口中声明的方法的实现.这与其他两个完全无关.我认为一个方法既可以是虚拟的,也可以是接口的.接口的优点是您可以声明一个可以由两个完全不同的类实现的接口 (duh).这样,您就可以在两个不同的类上运行相同的代码,只要您要调用的方法在它们共享的接口中声明即可.

Interface methods are implementations of a method that is declared in an interface that the class implements. This is quite unrelated to the other two. I think a method can be both virtual and interface. The advantage of interfaces is that you declare one interface (duh) that can be implemented by two totally different classes. That way, you can run the same code on two different classes, as long as the methods you'd like to call are declared in an interface they share.

这篇关于比较:接口方法 vs 虚方法 vs 抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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