以下是一個使用Java的StatusInfo類的示例:
import java.util.Date;
public class StatusInfo {
private String message;
private Date timestamp;
private int statusCode;
public StatusInfo(String message, int statusCode) {
this.message = message;
this.timestamp = new Date();
this.statusCode = statusCode;
}
public String getMessage() {
return message;
}
public Date getTimestamp() {
return timestamp;
}
public int getStatusCode() {
return statusCode;
}
public static void main(String[] args) {
StatusInfo statusInfo = new StatusInfo("Success", 200);
System.out.println("Message: " + statusInfo.getMessage());
System.out.println("Timestamp: " + statusInfo.getTimestamp());
System.out.println("Status Code: " + statusInfo.getStatusCode());
}
}
在上述示例中,我們定義了一個StatusInfo類,它有三個私有屬性:message、timestamp和statusCode。我們使用構造函數初始化這些屬性。然后,我們定義了幾個getter方法來訪問這些屬性。
在main方法中,我們創建了一個StatusInfo對象,并使用getter方法打印出對象的屬性值。輸出結果如下:
Message: Success
Timestamp: Sun Aug 29 11:34:52 CST 2021
Status Code: 200