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

查看:172
本文介绍了对比:接口方法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.

接口中的方法是在接口中声明的方法实现,类实现。这是相当无关的其他两个。我觉得一个方法既可以是虚拟和接口。的接口的优点是,声明一个接口(杜)可以由两个完全不同的类来实现。这样,你可以在两个不同的类中运行相同的代码,只要你的方法要调用它们共用一个接口中声明。

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天全站免登陆