java 接口有 toString 方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12124163/
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-10-31 07:40:31  来源:igfitidea点击:

Do interfaces have toString method?

javainterface

提问by Akhilesh Dhar Dubey

How is it possible to call toStringmethod using the reference variable of interface Test, which does not have a toStringmethod?

如何toString使用Test没有toString方法的 interface 的引用变量来调用方法?

interface Test
{
    void show();
    String toHi();
}
class Demo implements Test
{
    public void show(){
        System.out.println("Show");
    }
    public String toString(){
        return "Hello"; 
    }
    public String toHi(){
        return "Hi";    
    }

    public static void main(String[] args) 
    {
        Test t=new Demo();
        String s=t.toString();
        System.out.println(s);
    }
}

采纳答案by Kumar Vivek Mitra

The the Java Documentation says...

在该Java文档说...

When an interface has no direct SuperInterface, it will create abstract public method for all those public methods present in the Object class.

When an interface has no direct SuperInterface, it will create abstract public method for all those public methods present in the Object class.

This is why you are able to call the toString()method on the interface reference

这就是为什么您可以toString()在接口引用上调用该方法的原因

回答by John Calsbeek

Objecthas a toString()method, so everything(except primitive types) has a toString()method. Java will treat anything, even an empty interface, as having all the methods of Object, because it always does.

Object有一个toString()方法,所以一切(原始类型除外)都有一个toString()方法。Java 会将任何东西,甚至是一个空接口,都视为拥有 的所有方法Object,因为它总是如此。

回答by Claudiu

Any Objecthas a toString()method. Anything that would implement an interface will implicitly extend Object, so will also have a toString()method.

任何Object都有toString()方法。任何实现接口的东西都会隐式地扩展Object,所以也会有一个toString()方法。

回答by Bhesh Gurung

The reason that you are able to invoke that method through that variable of your interfce is because of a special treatment in case of interfaces in Java.

您能够通过接口的该变量调用该方法的原因是对 Java 中的接口进行了特殊处理。

Even though the method is not explicitly declared in the interface, the special treatment implicitly provides declarations for all the public instance methods defined in the class Object. And the toStringmethod is one of them.

即使该方法未在接口中显式声明,特殊处理也隐式地为类中定义的所有公共实例方法提供了声明Object。而toString方法就是其中之一。

But, note that interfaces don't implicitly extend any super interface (or class) unlike classes which implicitly extend the Objectclass.

但是,请注意,与隐式扩展Object类的类不同,接口不会隐式扩展任何超级接口(或类)。

You will find a better explanation here - Do Interfaces really inherit the Object class in Java? .

你会在这里找到更好的解释——接口真的继承了 Java 中的 Object 类吗? .

回答by Ilya Gazman

You can't call for Interface toString() method because they only have the methods you declare.

您不能调用接口 toString() 方法,因为它们只有您声明的方法。

But you can trickthe system. Every object have a toString() method and it is obvious that your interface be implemented by class that extend object so you will have it too.

但是你可以欺骗系统。每个对象都有一个 toString() 方法,很明显,您的接口是由扩展对象的类实现的,因此您也将拥有它。

So you can do some thing like that:

所以你可以做这样的事情:

public static void main(String[] args) 
    {
        My m=new C();
        String s= m + ""; // the toString() of the C class will be called here
        System.out.println(s);
    }

回答by Platinum Azure

Class Cimplements Mybut it also extends Object, as all objects eventually do in their inheritance tree.

C实现了,My但它也扩展了Object,就像所有对象最终在它们的继承树中所做的那样。

The Objectclass does have the method toString(), along with a number of others.

Object班的确有方法toString(),有许多其他的一起。

回答by mre

Since class Demoimplicitly extendsclass Object, it inherits method toString. And since that's not an abstractmethod, class Demois not forced to provide an implementation, although you're able to directly invoke toStringon an instance of Demo. For more information, please see Lesson: Interfaces and Inheritance.

由于 classDemo隐式extendsclass Object,它继承了 method toString。而且,由于这不是一个abstract方法,类Demo不强制提供一个实现,虽然你也可以直接调用toString上的一个实例Demo。有关更多信息,请参阅课程:接口和继承

As stated in the ObjectAPI,

正如ObjectAPI中所述,

Class Objectis the root of the class hierarchy. Every class has Objectas a superclass. All objects, including arrays, implement the methods of this class.

Object是类层次结构的根。每个类都有 Object一个超类。所有对象,包括数组,都实现了这个类的方法。

Also, note that the toStringmethod is not part of the interface definition, but rather the Objectclass definition.

另请注意,该toString方法不是接口定义的一部分,而是Object类定义的一部分。

回答by Bharat Sinha

Every Object(except primitive types) in java has a toString() method.

java 中的每个对象(原始类型除外)都有一个 toString() 方法。