当我们需要实现抽象类和接口时

发布于 2024-09-25 03:23:29 字数 337 浏览 5 评论 0原文

可能的重复:
抽象类与接口
抽象类和接口类?

大家好, 我对抽象类和接口的使用有点困惑, 当我们需要实现抽象类和接口时。

谢谢 维詹德拉·辛格

Possible Duplicates:
Abstract classes vs Interfaces
Abstract class and Interface class?

Hi All,
I have little bit confusion about the use of the abstract class and Interface,
when we need to Implement abstract class and when Interface.

Thanks
Vijendra Singh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缺⑴份安定 2024-10-02 03:23:29

一般来说:

接口应该用在任何情况下,重要的是类做什么,而不一定是它是什么。例如,一个可以创建自身副本的类还可以做许多其他事情,但是当您只关心能够复制对象时,您只关心该对象实现了 ICloneable。此外,当一组功能的实现不共享时,接口也很有用。例如,输出计算结果的能力可以是文件、控制台或网络的形式。这三个实现完全不同,但对于需要 IOutputWriter 的类来说,它们看起来都是一样的。

摘要通常用于共享代码。与接口不同,抽象类可以指定其子级可以使用的方法逻辑。 BitmapImagePrinter 专门用于位图文件类型,但它需要与 JpegImagePrinter 相同的逻辑来实际访问打印机;因此,该逻辑可以放入 AbstractImagePrinter 中。当类是什么比它做什么更重要时,抽象也很有用。 CheckingAccount 和 SavingsAccount 都是 BankAccount,尽管它们的行为不同。

还有一些其他特殊情况,您必须使用其中之一,但总的来说,这是主要区别。

In general:

Interfaces should be used in any situation where what's important is what the class does, not necessarily what it is. A class that can, for instance, create a copy of itself can do many other things besides, but when you only care about being able to copy the object, you only care that the object implements ICloneable. Also, interfaces are useful when the implementation of a set of functionality is not shared; the ability to output the results of a calculation, for instance, may be in the form of a file, or the console, or over the network. These three implementations are totally different, but they can all look the same to a class that needs an IOutputWriter.

Abstracts are generally used to share code. An abstract class, unlike an interface, can specify method logic that its children can use. A BitmapImagePrinter works specifically with a Bitmap file type, but it needs the same logic as a JpegImagePrinter to actually access the printer; so, that logic can go in AbstractImagePrinter. Abstracts are also useful when what a class is is more important than what it does. CheckingAccount and SavingsAccount are both BankAccounts, even though they behave differently.

There are some other special cases where you MUST use one or the other, but on the whole, that's the major difference.

香橙ぽ 2024-10-02 03:23:29

使用接口,您可以声明行为而不提供实现。这是用作方法参数的最佳选择,因为您依赖于行为,而不是给定的实现(即使是部分抽象类)。

抽象类最好用作基类,提供一组通用功能,供许多实现接口的类使用。我建议将此基抽象类视为内部实现助手。

With an interface you declare a behavior without providing an implementation. This is the best thing to use as a parameter for methods, as you rely on behavior, not on a given implementation (even partial with an abstract class).

Abstract classes are best used as a base class providing a set of common functionality used by many classes implementing an interface for example. I would advise to treat this base abstract class as an internal implementation helper.

流绪微梦 2024-10-02 03:23:29

这已经多次讨论过,请参阅-

当您希望所有类继承某些默认行为时,请实现抽象类,因为它比使用抽象类更有意义。

当您只想为某些类提供某些特定功能并且不需要继承它时,请实现接口。

This has been already discussed multiple times refer this-

Implement Abstract Classes when you want some default behaviour to be inherited by all the classes as it makes more sense then to use abstract classes.

Implement Interfaces when you want to have some specific functionality for only some your classes and dont need it to be inherited.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文