CharSequence
接口中的toString()
方法和Object
类中的toString()
方法之间的实际区别是什么?CharSequence接口和对象类的toString()方法之间的区别
我知道String
类默认实现了CharSequence
并且扩展了Object
类。
但是否String
类从CharSequence
给实施toString()
?如果是的话哪toString()
virsion当我们打印String
被调用?
CharSequence
接口中的toString()
方法和Object
类中的toString()
方法之间的实际区别是什么?CharSequence接口和对象类的toString()方法之间的区别
我知道String
类默认实现了CharSequence
并且扩展了Object
类。
但是否String
类从CharSequence
给实施toString()
?如果是的话哪toString()
virsion当我们打印String
被调用?
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()
如果有与超类和超接口相同的签名拖的方法,子类将继承超类中的一个,并用它来覆盖从超级接口继承的一个。
你可以参考这个演示。
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
}
从你的问题“?哪一个被调用”,它的声音,如果你认为有两个独立的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.toString
和CharSequence.toString
没有什么不同。
这很有帮助。 – Deepak
尝试查看'String.toString()'的源代码。 –
不!我只想说,我们在String类中有一个由Object类给出的toString()!我们还有另一个通过CharSequence的toString()(因为String实现了CharSequence)!Ofcourse String类将被赋予CharSequence的抽象方法toString()的实现!那么当我们打印字符串对象的时候呢?哪一个被调用?为什么?因为两种方法都有相同的原型。 – Deepak