为什么是数组对象,但不能用作基类? [英] Why are arrays Objects, but can not be used as a base class?

查看:135
本文介绍了为什么是数组对象,但不能用作基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java语言规范指定

The Java language specification specifies that


在Java编程语言中,数组是对象(§4.3.1)并且可以分配给Object类型的变量(§4.3.2)。 Object 的所有方法都可以在数组上调用。

In the Java programming language arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.

考虑数组是对象 - 为什么Java设计人员决定不允许继承和覆盖它,例如 toString() equals()

So, considering arrays are objects — why did the Java designers make the decision not to allow inherit and override from it, for example, toString() or equals()?

当前语法不允许创建带有数组作为基类的匿名类,但我不认为<

The current syntax wouldn't allow creating anonymous classes with an array as the base class, but I don't think that was the reason for their decision.

推荐答案

Java是非目标语言和非常慢的语言之间的折衷方案其中一切都是一个对象(想想 Smalltalk )。

Java was a compromise between non-object languages and very slow languages of that time where everything was an object (think about Smalltalk).

即使在最近的语言中,在数组(通常是地图)的语言级别具有快速结构也被视为一个战略目标。大多数人不喜欢数组的可继承对象的权重,肯定没有人想要这样的JVM前进像JIT。

Even in more recent languages, having a fast structure at the language level for arrays (and usually maps) is considered a strategic goal. Most people wouldn't like the weight of an inheritable object for arrays, and certainly nobody wanted this before JVM advances like JIT.

这就是为什么数组作为对象时,不是设计为类实例()。jls / se8 / html / jls-4.html#jls-4.3.1有能力重写数组上的方法,并且肯定不是一个足够的一个来平衡需要检查正确的方法应用(在我看来不是一个很好的平衡代码读取的难度增加,类似于覆盖运算符时发生的情况)。

That's why arrays, while being objects, weren't designed as class instances ("An object is a class instance or an array"). There would be little benefit in having the ability to override a method on an array, and certainly not a great-enough one to counterbalance the need to check for the right method to apply (and in my opinion not a great-one enough to counterbalance the increased difficulty in code reading, similar to what happens when you override operators).

这篇关于为什么是数组对象,但不能用作基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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