何时以及为何使用抽象类/方法?

发布于 2024-09-12 07:40:56 字数 98 浏览 10 评论 0原文

我有一些关于抽象类/方法的基本问题。我知道抽象类的基本用途是为未来的类创建模板。但它们还有其他用途吗?什么时候你应该更喜欢它们而不是接口?什么时候不应该?另外,抽象方法什么时候有用?

I have some basic questions about abstract classes/methods. I know the basic use of abstract classes is to create templates for future classes. But are there any more uses for them? When should you prefer them over interfaces and when not? Also, when are abstract methods useful?

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

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

发布评论

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

评论(5

爱*していゐ 2024-09-19 07:40:56

我知道了抽象类的基本用法
是为未来创建模板
类。但还有其他用途吗
其中?

您不仅可以为子类定义模板,而且抽象类还提供了额外的好处,让您可以定义子类稍后可以使用的功能。

在 Java 8 之前,您无法在接口中提供默认方法实现。

什么时候你应该更喜欢它们而不是
接口什么时候没有?

如果您想向子级提供实现细节,但又不想允许直接实例化类的实例(这允许您部分定义类),则抽象类非常适合。

如果您想简单地定义对象遵循的契约,那么请使用接口。

抽象方法什么时候有用?

抽象方法的用处与在接口中定义方法的用处相同。这是抽象类的设计者说“我的任何孩子都必须实现这个方法”的一种方式。

I know the basic use of abstract classes
is to create templates for future
classes. But are there any more uses
of them?

Not only can you define a template for children, but Abstract Classes offer the added benefit of letting you define the functionality that your child classes can utilize later.

You could not provide a default method implementation in an Interface prior to Java 8.

When should you prefer them over
interfaces and when not?

Abstract Classes are a good fit if you want to provide implementation details to your children but don't want to allow an instance of your class to be directly instantiated (which allows you to partially define a class).

If you want to simply define a contract for Objects to follow, then use an Interface.

Also when are abstract methods useful?

Abstract methods are useful in the same way that defining methods in an interface is useful. It's a way for the designer of the Abstract class to say "any child of mine MUST implement this method".

吻泪 2024-09-19 07:40:56

阅读以下文章
http:// mycodelines.wordpress.com/2009/09/01/in-which-scenario-we-use-abstract-classes-and-interfaces/

抽象类

-->当您有需求时
你的基类应该提供默认值
某些方法的实施
而其他方法应该开放
被子类覆盖
使用抽象类。

例如,再次举个例子
车辆类别以上。如果我们想要全部
从 Vehicle 派生到的类
在 a 中实现 Drive() 方法
固定方式,而其他方法
可以被子类覆盖。在
这样的场景我们实现
作为抽象类的车辆类
实施 Drive while
保留其他方法/属性
尽可能抽象,所以它们可以是
被子类覆盖。

-->抽象类的目的是
提供一个通用的定义
多个派生的基类
班级可以共享。

例如,类库可以定义
一个抽象类,用作
它的许多函数的参数和
要求程序员使用该库
提供自己的实现
通过创建派生类来创建该类。

使用抽象类

创建类库时
将被广泛分布或
重复使用——尤其是对客户来说,使用
抽象类优先于
界面;因为,它简化了
版本控制。这是使用的做法
由微软团队开发
基类库。 (COM 是
围绕接口设计。)使用
抽象类来定义公共基础
类型族的类。使用
提供默认值的抽象类
行为。仅子类化基类
在该类的层次结构中
逻辑上属于。

read the following article
http://mycodelines.wordpress.com/2009/09/01/in-which-scenario-we-use-abstract-classes-and-interfaces/

Abstract Classes

–> When you have a requirement where
your base class should provide default
implementation of certain methods
whereas other methods should be open
to being overridden by child classes
use abstract classes.

For e.g. again take the example of the
Vehicle class above. If we want all
classes deriving from Vehicle to
implement the Drive() method in a
fixed way whereas the other methods
can be overridden by child classes. In
such a scenario we implement the
Vehicle class as an abstract class
with an implementation of Drive while
leave the other methods / properties
as abstract so they could be
overridden by child classes.

–> The purpose of an abstract class is
to provide a common definition of a
base class that multiple derived
classes can share.

For example a class library may define
an abstract class that is used as a
parameter to many of its functions and
require programmers using that library
to provide their own implementation of
the class by creating a derived class.

Use an abstract class

When creating a class library which
will be widely distributed or
reused—especially to clients, use an
abstract class in preference to an
interface; because, it simplifies
versioning. This is the practice used
by the Microsoft team which developed
the Base Class Library. ( COM was
designed around interfaces.) Use an
abstract class to define a common base
class for a family of types. Use an
abstract class to provide default
behavior. Subclass only a base class
in a hierarchy to which the class
logically belongs.

夜巴黎 2024-09-19 07:40:56

在非常高的层面上:

任何类型的抽象都归结为分离关注点。抽象的“客户端”代码并不关心抽象公开的契约是如何实现的。例如,您通常不关心字符串类是否使用空终止或缓冲区长度跟踪的内部存储实现。封装隐藏了细节,但是通过创建类/方法/等。抽象,您允许更改实现或添加新的实现,而不会影响客户端代码。

At a very high level:

Abstraction of any kind comes down to separating concerns. "Client" code of an abstraction doesn't care how the contract exposed by the abstraction is fulfilled. You usually don't care if a string class uses a null-terminated or buffer-length-tracked internal storage implementation, for example. Encapsulation hides the details, but by making classes/methods/etc. abstract, you allow the implementation to change or for new implementations to be added without affecting the client code.

日暮斜阳 2024-09-19 07:40:56

当类提供一些高级功能但省略了由派生类实现的某些细节时,通常使用抽象类/方法。使类/方法抽象可确保它不能单独使用,而必须专门定义高层实现中遗漏的细节。这最常与模板方法模式一起使用:

http://en.wikipedia.org/wiki/Template_method_pattern

Abstract classes/methods are generally used when a class provides some high level functionality but leaves out certain details to be implemented by derived classes. Making the class/method abstract ensures that it cannot be used on its own, but must be specialized to define the details that have been left out of the high level implementation. This is most often used with the template method pattern:

http://en.wikipedia.org/wiki/Template_method_pattern

冷情 2024-09-19 07:40:56

通常,人们使用抽象类来提供一些不完整的功能,这些功能将通过具体的子类来充实。它可以提供其子类使用的方法;它还可以表示类层次结构中的中间节点,以表示具体子类的公共分组,以某种方式将它们与其超类的其他子类区分开来。由于接口不能从类派生,因此与接口相比,这是需要类(抽象或其他)的另一种情况。

一个好的经验法则是,只有类层次结构的叶节点才应该被实例化。使非叶节点抽象是确保这一点的简单方法。

Typically one uses an abstract class to provide some incomplete functionality that will be fleshed out by concrete subclasses. It may provide methods that are used by its subclasses; it may also represent an intermediate node in the class hierarchy, to represent a common grouping of concrete subclasses, distinguishing them in some way from other subclasses of its superclass. Since an interface can't derive from a class, this is another situation where a class (abstract or otherwise) would be necessary, versus an interface.

A good rule of thumb is that only leaf nodes of a class hierarchy should ever be instantiated. Making non-leaf nodes abstract is an easy way of ensuring that.

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