CharSequence接口和Object类的toString()方法之间的区别

时间:2017-08-29 13:36:49

标签: java

toString()界面中的CharSequence方法和toString()类中的Object方法之间的真正区别是什么?

我知道String类默认实现CharSequence并扩展Object类。

但是String类是否会从toString()CharSequence提供实现?如果是,那么当我们打印toString()时,会调用String的哪个virion?< / p>

3 个答案:

答案 0 :(得分:1)

toString()方法在CharSequence接口中定义,但未实现。这样做是为了添加有关CharSequence实施需要遵循的要求的相关文档。

具体而言(Java 8 Update 141),CharSequence中的定义是:

/**
 * Returns a string containing the characters in this sequence in the same
 * order as this sequence.  The length of the string will be the length of
 * this sequence.
 *
 * @return  a string consisting of exactly this sequence of characters
 */
public String toString();

这描述了toString()CharSequence实施的行为方式。

将此与Object中的javadoc对比:

/**
 * Returns a string representation of the object. In general, the
 * {@code toString} method returns a string that
 * "textually represents" this object. The result should
 * be a concise but informative representation that is easy for a
 * person to read.
 * It is recommended that all subclasses override this method.
 * <p>
 * The {@code toString} method for class {@code Object}
 * returns a string consisting of the name of the class of which the
 * object is an instance, the at-sign character `{@code @}', and
 * the unsigned hexadecimal representation of the hash code of the
 * object. In other words, this method returns a string equal to the
 * value of:
 * <blockquote>
 * <pre>
 * getClass().getName() + '@' + Integer.toHexString(hashCode())
 * </pre></blockquote>
 *
 * @return  a string representation of the object.
 */
public String toString()

答案 1 :(得分:1)

从您的问题&#34;调用了哪一个?&#34;,听起来好像您认为有两个单独的toString方法:一个来自CharSequence,一个来自{{1 }}。事实并非如此。在Java中,具有相同名称的方法是相同的方法,无论它是从一个,两个还是多个接口实现方法。

例如:

Object

在Java中,只有一种方法interface I1 { int foo(); } interface I2 { int foo(); } class C implements I1, I2 { int foo() { System.out.println("bar"); } } ,无论它是通过interace I1还是I2。将此与C#进行对比,您可以在其中为foo()提供两种不同的实现:每个接口一个。

专门查看您的问题,当您编写实现foo()的类时,您打算覆盖CharSequence方法。但是,唯一让你这样做的是文档。如果您不覆盖它,您将继承toString。如果您覆盖它,则会覆盖仅有一种Object.toString方法,因为如上所示,toStringObject.toString没有区别。< / p>

答案 2 :(得分:0)

如果在超类和超级接口中存在具有相同签名的两个方法,则子类将继承超类中的一个并使用它来覆盖从超级接口继承的一个。 你可以参考这个演示。

    public class Demo {
        public static void main(String args[]) {
            SubClass subClass = new SubClass();
            subClass.printA();  // I inherit from SuperClass
            subClass.printB();  // I inherit from SuperClass
        }
    }


    class SuperClass{
        public void printA(){
            System.out.println("I inherit from SuperClass");
        }

        public void printB(){
            System.out.println("I inherit from SuperClass");
        }
    }

    interface SuperInterface{
        public default void printA(){
            System.out.println("I inherit from SuperInterface");
        };

        void printB();
    }

    class SubClass extends SuperClass implements SuperInterface{
        // No need to override printB() of SuperInterface, 
        // as already inherited one from SuperClass
    }