2013-03-21 228 views
-1

我在介紹性的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; 
    } 
} 
+0

經過一番編輯,這沒有任何意義,是printShortMemory的一種方法? – 2013-03-21 03:45:24

+0

你得到的錯誤是什麼 – PSR 2013-03-21 03:46:01

+0

錯誤你沒有在方法中返回一個字符串! – smk 2013-03-21 03:46:39

回答

1

調用方法爲(括號內用)如下:

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

而且,你要麼必須做出個e printShortSummary方法無效或返回一些字符串。

1

你應該把它作爲方法的人, 這樣稱呼它

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

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

應該

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

getUserName()是一種方法,即使它沒有參數,也需要在其末尾加括號。

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,這樣就可以很容易地糾正錯誤。

+0

感謝所有人的幫助!我仍然很綠(顯然) – user2193513 2013-03-22 02:02:47