Java:Object类的基本原理未被声明为abstract [英] Java: Rationale of the Object class not being declared abstract

查看:86
本文介绍了Java:Object类的基本原理未被声明为abstract的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 java.lang.Object 类声明为抽象?

当然是对象有用它需要添加状态或行为,Object类是一个抽象,因此它应该被声明为abstract ... 他们为什么选择不这样做?

Surely for an Object to be useful it needs added state or behaviour, an Object class is an abstraction, and as such it should have been declared abstract ... why did they choose not to ?

推荐答案

Ande,我认为你正在接近这个 - 不是意图 - 不必要的抽象程度。我认为这个(恕我直言)不必要的抽象层次是造成这里问题的原因。您可能正在从数学理论方法中接近这一点,我们中的许多人正在从试图解决问题的程序员方法中接近这一点。我认为这种方法的不同导致了分歧。

Ande, I think you are approaching this -- pun NOT intended -- with an unnecessary degree of abstraction. I think this (IMHO) unnecessary level of abstraction is what is causing the "problem" here. You are perhaps approaching this from a mathematical theoretical approach, where many of us are approaching this from a "programmer trying to solve problems" approach. I believe this difference in approach is causing the disagreements.

当程序员看到实用性以及如何实际实现时,有很多当你需要一些完全任意的对象时,它的实际实例是完全不相关的。它不能为空。我在另一篇文章的评论中给出的例子是 * Set 的实现( * == 哈希并发或选择类型),这通常通过使用支持 * Map 并使用 Map 键作为Set。您经常无法使用 null 作为 Map 值,因此通常使用静态 Object instance as value,将被忽略且从未使用过。但是,需要一些非空占位符。

When programmers look at practicalities and how to actually implement something, there are a number of times when you need some totally arbitrary Object whose actual instance is totally irrelevant. It just cannot be null. The example I gave in a comment to another post is the implementation of *Set (* == Hash or Concurrent or type of choice), which is commonly done by using a backing *Map and using the Map keys as the Set. You often cannot use null as the Map value, so what is commonly done is to use a static Object instance as the value, which will be ignored and never used. However, some non-null placeholder is needed.

另一个常见用途是使用 synchronized 关键字,其中需要一些 对象进行同步,并且您希望确保您的同步项完全是私有的,以避免在同一个锁上无意中同步的不同类的死锁。一个非常常见的习惯用法是在一个类中使用私有最终对象作为锁。公平地说,从Java 5和 java.util.concurrent.locks.Lock 和相关的附加内容来看,这个习惯用法的适用性要小得多。

Another common use is with the synchronized keyword where some Object is needed to synchronize on, and you want to ensure that your synchronizing item is totally private to avoid deadlock where different classes are unintentionally synchronizing on the same lock. A very common idiom is to allocate a private final Object to use in a class as the lock. To be fair, as of Java 5 and java.util.concurrent.locks.Lock and related additions, this idiom is measurably less applicable.

从历史上看,在Java中使 Object 可实例化非常有用。您可以提出一个很好的观点,即如果设计变化很小或API变化较小,则不再需要这样做。你可能在这方面是正确的。

Historically, it has been quite useful in Java to have Object be instantiable. You could make a good point that with small changes in design or with small API changes, this would no longer be necessary. You're probably correct in this.

是的,API可以提供占位符类扩展 Object ,根本不添加任何内容,用作上述目的的占位符。但是 - 如果你正在扩展 Object 但是什么都没有添加,除了允许 Object 之外,该类中的值是多少?抽象?数学上,从理论上讲,也许人们可以找到一个值,但实际上,它会增加什么价值?

And yes, the API could have provided a Placeholder class that extends Object without adding anything at all, to be used as a placeholder for the purposes described above. But -- if you're extending Object but adding nothing, what is the value in the class other than allowing Object to be abstract? Mathematically, theoretically, perhaps one could find a value, but pragmatically, what value would it add to do this?

编程中有时候需要一个对象,一些对象,任何非空的具体对象,可以通过 == 和/或<$来比较c $ c> .equals(),但您不需要此对象的任何其他功能。它仅作为唯一标识符存在,否则绝对没有任何作用。 对象非常干净地完成了这个角色和(恕我直言)。

There are times in programming where you need an object, some object, any concrete object that is not null, something that you can compare via == and/or .equals(), but you just don't need any other feature to this object. It exists only to serve as a unique identifier and otherwise does absolutely nothing. Object satisfies this role perfectly and (IMHO) very cleanly.

我猜想这个 Object 未被声明为abstract的部分原因:它直接用于它不是。

I would guess that this is part of the reason why Object was not declared abstract: It is directly useful for it not to be.

这篇关于Java:Object类的基本原理未被声明为abstract的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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