Java 使用构造函数和 "this" 对代码输出的解释
Java explanation to code output using constructors and "this"
public class potpie {
public int month;
public int day;
public int year;
public static void main(String[] args) {
potpie potObject = new potpie(4,5,6);
}
public potpie(int m, int d, int y){
month = m;
day = d;
year = y;
System.out.printf("The constructor for this is %s\n", this);
}
public String toString(){
return String.format("%d/%d/%d", month, day, year);
}
}
我正在看java上的视频教程,他写了这段代码。但是我不明白他对为什么打印出来的解释
The constructor for this is 4/5/6
我只是不明白为什么要使用 toString 方法?
当您覆盖 class 中的 toString
方法时。它在 class.
的构造对象上调用了 toString
方法
如果你不覆盖它,输出是,class name,然后是'at'符号,最后是object构造对象的hashCode like
The default toString() method in Object prints “class name @ hash code"
在这里打印:
System.out.printf("The constructor for this is %s\n", this);
这次调用字符串:
String.format("%d/%d/%d", month, day, year)
你的输出如下:
System.out.printf("The constructor for this is %s\n", {toString call});
最后,
The constructor for this is 4/5/6
位于构造函数内部的this
引用当前对象"being constructed",potpie
。
sysout函数自动调用传入对象的toString()方法。如果对象的 class 没有覆盖 toString
方法,您可能会得到看起来像 "classname@HexNumber"
.
的 Object
版本
// Behind the scenes
System.out.printf("The constructor for this is %s\n", this.toString());
potpie potObject = new potpie(4,5,6);
它将使用值 (4,5,6) 调用 public potpie(int m, int d, int y)
方法;
然后将m,d,y的值赋给month,day,year变量
这用于 class 的调用电流 object
,方法等
然后他们编写 toString()
方法来格式化月份日期和年份。
格式为 %d
即 return /date/month/year
.
- 默认的
toString()
方法包含“classname @ hash code of class”,比如 test@45689。
toString()
方法在调用 printf
期间由 API 本身调用,因为您在格式字符串中指定了 %s
标识符。
System.out.printf("The constructor for this is %s\n", this);
^^
此方法使用 class Formatter
格式化输出并引用 its documentation 作为 %s
标识符(强调我的):
If the argument arg
is null
, then the result is "null"
. If arg
implements Formattable
, then arg.formatTo
is invoked. Otherwise, the result is obtained by invoking arg.toString()
.
public class potpie {
public int month;
public int day;
public int year;
public static void main(String[] args) {
potpie potObject = new potpie(4,5,6);
}
public potpie(int m, int d, int y){
month = m;
day = d;
year = y;
System.out.printf("The constructor for this is %s\n", this);
}
public String toString(){
return String.format("%d/%d/%d", month, day, year);
}
}
我正在看java上的视频教程,他写了这段代码。但是我不明白他对为什么打印出来的解释
The constructor for this is 4/5/6
我只是不明白为什么要使用 toString 方法?
当您覆盖 class 中的 toString
方法时。它在 class.
toString
方法
如果你不覆盖它,输出是,class name,然后是'at'符号,最后是object构造对象的hashCode like
The default toString() method in Object prints “class name @ hash code"
在这里打印:
System.out.printf("The constructor for this is %s\n", this);
这次调用字符串:
String.format("%d/%d/%d", month, day, year)
你的输出如下:
System.out.printf("The constructor for this is %s\n", {toString call});
最后,
The constructor for this is 4/5/6
位于构造函数内部的this
引用当前对象"being constructed",potpie
。
sysout函数自动调用传入对象的toString()方法。如果对象的 class 没有覆盖 toString
方法,您可能会得到看起来像 "classname@HexNumber"
.
Object
版本
// Behind the scenes
System.out.printf("The constructor for this is %s\n", this.toString());
potpie potObject = new potpie(4,5,6);
它将使用值 (4,5,6) 调用 public potpie(int m, int d, int y)
方法;
然后将m,d,y的值赋给month,day,year变量
这用于 class 的调用电流 object
,方法等
然后他们编写 toString()
方法来格式化月份日期和年份。
格式为 %d
即 return /date/month/year
.
- 默认的
toString()
方法包含“classname @ hash code of class”,比如 test@45689。
toString()
方法在调用 printf
期间由 API 本身调用,因为您在格式字符串中指定了 %s
标识符。
System.out.printf("The constructor for this is %s\n", this);
^^
此方法使用 class Formatter
格式化输出并引用 its documentation 作为 %s
标识符(强调我的):
If the argument
arg
isnull
, then the result is"null"
. Ifarg
implementsFormattable
, thenarg.formatTo
is invoked. Otherwise, the result is obtained by invokingarg.toString()
.