为什么Java不允许扩展数组类型

时间:2022-06-21 00:22:24

As an experiment, I tried to extend an int-array like this:

作为一个实验,我试图像这样扩展一个int数组:

public class IntArrayExtension extends int[]{
 // additional fields and methods.
}

to add some methods related to sorting, swapping, sub-array building etc. in the class itself. But I got this error while compiling:

在类本身中添加一些与排序,交换,子数组构建等相关的方法。但是我在编译时遇到了这个错误:

IntArrayExtension.java:1: unexpected type
found   : int[]
required: class
public class IntArrayExtension extends int[]{
                                          ^
1 error

I am curious to know: why Java does not allow to extend an array?

我很想知道:为什么Java不允许扩展数组?

2 个解决方案

#1


9  

Extending a fundamental type such as a String or an array opens up security holes. If Java let you extend an array, its methods that take arrays would become insecure. That is why strings are final, and arrays cannot be extended at all.

扩展基本类型(如String或数组)会打开安全漏洞。如果Java允许您扩展数组,那么采用数组的方法将变得不安全。这就是为什么字符串是最终的,并且数组根本无法扩展。

For example, you could override the clone() method, and return an array of incorrect size. This has a potential of breaking the logic of system code that takes an array as its parameter.

例如,您可以覆盖clone()方法,并返回不正确大小的数组。这有可能打破以数组为参数的系统代码逻辑。

On top of that, arrays are special objects in Java, in that they do not have a class definition.

最重要的是,数组是Java中的特殊对象,因为它们没有类定义。

There are two solution to the problem that you are trying to solve:

您尝试解决的问题有两种解决方案:

  • You could put the logic into a helper class with static methods, similar to Collections, etc. or
  • 您可以使用静态方法将逻辑放入辅助类中,类似于集合等。或者

  • You could encapsulate an array inside your IntArrayExtension class, and provide wrapper methods for accessing the array and its additional features.
  • 您可以在IntArrayExtension类中封装数组,并提供用于访问数组及其附加功能的包装器方法。

#2


4  

Arrays are objects but an array type is not a class and therefore can't be extended. See for example JLS #10.8.

数组是对象,但数组类型不是类,因此无法扩展。参见例如JLS#10.8。

#1


9  

Extending a fundamental type such as a String or an array opens up security holes. If Java let you extend an array, its methods that take arrays would become insecure. That is why strings are final, and arrays cannot be extended at all.

扩展基本类型(如String或数组)会打开安全漏洞。如果Java允许您扩展数组,那么采用数组的方法将变得不安全。这就是为什么字符串是最终的,并且数组根本无法扩展。

For example, you could override the clone() method, and return an array of incorrect size. This has a potential of breaking the logic of system code that takes an array as its parameter.

例如,您可以覆盖clone()方法,并返回不正确大小的数组。这有可能打破以数组为参数的系统代码逻辑。

On top of that, arrays are special objects in Java, in that they do not have a class definition.

最重要的是,数组是Java中的特殊对象,因为它们没有类定义。

There are two solution to the problem that you are trying to solve:

您尝试解决的问题有两种解决方案:

  • You could put the logic into a helper class with static methods, similar to Collections, etc. or
  • 您可以使用静态方法将逻辑放入辅助类中,类似于集合等。或者

  • You could encapsulate an array inside your IntArrayExtension class, and provide wrapper methods for accessing the array and its additional features.
  • 您可以在IntArrayExtension类中封装数组,并提供用于访问数组及其附加功能的包装器方法。

#2


4  

Arrays are objects but an array type is not a class and therefore can't be extended. See for example JLS #10.8.

数组是对象,但数组类型不是类,因此无法扩展。参见例如JLS#10.8。