java java中的自定义toString()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15615094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom toString() in java
提问by localhost
I'm studying to do my java OCA test, using the book "Java SE7 Programming Essentials" by Michael Ernest. This is my code for one of the answers to a question below:
我正在学习使用 Michael Ernest 所著的“Java SE7 Programming Essentials”一书进行 Java OCA 测试。这是我对以下问题的答案之一的代码:
public class Point3D {
int x, y, z;
public void setX(int x) {
this.x = x;
}
public int getX() {
return this.x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return this.y;
}
public void setZ(int z) {
this.z = z;
}
public int getZ() {
return this.z;
}
public String toString(Point3D p) {
String result = p.getX() + "," + p.getY() + "," + p.getZ();
return result;
}
public static void main(String args[]) {
Point3D point = new Point3D();
point.setX(5);
point.setY(12);
point.setZ(13);
System.out.println(point.toString(point));
}
}
My code works, but in the last line, I think I've made my code in a weird way, shouldn't there be a way to make just point.toString()
and not point.toString(point)
return the String representation of the point? Can anyone explain to me how to fix it?
我的代码有效,但在最后一行,我认为我以一种奇怪的方式编写了我的代码,难道不应该有一种方法来制作point.toString()
而不point.toString(point)
返回点的字符串表示吗?谁能向我解释如何解决它?
I'm sure it's a simple answer, just trying to understand it because I suspect it points to a hole in my java knowledge.
我确定这是一个简单的答案,只是试图理解它,因为我怀疑它指出了我的 Java 知识中的一个漏洞。
回答by Xavi López
Your method public String toString(Point3D p)
is not overriding Object.toString()
, which is the standard way to obtain a String
representation of an Object
.
您的方法public String toString(Point3D p)
是不重写Object.toString()
,这是获得的标准方式String
的表示Object
。
The point of overriding it is to allow any subclass of Object
to provide an appropriate representation of the object as a String
. The Java API uses this method in a number of circumstances, mainly when needing to transform an Object
into a String
(for instance when doing System.out.println(object)
, or performing String concatenation:
覆盖它的要点是允许 的任何子类Object
提供对象的适当表示为String
。Java API 在多种情况下使用此方法,主要是在需要将 anObject
转换为 a 时String
(例如在执行System.out.println(object)
或执行字符串连接时:
String s = "The point is " + pointObject;
String s = "The point is " + pointObject;
Implement it as ay89 suggested:
按照 ay89 的建议实施:
@Override
public String toString() {
String result = getX() + "," + getY() + "," + getZ();
return result;
}
Please note the usage of the @Override
annotation. If you used it on your method, the compiler would have warned you that something was wrong with its definition.
请注意注释的用法@Override
。如果你在你的方法中使用它,编译器会警告你它的定义有问题。
回答by Ankit
why don't you use
你为什么不使用
public String toString() {
String result = getX() + "," + getY() + "," + getZ();
return result;
}
then you can just use System.out.println(point)
那么你就可以使用 System.out.println(point)
回答by dasblinkenlight
I would use String.format
instead of concatenation for clarity:
String.format
为清楚起见,我将使用而不是串联:
public String toString() {
return String.format("(%d, %d, %d)", x, y, z);
}
With this method you can change the way the output looks in a way that does not require much "mental reconstruction" from the readers of your code. For example, should you decide to change the output to, say, {x=1, y=2, z=3}
you can simply rewrite your format line as follows:
使用此方法,您可以更改输出的外观,而无需从代码的读者那里进行太多的“心理重建”。例如,如果您决定将输出更改为,例如,{x=1, y=2, z=3}
您可以简单地重写您的格式行,如下所示:
return String.format("{x=%d, y=%d, z=%d}", x, y, z);
回答by localhost
OK, I worked it out myself - posting the question proved to be much the same as Rubber Duckingfor me.
好的,我自己解决了这个问题 - 事实证明,发布这个问题与我的橡皮鸭很相似。
I needed to add another method:
我需要添加另一种方法:
public String toString() {
String result = this.getX() + "," + this.getY() + "," + this.getZ();
return result;
}
回答by Alex Florescu
You need to override the toString() method. All objects in Java have a toString method (it's a method on the Object class).
您需要覆盖 toString() 方法。Java 中的所有对象都有一个 toString 方法(它是 Object 类上的一个方法)。
The default Object implementation just returns the hashCode of that object (so if you don't override it, that's what you will get as well), but if you override, you can do something more meaningful with data specific to your object.
默认的 Object 实现只返回该对象的 hashCode(因此,如果您不覆盖它,您也会得到它),但是如果您覆盖,您可以对特定于您的对象的数据做一些更有意义的事情。
@Override
public String toString() {
return this.getX() + "," + this.getY() + "," + this.getZ();
}
回答by Rob
You could simply use this
instead of the parameter p
. So change your method to public String toString()
and change the p
to this
in the function itself.
您可以简单地使用this
代替参数p
。因此,将您的方法更改为public String toString()
并更改函数本身中的p
to this
。
回答by Mohan Raj B
Try this
试试这个
public String toString() {
String result = this.getX() + "," + this.getY() + "," + this.getZ();
return result;
}
回答by Jops
This should fix it:
这应该解决它:
public String toString() {
String result = this.getX() + "," + this.getY() + "," + this.getZ();
return result;
}
回答by Code2Interface
do this way
这样做
public String toString() {
String result = this.getX() + "," + this.getY() + "," + this.getZ();
return result;
}
thisis the internal reference to the calling object(point in your case) and thus using it will invalidate the need for the argument point
这是对调用对象的内部引用(在您的情况下为点),因此使用它将使对参数点的需求无效
回答by rbento
This code:
这段代码:
@Override
public String toString() {
return "{ x: " + x + ", y: " + y + ", z: " + z + " }";
}
Would output this:
会输出这个:
{ x: 13, y: 2, z: 10 }
As your get methods return the attributes, by using the attributes directly you spare the method calls.
当您的 get 方法返回属性时,通过直接使用属性,您可以省去方法调用。
Also, recent compilers will optimize this concatenation with StringBuilder
automatically.
此外,最近的编译器将StringBuilder
自动优化这种连接。
For more information check this post.
有关更多信息,请查看此帖子。
I hope it helps
我希望它有帮助