Java “扩展对象”有目的还是多余?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2274720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:33:59  来源:igfitidea点击:

Does 'extends Object' have a purpose or is it redundant?

java

提问by Andrei Ciobanu

Following a tutorial on the internet regarding Soap development with Java, I found this link, with a rather unusual code for myself.

根据互联网上关于使用 Java 进行 Soap 开发的教程,我找到了这个链接,其中包含了一个相当不寻常的代码。

The code:

编码:

public class SoapService extends Object {

    /** Creates new SoapService */
    public SoapService() {
    }

/** This is the SOAP exposes method
*/
    public String sayGreeting(String name)
    {
        return "Hello "+name;
    }
}

What's with the 'extends Object' syntax ? I've never encountered this kind of syntax (only on Generics).

“扩展对象”语法有什么用?我从未遇到过这种语法(仅在泛型上)。

Does this syntax has any purpose or is 'plain dumb' ?

这种语法有任何目的还是“愚蠢”?

采纳答案by Thomas L?tzer

Unless the Object class is not actually the java.lang.Objectclass (the tutorial does not include the imports, so it's hard to see), the extends Objectis redundant.

除非 Object 类实际上不是java.lang.Object类(教程不包括导入,因此很难看到),否则extends Object是多余的。

回答by Vanessa MacDougal

It just means it inherits directly from the Object class. Hereis more about inheritance in Java.

这只是意味着它直接从 Object 类继承。这里有更多关于 Java 中的继承。

回答by Erich Douglass

All objects in Java implicitly extend Object, so I'd say it's redundant.

Java 中的所有对象都隐式扩展Object,所以我会说它是多余的。

回答by Trevor Tippins

All classes extend Object implicitlyanyway so it's just redundant coding having no impact.

无论如何,所有类都隐式地扩展了 Object ,因此它只是没有影响的冗余编码。

回答by Uri

No. It's just explicitly doing something that is implicit.

不,它只是明确地做一些隐含的事情。

回答by Kylar

It's unneeded. Every class in Java extends Object at some level. Leave it out, unless you need to clarify something specific.

这是不需要的。Java 中的每个类都在某种程度上扩展了 Object。除非你需要澄清一些具体的事情,否则不要管它。

回答by Martin Milan

My vote, plain dumb - but then I only play with Java...

我的投票,很愚蠢 - 但后来我只玩 Java ......

But any class inherits from the Object Class as far as I know...

但据我所知,任何类都继承自对象类......

回答by Yishai

It is legal but useless boilerplate. Everything extends Object so the language spec allows you to leave it out, and it generally should be left out (some writers of coding standards disagree).

这是合法但无用的样板。一切都扩展了 Object,因此语言规范允许您将其排除在外,并且通常应该将其排除在外(一些编码标准的作者不同意)。

The situation is the same in generics (extends Object is implicit and redundant), it is just that for some reason (I have seen some claim early buggy Generics implementations had issues with the ? wildcard) it has caught on a bit more there.

泛型中的情况是一样的(扩展 Object 是隐式和冗余的),只是由于某种原因(我看到一些声称早期有缺陷的泛型实现有 ? 通配符的问题)它在那里流行了一点。

回答by Lauri

Extends clause is optional as stated in Java Language Specification. If it is omitted, the class is derived from java.lang.Object. It is just a matter of coding style to write it or not to write it in this case. Usually it is omitted.

Extends 子句是可选的,如Java Language Specification 中所述。如果省略,则该类派生自 java.lang.Object。在这种情况下,编写或不编写它只是编码风格的问题。通常省略。

回答by mfx

Looks a bit like generated code - it's extra effort for a source code generator to omitthe "extends" clause if it is not needed, especially if the generator is template-based.

看起来有点像生成的代码 -如果不需要,源代码生成器省略“extends”子句是额外的努力,特别是如果生成器是基于模板的。