简体   繁体   English

覆盖Java中的toString方法

[英]Override toString method in Java

public class Curiosity {



public void toString()//error because of this specific method name 
{
    System.out.println("method is successfully implemented");
}



}

How can i use a method of the same name "toString()" if i want to ? 如果要如何使用同名的方法“ toString()”?

Do I have to give its return type as String if not what should i do to change its return type like suppose if i want to use a void return type for toString does java allow that ? 如果不是我是否必须将其返回类型指定为String, 如果我想为toString使用void返回类型,我应该怎么做才能更改其返回类型?java是否允许这样做?

toString() method must return a String . toString()方法必须返回String That's the only way to override Object 's toString() . 这是重写ObjecttoString()的唯一方法。

public String toString()
{
    return "method is successfully implemented";
}

If you wish to use the same name but not override Object 's toString , you can overload the name toString by adding arguments, thus changing the signature of your method. 如果希望使用相同的名称但不覆盖 ObjecttoString ,则可以通过添加参数来重载名称toString ,从而更改方法的签名。

Example : 范例:

public void toString (String something)
{
    System.out.println("method is successfully implemented " + something);
}

You are trying to overload toString() method in a wrong manner 您试图以错误的方式重载toString()方法

Overloaded methods are differentiated by the number and the type of the arguments passed into the method. 重载的方法通过传递给方法的参数的数量和类型来区分。 In the code sample, draw(String s) and draw(int i) are distinct and unique methods because they require different argument types. 在代码示例中,draw(String s)和draw(int i)是不同且唯一的方法,因为它们需要不同的参数类型。

You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart. 您不能声明多个具有相同名称,相同数量和类型的参数的方法,因为编译器无法区分它们。

The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type. 区分方法时,编译器不会考虑返回类型,因此即使两个方法具有不同的返回类型,也无法声明具有相同签名的两个方法。

The only way to use toString() in your class is by keeping the return type as String 在类中使用toString()的唯一方法是将返回类型保持为String

public String toString()
{
   //your code here
}

That is how it is defined in Object class and if you wish to override it you will have to use the exact signature 这就是在Object类中定义的方式,如果要覆盖它,则必须使用确切的签名

or if you still wish to use the method name as toString what you can do is change the method's signature. 或者,如果您仍然希望将方法名称用作toString,则可以更改方法的签名。

A method's signature includes method's name and the parameters. 方法的签名包括方法的名称和参数。 Remember that return type is not a part of a method's signature 请记住,返回类型不是方法签名的一部分

You can look at the source code of the java.lang.Object . 您可以查看java.lang.Object的源代码。

The toString method have a return value in String type. toString方法具有String类型的返回值。 You can't have another method which's name is toString but return type is not String. 您不能有另一个名为toString的方法,但返回类型不是String。

Actually, it's forbidden in Java in any inheritance relationship. 实际上,在Java中任何继承关系都禁止使用它。 When you call the method, the compiler only cares the name and the parameters. 调用该方法时,编译器只关心名称和参数。 So how can it distinguishes the methods of the same name but with the different return type? 那么,如何区分相同名称但返回类型不同的方法呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM