你能创建一个 class 不以 Object 作为基础的结构吗 class
Can you create a class structure that does not have Object as its base class
据我所知:Java 中的每个 class 都继承了对象 Class 的方法,而无需指定这样做,这使得 class 如此独特和有趣。
所以我想知道,这个"rule"在JVM里面指定在哪里? class 是否可以以某种方式被操纵,例如添加或删除方法或变量?是否可以独立于对象创建一个并行的层次结构class?
规则在JLS, Section 8.1.4中指定:
[T]he direct superclass of the class type C is the type given in the extends clause of the declaration of C if an extends clause is present, or Object otherwise.
每个 class 要么是 Object
的直接子 class,要么不提供 extends
子句,要么显式扩展 Object
,或者Object
的间接子 class,通过显式扩展 Object
.
的另一个子class
因此,此 class 层次结构中的 class 必须直接或间接扩展 Object
;不能有独立于 Object
.
的其他对象层次结构
Java 对象 class 是您声明的所有 class 对象的超级class。
根据甲骨文:
The Object class, in the java.lang package, sits at the top of the
class hierarchy tree. Every class is a descendant, direct or indirect,
of the Object class. Every class you use or write inherits the
instance methods of Object. You need not use any of these methods,
but, if you choose to do so, you may need to override them with code
that is specific to your class.
对象 class 中的方法可以被覆盖以满足您的需要。
例如:在 POJO 中覆盖 equals()
和 hashcode()
是一种常见的做法。
您还应该查看 Reflection API 以了解如何在运行时修改对象的行为。
Object
class本身确实很特别。如果您在 jre 文件夹的 rt.jar
中查看它的实现,您会注意到它的大部分方法只是带有 native
修饰符的声明。
package java.lang;
public class Object
{
private static native void registerNatives();
public final native Class<?> getClass();
public native int hashCode();
public boolean equals(Object paramObject)
{
return this == paramObject;
}
protected native Object clone()
throws CloneNotSupportedException;
public String toString()
{
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
public final native void notify();
public final native void notifyAll();
public final native void wait(long paramLong)
throws InterruptedException;
public final void wait(long paramLong, int paramInt)
throws InterruptedException
{
if (paramLong < 0L) {
throw new IllegalArgumentException("timeout value is negative");
}
if ((paramInt < 0) || (paramInt > 999999)) {
throw new IllegalArgumentException("nanosecond timeout value out of range");
}
if (paramInt > 0) {
paramLong += 1L;
}
wait(paramLong);
}
public final void wait()
throws InterruptedException
{
wait(0L);
}
protected void finalize()
throws Throwable
{}
static {}
}
那些本机方法是在 JVM 本身内部实现的。这些方法是 Java 程序的本机部分和托管部分之间的桥梁。正因如此,自然而然的就有了这个Object
class作为"the common things in every object"。 Java 语言的指定方式是除了 Object
.
之外无法构建单独的 class 层次结构
关于修改Object
虽然我还没有尝试过,但可能可以修改原始对象class。肯定不能删除现有方法,因为大多数运行时都依赖它们,但我看到您可以通过修改运行时本身来添加新方法。
好像不能用java agents修改Object
,String
,ClassLoader
,Class<?>
,ProtectionDomain
,IllegalClassFormatException
和数组 classes.
- where is this "rule" specified, inside the JVM ?
它不是由 JVM 完成的。编译器将 extends Object
添加到您的 class。
- Can this class be somehow manipulated
没有
- Is it possible to create a parallel, hierarchical structure independently of the Object class?
没有。你不能。每一个 java class 都会扩展 Object
class 而你就是无法避免它
如果您想创建一个不直接或间接派生自 java.lang.Object
的 class,您必须创建一个没有指定 superclass 的 class 文件.
现在,如果您查看解析 class 文件的 JDK 代码,您会发现 class 文件解析器执行此操作 check:
if (super_class_index == 0) {
check_property(class_name == vmSymbols::java_lang_Object(),
"Invalid superclass index %u in class file %s",
super_class_index,
CHECK_(nullHandle));
} else {
所以它会拒绝你的基地 class。只有java.lang.Object
可以没有superclass.
据我所知:Java 中的每个 class 都继承了对象 Class 的方法,而无需指定这样做,这使得 class 如此独特和有趣。
所以我想知道,这个"rule"在JVM里面指定在哪里? class 是否可以以某种方式被操纵,例如添加或删除方法或变量?是否可以独立于对象创建一个并行的层次结构class?
规则在JLS, Section 8.1.4中指定:
[T]he direct superclass of the class type C is the type given in the extends clause of the declaration of C if an extends clause is present, or Object otherwise.
每个 class 要么是 Object
的直接子 class,要么不提供 extends
子句,要么显式扩展 Object
,或者Object
的间接子 class,通过显式扩展 Object
.
因此,此 class 层次结构中的 class 必须直接或间接扩展 Object
;不能有独立于 Object
.
Java 对象 class 是您声明的所有 class 对象的超级class。
根据甲骨文:
The Object class, in the java.lang package, sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the Object class. Every class you use or write inherits the instance methods of Object. You need not use any of these methods, but, if you choose to do so, you may need to override them with code that is specific to your class.
对象 class 中的方法可以被覆盖以满足您的需要。
例如:在 POJO 中覆盖 equals()
和 hashcode()
是一种常见的做法。
您还应该查看 Reflection API 以了解如何在运行时修改对象的行为。
Object
class本身确实很特别。如果您在 jre 文件夹的 rt.jar
中查看它的实现,您会注意到它的大部分方法只是带有 native
修饰符的声明。
package java.lang;
public class Object
{
private static native void registerNatives();
public final native Class<?> getClass();
public native int hashCode();
public boolean equals(Object paramObject)
{
return this == paramObject;
}
protected native Object clone()
throws CloneNotSupportedException;
public String toString()
{
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
public final native void notify();
public final native void notifyAll();
public final native void wait(long paramLong)
throws InterruptedException;
public final void wait(long paramLong, int paramInt)
throws InterruptedException
{
if (paramLong < 0L) {
throw new IllegalArgumentException("timeout value is negative");
}
if ((paramInt < 0) || (paramInt > 999999)) {
throw new IllegalArgumentException("nanosecond timeout value out of range");
}
if (paramInt > 0) {
paramLong += 1L;
}
wait(paramLong);
}
public final void wait()
throws InterruptedException
{
wait(0L);
}
protected void finalize()
throws Throwable
{}
static {}
}
那些本机方法是在 JVM 本身内部实现的。这些方法是 Java 程序的本机部分和托管部分之间的桥梁。正因如此,自然而然的就有了这个Object
class作为"the common things in every object"。 Java 语言的指定方式是除了 Object
.
关于修改Object
虽然我还没有尝试过,但可能可以修改原始对象class。肯定不能删除现有方法,因为大多数运行时都依赖它们,但我看到您可以通过修改运行时本身来添加新方法。
好像不能用java agents修改Object
,String
,ClassLoader
,Class<?>
,ProtectionDomain
,IllegalClassFormatException
和数组 classes.
- where is this "rule" specified, inside the JVM ?
它不是由 JVM 完成的。编译器将 extends Object
添加到您的 class。
- Can this class be somehow manipulated
没有
- Is it possible to create a parallel, hierarchical structure independently of the Object class?
没有。你不能。每一个 java class 都会扩展 Object
class 而你就是无法避免它
如果您想创建一个不直接或间接派生自 java.lang.Object
的 class,您必须创建一个没有指定 superclass 的 class 文件.
现在,如果您查看解析 class 文件的 JDK 代码,您会发现 class 文件解析器执行此操作 check:
if (super_class_index == 0) {
check_property(class_name == vmSymbols::java_lang_Object(),
"Invalid superclass index %u in class file %s",
super_class_index,
CHECK_(nullHandle));
} else {
所以它会拒绝你的基地 class。只有java.lang.Object
可以没有superclass.