示例代码如下:
- 需求:


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class NoticeOperator {
public static void main(String[] args) {
NoticeInformation noticeInformation1 = new NoticeInformation(01,"欢迎访问我的CSDN博客","作者",new Date());
NoticeInformation noticeInformation2 = new NoticeInformation(02,"文章有错误请斧正,大家共同进步","爸爸",new Date());
NoticeInformation noticeInformation3 = new NoticeInformation(03,"欢迎下次光临","老鸨",new Date());
NoticeOperator noticeOperator = new NoticeOperator();
List arrayList = new ArrayList();
arrayList.add(noticeInformation1);
arrayList.add(noticeInformation2);
arrayList.add(noticeInformation3);
System.out.println("公告的具体内容和创建者是:");
for(int i=0; i<arrayList.size(); i++){
String noticeTitle =((NoticeInformation)(arrayList.get(i))).getTitle();
String noticeCreator =((NoticeInformation)(arrayList.get(i))).getCreator();
System.out.println(i+1+":"+noticeTitle+"---"+noticeCreator);
}
System.out.println("因为一些不为人知的原因,我修改一下公告的创建者");
((NoticeInformation)(arrayList.get(2))).setCreator("博主");
for(int i=0; i<arrayList.size(); i++){
String creator = ((NoticeInformation)(arrayList.get(i))).getCreator();
System.out.println("修改后公告的创建者为: "+creator);
}
}
}
import java.util.Date;
public class NoticeInformation {
private int id;
private String title;
private String creator;
private Date creatTime;
public NoticeInformation(int id, String title, String creator, Date creatTime) {
this.id = id;
this.title = title;
this.creator = creator;
this.creatTime = creatTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public Date getCreatTime() {
return creatTime;
}
public void setCreatTime(Date creatTime) {
this.creatTime = creatTime;
}
}
- 运行结果:
