为什么没有 java.lang.Array 类?如果一个java数组是一个对象,它不应该扩展对象吗? [英] Why isn't there a java.lang.Array class? If a java array is an Object, shouldn't it extend Object?

查看:25
本文介绍了为什么没有 java.lang.Array 类?如果一个java数组是一个对象,它不应该扩展对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是java包树:http://docs.oracle.com/javase/7/docs/api/java/lang/package-tree.html

我阅读了一篇关于 Java 的教程,其中指出 Java 中的数组是对象.

I read a tutorial on Java which stated that in Java arrays are objects.

数组类在哪里?为什么我们可以制作这样的数组:

Where is the array class? How comes we can make arrays like this:

byte[] byteArr = new byte[];
char[] charArr = new char[];
int[] intArr = new int[];

并且数组将继承 Object 的方法;例如:

and the arrays will inherit methods from Object; for example:

    byte thisByte = 1;
    byte thatByte = 2;
    byte[] theseBytes = new byte[] {thisByte, thatByte};
    int inheritance = theseBytes.length; //inherited 'length' field and some methods
    int wasntInWill = thatByte.length; //error

这是怎么回事?

根据答案,我现在知道它是 java.lang.reflect 包中的 final 类.

As per the answers, I now know it is a final class in java.lang.reflect package.

我现在已经在我的 Android 项目中创建了一个包 java.lang.reflect 并向它添加了一个名为 Array.java 的类.为了确认这是原始类的方式,Eclipse 给了我错误...已经存在于 path/to/android.jar"

I have now created a package java.lang.reflect in my Android project and have added a class called Array.java to it. To confirm this is in the way of the original class, Eclipse gave me the error "... already exists in path/to/android.jar"

如果我写出与 java.lang.reflect.Array 相同的类,但更改 toString() 方法...这应该在我的应用程序中工作,对吗?

If I write out the same class as java.lang.reflect.Array but change the toString() method... this should work within my application right?

推荐答案

来自 JLS:

每个数组都有一个关联的 Class 对象,与所有其他对象共享具有相同组件类型的数组.[This] 就像:数组的直接超类type 是 Object [并且] 每个数组类型都实现了接口 Cloneable和 java.io.Serializable.

Every array has an associated Class object, shared with all other arrays with the same component type. [This] acts as if: the direct superclass of an array type is Object [and] every array type implements the interfaces Cloneable and java.io.Serializable.

这由以下示例代码显示:

This is shown by the following example code:

class Test {
    public static void main(String[] args) {
        int[] ia = new int[3];
        System.out.println(ia.getClass());
        System.out.println(ia.getClass().getSuperclass());
    }
}

打印:

class [I
class java.lang.Object

其中字符串 "[I" 是类对象 "array with component type int" 的运行时类型签名.

where the string "[I" is the run-time type signature for the class object "array with component type int".

是的,因为数组类型有效地扩展了 Object,你可以在 arrayObject 上调用 toString() 也见上面的例子

And yes, since array types effectively extend Object, you can invoke toString() on arrayObject also see the above example

int arr[] = new arr[2];
arr.toString();

这篇关于为什么没有 java.lang.Array 类?如果一个java数组是一个对象,它不应该扩展对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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