Java 与扩展名和字段混淆

Java 与扩展名和字段混淆,java,Java,基本上,我打算将getTimestamp访问器方法和timestamp字段从MessagePost移动到Post,但当我这样做时,我会得到一个“has private access in”错误。我怎样才能解决这个问题?我知道我可以保护字段,但我认为这不是一个好的练习,我尝试过使用getter和setter,但我遇到了一个意外的类型错误 public class MessagePost extends Post { private String username; // username

基本上,我打算将getTimestamp访问器方法和timestamp字段从MessagePost移动到Post,但当我这样做时,我会得到一个“has private access in”错误。我怎样才能解决这个问题?我知道我可以保护字段,但我认为这不是一个好的练习,我尝试过使用getter和setter,但我遇到了一个意外的类型错误

public class MessagePost extends Post
{
    private String username;  // username of the post's author
    private String message;   // an arbitrarily long, multi-line message
    
    private int likes;
    private ArrayList<String> comments;

 
    public MessagePost(String author, String text)
    {
        username = author;
        message = text;
        timestamp = System.currentTimeMillis(); //where im getting the errors
        likes = 0;
        comments = new ArrayList<>();
    }

private
成员在其定义的类之外无法访问。要从继承类访问成员,您需要查找受保护的

或者让成员保持私有,并使其成为受保护的setter:

private long timestamp;

protected void setTimeStamp (long time) {
    this.timestamp = time;
}

旁注。。。这看起来像一个bug:

public Post(){
    this.timestamp=timestamp;
}
它将字段设置为自身。也许您想在该构造函数中包含一个参数:

public Post(long timestamp){
    this.timestamp=timestamp;
}
在这种情况下,您可能根本不需要任何setter或
protected
成员,因为
MessagePost
构造函数需要将
时间戳
值传递给
Post
构造函数。也许是这样的:

public MessagePost(String author, String text)
{
    super(System.currentTimeMillis());
    username = author;
    message = text;
    likes = 0;
    comments = new ArrayList<>();
}
public MessagePost(字符串作者,字符串文本)
{
super(System.currentTimeMillis());
用户名=作者;
信息=文本;
likes=0;
注释=新的ArrayList();
}

MessagePost构造函数不应初始化时间戳。由于它属于
Post
,它的构造函数负责初始化它:

// In the Post class
public Post() {
    this.timestamp = System.currentTimeMillis();
}

// In the MessagePost class
public MessagePost(String author, String text) {
    // Post() is implicitly called here, and timestamp is initiazlied
    username = author;
    message = text;
    likes = 0;
    comments = new ArrayList<>();
}
//在Post类中
公职人员职位(){
this.timestamp=System.currentTimeMillis();
}
//在MessagePost类中
public MessagePost(字符串作者、字符串文本){
//这里隐式调用Post(),并初始化时间戳
用户名=作者;
信息=文本;
likes=0;
注释=新的ArrayList();
}
只需移动这一行:

timestamp = System.currentTimeMillis(); //where im getting the errors
从MessagePost的构造函数到Post的构造函数,替换那里没有意义的行(根据@David的旁注)。毕竟,您必须初始化
时间戳
。什么比最初将其设置为当前时间更有意义


如果以后要将
timestamp
设置为其他内容,请在Post类中为其添加一个setter。

可以将
timestamp
添加为Post构造函数的参数。还可以选择包私有访问。从技术上讲,OP编写它的方式,
Post()
只需将时间戳分配给自身,这似乎不是很有用。@TiiJ7确实,很好的捕获。上下滚动太多,误读了那一行。编辑以使观点更清晰。感谢您的回复!当你说“因为MessagePost构造函数需要将时间戳值传递给Post构造函数”时,我试图理解你的意思。我理解这是Super调用超类的构造函数,所以当你把它放在“System.currentTimeMillis()”中时,你在调用它吗?对不起,再次感谢!
// In the Post class
public Post() {
    this.timestamp = System.currentTimeMillis();
}

// In the MessagePost class
public MessagePost(String author, String text) {
    // Post() is implicitly called here, and timestamp is initiazlied
    username = author;
    message = text;
    likes = 0;
    comments = new ArrayList<>();
}
timestamp = System.currentTimeMillis(); //where im getting the errors