2017-08-29 77 views
0

CharSequence接口中的toString()方法和Object类中的toString()方法之间的实际区别是什么?CharSequence接口和对象类的toString()方法之间的区别

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

但是否String类从CharSequence给实施toString()?如果是的话哪toString() virsion当我们打印String被调用?

+0

尝试查看'String.toString()'的源代码。 –

+0

不!我只想说,我们在String类中有一个由Object类给出的toString()!我们还有另一个通过CharSequence的toString()(因为String实现了CharSequence)!Ofcourse String类将被赋予CharSequence的抽象方法toString()的实现!那么当我们打印字符串对象的时候呢?哪一个被调用?为什么?因为两种方法都有相同的原型。 – Deepak

回答

1

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

具体地说(爪哇8更新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实现。

对比这与javadoc的在Object

/** 
* 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() 
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 
    } 
1

从你的问题“?哪一个被调用”,它的声音,如果你认为有两个独立的toString方法:一种从CharSequence,一个来自Object。事实并非如此。在Java中,具有相同名称的方法是相同的方法,无论它是从一个,两个还是多个接口实现方法。

例如:

interface I1 { 
    int foo(); 
} 
interface I2 { 
    int foo(); 
} 
class C implements I1, I2 { 
    int foo() { 
     System.out.println("bar"); 
    } 
} 

在Java中,仅存在一个方法foo()不管它经由interace I1或I2来。将其与C#进行对比,您可以在这里给出两种不同的foo()实现:每个接口一个。

特别注意你的问题,当你写一个实现CharSequence的类时,你的意思是覆盖toString方法。但是,唯一能让你做到这一点的是文档。如果您不覆盖它,您将继承Object.toString。如果你重写它,你将覆盖唯一的一个toString方法,因为如上所示,Object.toStringCharSequence.toString没有什么不同。

+0

这很有帮助。 – Deepak

相关问题