从 main 隐式调用 class 中的方法
Implicitly calling a method in a class from main
我是 Java 的新手,我正在尝试解决教授给出的练习。
他给了这个class
public class MioPunto {
public int x;
public int y;
public String toString() {
return ("[" + x + "," + y + "]");
}
}
我应该用 main 方法写另一个 class。在 main 中,我必须在不显式调用 "toString" 方法的情况下打印坐标。
不知不觉就这样解决了
public class TestMioPunto{
public static void main(String[] args){
MioPunto inizio = new MioPunto();
MioPunto fine = new MioPunto();
inizio.x=10;
inizio.y=10;
fine.x=20;
fine.y=30;
System.out.println("Inizio: " + inizio + "\n" + "Fine: " + fine);
}
}
输出为
我不明白 Java 是如何自动调用 toString 方法的(括号和逗号),您能解释一下吗?
Java 在 String
和对象上使用 +
时调用 toString
。
所以你的
System.out.println("Inizio: " + inizio + "\n" + "Fine: " + fine);
与
相同
System.out.println("Inizio: " + inizio.toString() + "\n" + "Fine: " + fine.toString());
除了,如果inizio
或fine
是null
,第一个不会给你一个错误(你会看到null
在字符串中代替)但第二个会。
来自 the String Concatenation operator 上的 JLS 部分:
If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time.
指的是 String Conversion section,它说(在讨论将原语转换为字符串之后):
...
Now only reference values need to be considered:
If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l).
Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.
由于 java.lang.Object
是 MioPunto
的隐式基 class(因为它是所有 Java 对象的基 class),方法 MioPunto.toString()
是 Object.toString()
方法的 覆盖 。
由于所有对象都有 toString()
的实现,Java 编译器可以自由地在使用 [=16 执行的字符串连接链中的任何对象上添加 toString()
的调用=] 运算符:
The string concatenation operator +
(§15.18.1), which, when given a String
operand and a reference, will convert the reference to a String
by invoking the toString
method of the referenced object (using "null"
if either the reference or the result of toString
is a null
reference), and then will produce a newly created String
that is the concatenation of the two strings.
我是 Java 的新手,我正在尝试解决教授给出的练习。 他给了这个class
public class MioPunto {
public int x;
public int y;
public String toString() {
return ("[" + x + "," + y + "]");
}
}
我应该用 main 方法写另一个 class。在 main 中,我必须在不显式调用 "toString" 方法的情况下打印坐标。 不知不觉就这样解决了
public class TestMioPunto{
public static void main(String[] args){
MioPunto inizio = new MioPunto();
MioPunto fine = new MioPunto();
inizio.x=10;
inizio.y=10;
fine.x=20;
fine.y=30;
System.out.println("Inizio: " + inizio + "\n" + "Fine: " + fine);
}
}
输出为
Java 在 String
和对象上使用 +
时调用 toString
。
所以你的
System.out.println("Inizio: " + inizio + "\n" + "Fine: " + fine);
与
相同System.out.println("Inizio: " + inizio.toString() + "\n" + "Fine: " + fine.toString());
除了,如果inizio
或fine
是null
,第一个不会给你一个错误(你会看到null
在字符串中代替)但第二个会。
来自 the String Concatenation operator 上的 JLS 部分:
If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time.
指的是 String Conversion section,它说(在讨论将原语转换为字符串之后):
...
Now only reference values need to be considered:
If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l).
Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.
由于 java.lang.Object
是 MioPunto
的隐式基 class(因为它是所有 Java 对象的基 class),方法 MioPunto.toString()
是 Object.toString()
方法的 覆盖 。
由于所有对象都有 toString()
的实现,Java 编译器可以自由地在使用 [=16 执行的字符串连接链中的任何对象上添加 toString()
的调用=] 运算符:
The string concatenation operator
+
(§15.18.1), which, when given aString
operand and a reference, will convert the reference to aString
by invoking thetoString
method of the referenced object (using"null"
if either the reference or the result oftoString
is anull
reference), and then will produce a newly createdString
that is the concatenation of the two strings.