简体   繁体   English

我们如何在java中创建类Object类

[英]How can we create a class like Object class in java

在java中,默认情况下,每个类都扩展了Object ,就像我们如何使我们的类A可以在我们的应用程序中默认扩展。

The short answer - you can't. 简短的回答 - 你不能。

The slightly longer answer - you can create you own custom empty class... 稍微长一点的答案 - 您可以创建自己的自定义空类...

public class MyObject {
}

.. and make every class in your project extend it... ..并使你的项目中的每个类都扩展它...

public class MyOtherClass extends MyObject {
    public int doStuff();
}

... but it's really unlikely there'll be any (good) use for this. ......但实际上不太可能(好)使用它。

No. The class that other classes extend by default is Object . 不会。其他类默认扩展类是Object You cannot change that. 你不能改变它。 It is a fundamental part of the Java language. 它是Java语言的基础部分。

The JLS 8.1.4 says: JLS 8.1.4说:

Given a (possibly generic) class declaration C<F1,...,Fn> (n ≥ 0, C ≠ Object) , the direct superclass of the class type C<F1,...,Fn> is the type given in the extends clause of the declaration of C if an extends clause is present, or Object otherwise. 给定(可能是通用的)类声明C<F1,...,Fn> (n ≥ 0, C ≠ Object) ,类类型C<F1,...,Fn>的直接超类是给定的类型如果存在extends子句,则声明Cextends子句,否则为Object

There is no "wriggle room". 没有“蠕动的空间”。


If you want all of the classes in your applications to be subclasses of something other than Object , then you must have an extends clause on each and every one of them. 如果您希望应用程序中的所有类都是Object其他类,那么您必须在每个类上都有一个extends子句。

You cannot define an own class as a replacement for the Object class. 您不能将自己的类定义为Object类的替代。

You can however create your own base class and let all your classes extend from that base class. 但是,您可以创建自己的基类,并让所有类从该基类扩展。

Answer: You Cannot !! 答:你不能!!

  • Object class is the parent of all the classes in java Object class是java中所有classes的父classes

  • What ever classes you use is a subclass of object class by default 什么都classes ,你用的是子类object class默认

  • You can guarantee there will always be certain methods, like toString() , equals() , hashCode() , etc. 您可以保证总会有某些方法,如toString()equals()hashCode()等。

  • It helps in synchronization , garbage collection etc 它有助于synchronizationgarbage collection


Why it is like this: 为什么会这样:

Java is designed this way so Object class helps by providing all the above mentioned and more features which are fundamental for object communication and object identifying so to have these functionality explicitly extending a class not required Java是这样设计的,因此Object类通过提供所有上述和更多功能来帮助对象通信和对象识别的基础,从而使这些功能显式扩展不需要的类


More information: 更多信息:

  1. Have a look at this Stackoverflow answer here 在这里看看这个Stackoverflow的答案
  2. Oracle Docs Oracle Docs
  3. Grep Code Grep代码

There's no way to do that. 没有办法做到这一点。

You just have to explicitly make every class extend A. like so 你只需要明确地让每个类扩展A.就像这样

public class B extends A {

}

The answer is you can't. 答案是你不能。

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

It's certainly impossible to do it without explicitly using extends . 如果没有明确地使用extends它肯定是不可能的。 Object is the parent class of every Java Class , so I think you'll need to stick to it. Object是每个Java Class的父类,所以我认为你需要坚持下去。 Apart from that, you can always extend a class with the one you like. 除此之外,您始终可以使用自己喜欢的课程扩展课程。

Hope it helps. 希望能帮助到你。

Clemencio Morales Lucas. 克莱门西奥莫拉莱斯卢卡斯。

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

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