- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试重构代码,但由于我对编程非常陌生,所以很难知道从哪里开始。
我尝试将方法注释(例如和不像)放在 CommentedPost 类下,但不知道该怎么办。
Post.java
public class Post {
private String username; // username of the post's author
private long timestamp;
private int likes;
private ArrayList<String> comments;
/**
* Constructor for objects of class Post.
*
* @param author The username of the author of this post.
*/
public Post(String author)
{
username = author;
timestamp = System.currentTimeMillis();
likes = 0;
comments = new ArrayList<String>();
}
/**
* Record one more 'Like' indication from a user.
*/
public void like()
{
likes++;
}
/**
* Record that a user has withdrawn his/her 'Like' vote.
*/
public void unlike()
{
if (likes > 0) {
likes--;
}
}
/**
* Add a comment to this post.
*
* @param text The new comment to add.
*/
public void addComment(String text)
{
comments.add(text);
}
/**
* Return the time of creation of this post.
*
* @return The post's creation time, as a system time value.
*/
public long getTimeStamp()
{
return timestamp;
}
/**
* Display the details of this post.
*
* (Currently: Print to the text terminal. This is simulating display
* in a web browser for now.)
*/
public void display()
{
System.out.println(username);
System.out.print(timeString(timestamp));
if(likes > 0) {
System.out.println(" - " + likes + " people like this.");
}
else {
System.out.println();
}
if(comments.isEmpty()) {
System.out.println(" No comments.");
}
else {
System.out.println(" " + comments.size() + " comment(s). Click here to view.");
}
}
/**
* Create a string describing a time point in the past in terms
* relative to current time, such as "30 seconds ago" or "7 minutes ago".
* Currently, only seconds and minutes are used for the string.
*
* @param time The time value to convert (in system milliseconds)
* @return A relative time string for the given time
*/
private String timeString(long time)
{
long current = System.currentTimeMillis();
long pastMillis = current - time; // time passed in milliseconds
long seconds = pastMillis/1000;
long minutes = seconds/60;
if(minutes > 0) {
return minutes + " minutes ago";
}
else {
return seconds + " seconds ago";
}
}
}
CommentedPost.java
public class CommentedPost extends Post {
public CommentedPost(String author) {
super(author);
// TODO Auto-generated constructor stub
}
}
EventPost.java
public class EventPost extends Post{
public EventPost(String author) {
super(author);
// TODO Auto-generated constructor stub
}
}
MessagePost.java
public class MessagePost extends Post{
private String message; // an arbitrarily long, multi-line message
/**
* Constructor for objects of class MessagePost.
*
* @param author The username of the author of this post.
* @param text The text of this post.
*/
public MessagePost(String author, String text)
{
super(author);
message = text;
}
/**
* Return the text of this post.
*
* @return The post's message text.
*/
public String getText()
{
return message;
}
/**
* Display the details of this post.
*
* (Currently: Print to the text terminal. This is simulating display
* in a web browser for now.)
*/
public void display()
{
super.display();
System.out.println(message);
}
}
StartNetwork.java
public class StartNetwork{
/**
* @param args
*/
public static void main( String[] args )
{
MessagePost message = new MessagePost("White Rabbit", "Oh dear, oh dear, I shall be late!");
PhotoPost photo = new PhotoPost("Alice Wonderland", "RabbitHole.jpg" ,"Down the rabbit hole :)");
message.addComment( "Your watch is exactly two days slow." );
photo.like();
NewsFeed news = new NewsFeed();
news.addPost( message );
news.addPost( photo );
news.show();
}
}
PhotoPost.java
public class PhotoPost extends Post{
private String filename; // the name of the image file
private String caption; // a one line image caption
/**
* Constructor for objects of class PhotoPost.
*
* @param author The username of the author of this post.
* @param filename The filename of the image in this post.
* @param caption A caption for the image.
*/
public PhotoPost(String author, String filename, String caption)
{
super(author);
this.filename = filename;
this.caption = caption;
}
/**
* Return the file name of the image in this post.
*
* @return The post's image file name.
*/
public String getImageFile()
{
return filename;
}
/**
* Return the caption of the image of this post.
*
* @return The image's caption.
*/
public String getCaption()
{
return caption;
}
/**
* Display the details of this post.
*
* (Currently: Print to the text terminal. This is simulating display
* in a web browser for now.)
*/
public void display()
{
super.display();
System.out.println(" [" + filename + "]");
System.out.println(" " + caption);
}
}
NewsFeed.java
public class NewsFeed{
private ArrayList<Post> posts;
/**
* Construct an empty news feed.
*/
public NewsFeed()
{
posts = new ArrayList<Post>();
}
/**
* Add a post to the news feed.
*
* @param post The post to be added.
*/
public void addPost(Post post)
{
posts.add(post);
}
/**
* Show the news feed. Currently: print the news feed details
* to the terminal. (To do: replace this later with display
* in web browser.)
*/
public void show()
{
// display all posts
for(Post post : posts) {
post.display();
System.out.println(); // empty line between posts
}
}
}
重构后的代码应该有一个包含以下内容的类图:
发布带有字段用户名和时间戳的帖子
CommentedPost(指向帖子的箭头,即继承自它)带有喜欢和评论的字段
EventPost(指向 Post 的箭头)带事件类型字段
带有字段消息的消息帖子(箭头指向 CommentedPost)
PhotoPost(指向 CommentedPost 的箭头)带字段文件名和标题
最佳答案
帖子包含点赞和评论。您应该将这些字段移至 CommentedPost。
我不确定 eventType 字段是什么,但 EventPost 没有这样的字段。您应该创建它。
根据您的描述,MessagePost 应该扩展 CommentedPost。它不是;相反,它扩展了 Post。您应该更改此设置,使其扩展 CommentedPost。
根据您的描述,PhotoPost 应该扩展 CommentedPost。它不是;相反,它扩展了 Post。您应该更改此设置,使其扩展 CommentedPost。
关于java - 重构代码以反射(reflect)继承层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54524774/
我正在尝试将多个水平链接的 Button 和 TextView 垂直链接为 View 集,但仍保持平面 View 层次结构。这是我的初始布局和代码:
到目前为止,我已经在Google BigQuery上训练了几种模型,目前我需要查看模型的外观(即架构,损失函数等)。 有没有办法获取这些信息? 最佳答案 仔细阅读文档后,我可以说该功能尚不存在。我什至
本文实例讲述了PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)。分享给大家供大家参考,具体如下: 前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个
我是一名优秀的程序员,十分优秀!