简体   繁体   English

类不扩展java.lang.Object

[英]Classes not extending java.lang.Object

While you create a user defined class in Java, you do not specify it as extending Object. 在Java中创建用户定义的类时,不要将其指定为扩展Object。 But still the class is an Object. 但是这个类仍然是一个对象。 How does this work? 这是如何运作的? How does javac or the JVM inject all properties of a class to the user defined class? javac或JVM如何将类的所有属性注入用户定义的类?

If you don't actually write extends Object , the compiler inserts it for you. 如果您实际上没有编写extends Object ,编译器会为您插入它。

EDIT : Apparently I caused some confusion about whether there is actually an insertion of code going on. 编辑 :显然我对是否实际插入代码造成了一些混淆。 I wasn't entirely sure myself so I ran a little experiment: create the following class in file test.java : 我不完全确定自己所以我做了一个小实验:在文件test.java创建以下类:

public class test {}

and compile it, then run 并编译它,然后运行

javap -c test

to disassemble the bytecode. 反汇编字节码。 Look what comes out: 看看会发生什么:

Compiled from "test.java"
public class test extends java.lang.Object{
public test();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."":()V
   4:   return

}

So yes, the compiler does actually insert extends java.lang.Object (or the bytecode equivalent) into the class. 所以是的,编译器确实extends java.lang.Object (或等效的字节码)插入到类中。

All java classes implicitly extend java.lang.Object. 所有java类都隐式扩展java.lang.Object。 From the documentation : 文档

Class Object is the root of the class hierarchy. Class Object是类层次结构的根。 Every class has Object as a superclass. 每个类都有Object作为超类。 All objects, including arrays, implement the methods of this class. 所有对象(包括数组)都实现此类的方法。

Here's a link to JVM spec as well: 这里还有一个指向JVM规范的链接:

The standard class Object is the superclass (§2.8.3) of all other classes. 标准类Object是所有其他类的超类(第2.8.3节)。 A variable of type Object can hold a reference to any object, whether it is an instance of a class or an array. Object类型的变量可以保存对任何对象的引用,无论它是类的实例还是数组。 All class and array types inherit the methods of class Object. 所有类和数组类型都继承Object类的方法。

Well, this may be a glib answer (my favorite kind), but it probably does it the same way it derives a class if you specify a parent. 好吧,这可能是一个滑稽的答案(我最喜欢的那种),但如果你指定一个父类,它可能就像它派生一个类一样。 Isn't that how you'd do it if you were writing the compiler? 如果您正在编写编译器,那不就是这样做的吗?

这是因为所有用户定义的类型都隐式地从Object继承。

To say that the JVM "injects" properties or methods into the class makes it sound like it's something the compiler or runtime does after the fact, as though the .class file is different. 要说JVM在类中“注入”属性或方法,这听起来像是编译器或运行时事后的事情,好像.class文件不同。 Really, all that happens is that when the parser sees that you haven't included any base class with extends , it simply pretends you explicitly specified Object . 实际上,所有发生的事情是当解析器发现你没有包含任何带有extends基类时,它只是假装你明确指定了Object From that point onward, the compiler treats it the same as if you'd typed it out yourself, and the JVM hasn't a clue what you did or didn't specify in the source code. 从那时起,编译器将其视为与您自己键入的相同,并且JVM无法确定您在源代码中执行或未指定的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM