继承问题

时间:2013-03-21 03:42:45

标签: java inheritance

我是一门Java入门课程,正在学习继承。我必须在超类代码中创建一个新方法(getUserName),该方法已经编写,以使私有用户名变量公开,并且编译并运行良好:

import java.util.ArrayList;

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.");
        }
    }
    public String getUserName()
    {
        return username;
    }

    /**
     * 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";
        }
    }
}

但是当我尝试在子类中调用此方法(在printShortSummary方法中)时,它将无法编译:

`enter code here`import java.util.ArrayList;
`enter code here`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;
    }
    public String printShortSummary;
    {
        System.out.println("Message post from " + getUserName);

    }
    /**
     * Return the text of this post.
     * 
     * @return The post's message text.
     */
    public String getText()
    {
        return message;
    }
}

4 个答案:

答案 0 :(得分:2)

System.out.println("Message post from " + getUserName);

应该是

System.out.println("Message post from " + getUserName());

getUserName()是一种方法,即使它没有参数,也需要在它的末尾加上括号。

答案 1 :(得分:2)

更改

public String printShortSummary;
{
    System.out.println("Message post from " + getUserName);

}

public String printShortSummary()
{
    String username;
    System.out.println("Message post from " + getUserName());
    username = getUsername();
    return username;
}

规则

您的方法printShortSummary()必须返回一个字符串值。当你调用一个方法时不要忘记括号()。最终你的错误大多是一个错字,我建议你使用像Netbeans或Eclipse这样的IDE,这样就可以轻松纠正错误。

答案 2 :(得分:1)

按如下方式调用方法(带括号):

System.out.println("Message post from " + getUserName());

此外,您必须使printShortSummary方法无效或返回一些字符串。

答案 3 :(得分:1)

你应该称它为方法人, 像这样称呼它

System.out.println("Message post from " + getUserName());