混淆扩展和字段

huangapple go评论70阅读模式
英文:

Confusion with extensions and fields

问题

基本上,我应该将 MessagePost 中的 getTimestamp 访问方法和 timestamp 字段移动到 Post 中,但是当我这样做时,我会得到一个 "在...中具有私有访问权限" 的错误。我该如何解决这个问题?我知道我可以将字段改为受保护的,但我认为这不是一个好的做法,我已经尝试使用 getter 和 setter,但是我得到了一个意外的类型错误。

public class MessagePost extends Post
{
    private String username;  // 帖子作者的用户名
    private String message;   // 任意长的多行消息
    
    private int likes;
    private ArrayList<String> comments;

 
    public MessagePost(String author, String text)
    {
        username = author;
        message = text;
        timestamp = System.currentTimeMillis(); // 这里出现了错误
        likes = 0;
        comments = new ArrayList<>();
    }
}

public class Post
{
    private long timestamp;
      
    public Post(){
        this.timestamp=timestamp;
    }
    
    public long getTimeStamp()
    {
        return this.timestamp;
    }
}
英文:

Basically I am meant to move the getTimestamp accessor method and timestamp field from MessagePost to Post, but when I do that I get a "has private access in" error. How can I fix this? I know I could make the field protected but I don't think it's good practise and I've tried using getters and setters but I get an unexpected type error.

public class MessagePost extends Post
{
    private String username;  // username of the post&#39;s author
    private String message;   // an arbitrarily long, multi-line message
    
    private int likes;
    private ArrayList&lt;String&gt; comments;

 
    public MessagePost(String author, String text)
    {
        username = author;
        message = text;
        timestamp = System.currentTimeMillis(); //where im getting the errors
        likes = 0;
        comments = new ArrayList&lt;&gt;();
    }
public class Post
{
    private long timestamp;
  
    public Post(){
        this.timestamp=timestamp;

    }
    public long getTimeStamp()
    {
        return this.timestamp;
    }

}

答案1

得分: 2

private 成员在其所在的类之外是不可访问的。若要从继承类中访问成员,您应该使用 protected 访问修饰符

protected long timestamp;

或者保持成员为私有,然后创建一个受保护的设置器(setter):

private long timestamp;

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

顺便提一下,这看起来像是一个错误:

public Post(){
    this.timestamp = timestamp;
}

它将字段设置为其本身。也许您想在构造函数中包含一个参数:

public Post(long timestamp){
    this.timestamp = timestamp;
}

在这种情况下,您可能根本不需要任何 setter 或 protected 成员,因为 MessagePost 构造函数需要将 timestamp 值传递给 Post 构造函数。也许可以像这样处理:

public MessagePost(String author, String text) {
    super(System.currentTimeMillis());
    username = author;
    message = text;
    likes = 0;
    comments = new ArrayList<>();
}
英文:

private members are not accessible outside of the class in which they're defined. To access members from an inheriting class, you're looking for the protected access modifier:

protected long timestamp;

Or perhaps keep the member private and make a protected setter:

private long timestamp;

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

<hr />

Side note... This looks like a bug:

public Post(){
    this.timestamp=timestamp;
}

It's setting the field to itself. Perhaps you meant to include a parameter on that constructor:

public Post(long timestamp){
    this.timestamp=timestamp;
}

In this case you might not need any setter or protected member at all, as the MessagePost constructor would need to pass along the timestamp value to the Post constructor. Perhaps something like this:

public MessagePost(String author, String text)
{
    super(System.currentTimeMillis());
    username = author;
    message = text;
    likes = 0;
    comments = new ArrayList&lt;&gt;();
}

答案2

得分: 0

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

// 在 Post 类中
public Post() {
    this.timestamp = System.currentTimeMillis();
}

// 在 MessagePost 类中
public MessagePost(String author, String text) {
    // 这里隐式调用了 Post(),时间戳被初始化
    username = author;
    message = text;
    likes = 0;
    comments = new ArrayList<>();
}
英文:

The MessagePost constructor shouldn't initialize timestamp. Since it belongs to Post, its constructor is in charge of initializing it:

// 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&lt;&gt;();
}

答案3

得分: 0

将这行代码移到Post的构造函数中,替换掉那里不太合理的代码行(根据@David的附注)。毕竟你必须将timestamp初始化为某个值。最合理的方式是最初将其设置为当前时间。

如果你想稍后将timestamp设置为其他值,可以在Post类中添加一个用于设置它的setter方法。

英文:

Just move this line:

timestamp = System.currentTimeMillis(); //where im getting the errors

from MessagePost's constructor to Post's constructor, replacing the line there that doesn't quite make sense (per @David's side note). You have to initialize timestamp to something after all. What makes more sense than to set it to the current time initially.

If you want to set timestamp to something else later, add a setter to the Post class for it.

huangapple
  • 本文由 发表于 2020年8月21日 23:30:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63525764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定