如何创建子类使其不使用超类的所有属性?

问题描述

我在练习堆栈溢出的低级设计时遇到了一个类结构

public class Post {
    public int postId;
    public PostBody postbody;
    public Date createdAt;
    public User user;
    public int upVotes;
    public int downVotes;
    public List<Comment> comments;
}

public class Question extends Post {
    public List<Answer> answer;
    public Topic topic;
    public List<Tag> tags;
}

public class Answer extends Post {
    public Bool isAcceptedAnswer;
}

public class PostBody {
    public String header;
    public String body;
    public String code;
}

public class Comment extends Post {
     public String comment;
}

这里,Comment 类扩展了 Post 类。或者,我可以考虑 Post 类中已经定义的 PostBody 类,或者我可以在 Comment 类中创建一个简单的 String。

  1. 我们可以实现一个空类吗?如果不是,那么我应该如何实现 Comment 类?
  2. 如果我要添加一个字符串的评论属性,如何处理我从 Post 类继承的 PostBody 属性

总的来说,我如何实现 Comment 类?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)