一道Java练习题
1.定义论坛的帖子类(Topic),拥有属性:标题(title)、内容(content)、发表时间(publishTime)、用户(user)、所属板块(board),回...
1. 定义论坛的帖子类(Topic),拥有属性:标题(title)、内容(content)、发表时间(publishTime)、用户(user)、所属板块(board),回复(fTopics ArrayList),无参数构造方法对属性进行默认初始化,有参数构造方法,对所有属性进行初始化,显示信息方法(toString())
编写测试类 设置帖子信息,并输出调用输出方法,输出帖子信息,包括子贴信息
例如:
标题:报道帖
内容:初来乍到请各位多多指教!
用户:张三
回复:
1楼:
标题:re:报道帖
内容:欢迎,欢迎,热烈欢迎
用户:李四
2楼:
标题:re:报道帖
内容:路过,我是来打酱油的
用户:马五
3楼:
标题:哈哈
内容:板凳哦!~~~
用户:王六
求答案,答案满意的会加分 展开
编写测试类 设置帖子信息,并输出调用输出方法,输出帖子信息,包括子贴信息
例如:
标题:报道帖
内容:初来乍到请各位多多指教!
用户:张三
回复:
1楼:
标题:re:报道帖
内容:欢迎,欢迎,热烈欢迎
用户:李四
2楼:
标题:re:报道帖
内容:路过,我是来打酱油的
用户:马五
3楼:
标题:哈哈
内容:板凳哦!~~~
用户:王六
求答案,答案满意的会加分 展开
1个回答
展开全部
import java.util.ArrayList;
import java.util.Iterator;
/**
*
*/
/**
* @author Think
*
*/
public class TestTopics {
/**
*
*/
public TestTopics() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Topic parentTopic = new Topic("报道帖","初来乍到请各位多多指教!","张三","","");
Topic child1 = new Topic();
Topic child2 = new Topic();
Topic child3 = new Topic();
child1.setContent("欢迎,欢迎,热烈欢迎");
child1.setTitle("re:" + parentTopic.getTitle());
child1.setUser("李四");
child2.setContent("路过,我是来打酱油的");
child2.setTitle("re:" + parentTopic.getTitle());
child2.setUser("马五");
child3.setContent("板凳哦!~~~");
child3.setTitle("哈哈");
child3.setUser("王六");
parentTopic.addChildTopic(child1);
parentTopic.addChildTopic(child2);
parentTopic.addChildTopic(child3);
System.out.println(parentTopic.toString());
}
}
class Topic
{
public Topic()
{
this.title = "";
this.content = "";
this.publishTime = "";
this.user = "";
this.board = "";
}
public Topic(String title,String content,String user,String board,String publishTime)
{
this.title = title;
this.content = content;
this.publishTime = publishTime;
this.user = user;
this.board = board;
}
public void addChildTopic(Topic child)
{
// child.setTitle("re:" + title);
fTopics.add(child);
}
public String toString()
{
StringBuffer sb = new StringBuffer();
if(fTopics.size() != 0)
{
sb.append("标题:").append(title).append("\n");
sb.append("内容:").append(content).append("\n");
sb.append("用户:").append(user).append("\n");
sb.append("回复:").append("\n");
int n = 1;
Iterator<Topic> iterator = fTopics.iterator();
while (iterator.hasNext()) {
Topic topic = (Topic) iterator.next();
sb.append(" ");
sb.append(n++).append("楼:\n");
sb.append(topic.toString());
}
}
else {
sb.append(" ");
sb.append("标题:").append(title).append("\n");
sb.append(" ");
sb.append("内容:").append(content).append("\n");
sb.append(" ");
sb.append("用户:").append(user).append("\n");
}
return sb.toString();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getPublishTime() {
return publishTime;
}
public void setPublishTime(String publishTime) {
this.publishTime = publishTime;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getBoard() {
return board;
}
public void setBoard(String board) {
this.board = board;
}
private String title,content,publishTime;
private String user,board;
private ArrayList<Topic> fTopics = new ArrayList<Topic>();
}
import java.util.Iterator;
/**
*
*/
/**
* @author Think
*
*/
public class TestTopics {
/**
*
*/
public TestTopics() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Topic parentTopic = new Topic("报道帖","初来乍到请各位多多指教!","张三","","");
Topic child1 = new Topic();
Topic child2 = new Topic();
Topic child3 = new Topic();
child1.setContent("欢迎,欢迎,热烈欢迎");
child1.setTitle("re:" + parentTopic.getTitle());
child1.setUser("李四");
child2.setContent("路过,我是来打酱油的");
child2.setTitle("re:" + parentTopic.getTitle());
child2.setUser("马五");
child3.setContent("板凳哦!~~~");
child3.setTitle("哈哈");
child3.setUser("王六");
parentTopic.addChildTopic(child1);
parentTopic.addChildTopic(child2);
parentTopic.addChildTopic(child3);
System.out.println(parentTopic.toString());
}
}
class Topic
{
public Topic()
{
this.title = "";
this.content = "";
this.publishTime = "";
this.user = "";
this.board = "";
}
public Topic(String title,String content,String user,String board,String publishTime)
{
this.title = title;
this.content = content;
this.publishTime = publishTime;
this.user = user;
this.board = board;
}
public void addChildTopic(Topic child)
{
// child.setTitle("re:" + title);
fTopics.add(child);
}
public String toString()
{
StringBuffer sb = new StringBuffer();
if(fTopics.size() != 0)
{
sb.append("标题:").append(title).append("\n");
sb.append("内容:").append(content).append("\n");
sb.append("用户:").append(user).append("\n");
sb.append("回复:").append("\n");
int n = 1;
Iterator<Topic> iterator = fTopics.iterator();
while (iterator.hasNext()) {
Topic topic = (Topic) iterator.next();
sb.append(" ");
sb.append(n++).append("楼:\n");
sb.append(topic.toString());
}
}
else {
sb.append(" ");
sb.append("标题:").append(title).append("\n");
sb.append(" ");
sb.append("内容:").append(content).append("\n");
sb.append(" ");
sb.append("用户:").append(user).append("\n");
}
return sb.toString();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getPublishTime() {
return publishTime;
}
public void setPublishTime(String publishTime) {
this.publishTime = publishTime;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getBoard() {
return board;
}
public void setBoard(String board) {
this.board = board;
}
private String title,content,publishTime;
private String user,board;
private ArrayList<Topic> fTopics = new ArrayList<Topic>();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询