问题小结
一开始运行书上的例子的时候我又一个很疑惑的点,就是在输出logs这个对象数组的时候,我用System.out.println(logs[i]);和System.out.println(logs[i].toString);的输出数据是完全一样的,而且debug的输出显示这两种写法执行的时候都会直接跳转到toString方法。我看了很久猛然发现在LogRec类中有一个小图标
这才激起了我的灵感,我发现这是因为toString方法被重写了,然后我把这个重写后的toString方法注释掉了重新运行,输出结果就变成了这样:
这里输出的是地址对应的哈希值,这才是我印象中正常的输出啊!
随后我又debug了一下
发现System.out.println(logs[i])的时候是访问Object类中的println方法
很显然这个方法就是创建一个s字符串然后调用String.valueOf方法,然后输出这个s字符串。
然后在valueOf方法当中,首先会判断是不是空的,如果是空,输出null,不是空则输出toString方法的返回值。
在这个toString方法当中,很显然返回值就是类名+方法名+@+16进制的哈希码。
所以在没有重写的时候,输出结果会是像com.qst.dms.entity.LogRec@2ef5e5e3这样。
然后在这个程序中,如图重写了toString方法,这时候调用toString方法的时候会输出对象数组中的内容,所以System.out.println(logs[i])的输出结果才会直接是对象数组的内容。
代码:
package com.qst.dms.dos;
import com.qst.dms.entity.LogRec;
import com.qst.dms.service.LogRecService;
import java.util.Date;
public class LogRecDemo {
public static void main(String[] args) {
LogRecService logService = new LogRecService();
LogRec[] logs = new LogRec[3];
for (int i = 0; i < logs.length; i++) {
System.out.println("第" + (i + 1) + "个日志数据采集:");
logs[i] = logService.inputLog();
}
logService.showLog(logs);
}
}
package com.qst.dms.service;
import java.util.Date;
import java.util.Scanner;
import com.qst.dms.entity.LogRec;
public class LogRecService {
public LogRec inputLog() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入ID标识:");
int id = scanner.nextInt();
Date nowDate = new Date();
System.out.println("请输入地址:");
String address = scanner.next();
int type = LogRec.GATHER;
System.out.println("请输入登录用户名:");
String user = scanner.next();
System.out.println("请输入主机IP:");
String ip = scanner.next();
System.out.println("请输入登录状态:1是登录,0是登出");
int logType = scanner.nextInt();
LogRec log = new LogRec(id, nowDate, address, type, user, ip, logType);
return log;
}
public void showLog(LogRec... logRecs) {
for (LogRec e : logRecs) {
if (e != null) {
System.out.println(e.toString());
}
}
}
}
package com.qst.dms.entity;
import java.util.Date;
public class LogRec {
// ID 标识
private int id;//时间
private Date time;
private String address;//状态
private int type;
private String user;
private String ip;
private int logType;
public static final int LOG_IN = 1;
public static final int LOG_out = 0;
public static final int GATHER = 1;
public static final int МАТНСН = 2;
public static final int RECORD = 3;
public static final int SEND = 4;
public static final int RECEIVE = 5;
public static final int WRITE = 6;
public static final int SAVE = 7;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getLogType() {
return logType;
}
public void setLogType(int logType) {
this.logType = logType;
}
public LogRec() {
}
public LogRec(int id, Date time, String address, int type, String user, String ip, int logType) {
this.id = id;
this.time = time;
this.address = address;
this.type = type;
this.user = user;
this.ip = ip;
this.logType = logType;
}
public String toString() {
return id + "," + time + "," + address + "," + this.getType() + "," + user + "," + ip + "," + logType;
}
}