用java创建新闻实体类 NewsInfo,包含的属性:newsId(新闻编号)、newsTitle(新闻标 题)、newsContent(新闻内容)、newsDate(发布时间);再定义一个测试类 TestNews, 定义如下方法:showAl1News()查询全部新闻addNews(NewsInfo news)添加新闻deleteNews(int newsId)删除新闻。
时间: 2023-06-03 11:04:26 浏览: 180
这是一个属于程序设计的问题,可以回答。下面是Java代码示例:
NewsInfo.java:
```
public class NewsInfo {
private int newsId;
private String newsTitle;
private String newsContent;
private Date newsDate;
public NewsInfo(int newsId, String newsTitle, String newsContent, Date newsDate) {
this.newsId = newsId;
this.newsTitle = newsTitle;
this.newsContent = newsContent;
this.newsDate = newsDate;
}
public int getNewsId() {
return newsId;
}
public void setNewsId(int newsId) {
this.newsId = newsId;
}
public String getNewsTitle() {
return newsTitle;
}
public void setNewsTitle(String newsTitle) {
this.newsTitle = newsTitle;
}
public String getNewsContent() {
return newsContent;
}
public void setNewsContent(String newsContent) {
this.newsContent = newsContent;
}
public Date getNewsDate() {
return newsDate;
}
public void setNewsDate(Date newsDate) {
this.newsDate = newsDate;
}
}
```
TestNews.java:
```
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class TestNews {
private List<NewsInfo> newsList;
public TestNews() {
newsList = new ArrayList<>();
}
public void showAllNews() {
for (NewsInfo news : newsList) {
System.out.println(news.getNewsId() + " " + news.getNewsTitle() + " " +
news.getNewsContent() + " " + news.getNewsDate());
}
}
public void addNews(NewsInfo news) {
newsList.add(news);
}
public void deleteNews(int newsId) {
for (int i = 0; i < newsList.size(); i++) {
NewsInfo news = newsList.get(i);
if (news.getNewsId() == newsId) {
newsList.remove(i);
break;
}
}
}
public static void main(String[] args) {
TestNews tn = new TestNews();
tn.addNews(new NewsInfo(1, "News Title 1", "News Content 1", new Date()));
tn.addNews(new NewsInfo(2, "News Title 2", "News Content 2", new Date()));
tn.showAllNews();
tn.deleteNews(1);
tn.showAllNews();
}
}
```
阅读全文