2017-08-29 79 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

相關問題