2014-10-19 21 views
0

我最近不得不爲我的大學編程課程編寫一個程序。幾個小時後,我終於完成了它,一切正常(所以我認爲)。該程序實際上應該包含兩個類之一(主)BookTest應該創建一個對象,然後調用Book類的所有方法。問題是屬性(標識,標題,作者,dateOfPublication)不應該是靜態的。有沒有辦法使它的工作,而不是屬性是靜態的。這是我得到錯誤的兩個類,因爲我沒有屬性static.Everywhere哪裏我的屬性出現在方法它給我的錯誤不能靜態引用靜態方法methodname()從類型Book.Every幫助是非常感謝!何時使用靜態屬性?

import java.util.Date; 
import java.util.Scanner; 
import java.text.*; 

public class Book 
{ 
private int id; 
private String title; 
private String author; 
private Date dateOfPublication; 

public static final String DATE_FORMAT = "dd.MM.yyyy"; 

//--- constructors --- 
public Book(int ID,String TITLE,String AUTHOR,Date DATEOFPUBLICATION){ 
    setId(ID); 
    setTitle(TITLE); 
    setAuthor(AUTHOR); 
    setDateOfPublication(DATEOFPUBLICATION); 
    } 
    public Book() { 
} 

    /** Returns the age of the book in days since publication */ 
    public int age(){ 
    Date date = new Date(); 
    long difference = date.getTime() - Book.getDateOfPublication().getTime(); 
    long differenceDays = difference/(1000 * 60 * 60 * 24); 
    return (int) differenceDays; //this is to avoid compiler errors, replace it! 
    } 
    /** Returns a String representation of the book */ 
    public String toString(){ 
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy"); 
    String dateAsString = simpleDateFormat.format((Book.getDateOfPublication())); 
    String Documentation; 
    Documentation = String.valueOf(Book.getId())+", " + Book.getTitle()+", " + Book.getAuthor()+",  "  + dateAsString;  
    return Documentation; //this is to avoid compiler errors, replace it! 
} 
/** Reads all book data from user input */ 
public void input(){ 
    Scanner scn = new Scanner(System.in); 
    System.out.println("Please enter id: "); 
    Book.setId(scn.nextInt()); 
    scn.nextLine(); 
    System.out.println("Please enter title: "); 
    Book.setTitle((scn.nextLine())); 
    System.out.println("Please enter author: "); 
    Book.setAuthor(scn.nextLine()); 
    System.out.println("Please enter Date of Publication: "); 
    String string_dateOfPublication = scn.nextLine(); 
    Book.setDateOfPublication(stringToDate(string_dateOfPublication)); 
    scn.close(); 
    } 
    //--- Get-/Set-methods --- 


    public int getId() { 
    return id; 
} 
public void setId(int id) { 
    this.id = id; 
} 
//--- helper methods -- DO NOT CHANGE ------------------------------------ 
/** Converts the Date object d into a String object */ 
public static String dateToString(Date d) 
{ 
    SimpleDateFormat fmt = new SimpleDateFormat(DATE_FORMAT); 
    return fmt.format(d); 
} 

/** Converts the String object s into a Date object */ 
public static Date stringToDate(String s) 
{ 
    Date r = null; 
    try { 
     SimpleDateFormat fmt = new SimpleDateFormat(DATE_FORMAT); 
     r = fmt.parse(s); 
    } catch (ParseException e){ 
     System.err.println(e); 
     System.exit(1); 
    } 
    return r; 
} 
public String getTitle() { 
    return title; 
} 
public void setTitle(String title) { 
    this.title = title; 
} 
public String getAuthor() { 
    return author; 
} 
public void setAuthor(String author) { 
    this.author = author; 
} 
public Date getDateOfPublication() { 
    return dateOfPublication; 
} 
public void setDateOfPublication(Date dateOfPublication) { 
    this.dateOfPublication = dateOfPublication; 
} 
} 



public class BookTest{ 

public static void main (String []args){ 

    Book Bookobj = new Book(); 
    Bookobj.input(); 
    System.out.println(Book.age()+" Tage alt."); 
    System.out.println(Bookobj.toString()); 

    } 

} 

回答

1

您應該閱讀約static members。一個(簡化)的方式來思考它是一個非靜態成員(字段,方法)是由實例提供的,而靜態成員是由提供的靜態成員。

在您的Book類案例中,每本書都有一個名稱,作者等屬於特定書籍(實例)的屬性。那麼有意義的是,它們應該是非靜態的。他們的getter和setter方法以及所有其他實例特定的方法也是如此。

轉換字符串和日期的方法並非特定於實例。類Book提供它們。因此,他們被稱爲

Book.stringToDate(...); 

這是有道理的,因爲轉換不是書特有的功能\屬性。

在你main,您創建的Book一個實例:

Book bookObj = new Book(); // instance names should start with a lowercase 

,然後你想要用戶爲這個特定本書輸入數據,所以你需要調用

bookObj.input(); // Not Book.input() 

之後的操作也是如此。

裏面的類定義(讓我們來看看input),您呼叫的非靜態setter方法,就好像它們是靜態的:

Book.setId(...); 

事實上,你要設置的ID爲這個特定實例。所以,你需要寫的是

this.setId(...); 

在此,關鍵字this給予你在(Book在這種情況下)的類的當前實例的引用。請注意,您可以刪除關鍵字this

setId(...); 

的方法的調用會自動暗示。

+0

隨着你的答案我能弄明白。現在我明白了爲什麼我的屬性不應該是靜態的,以及如何改變其他代碼,以便它的工作。感謝一百萬! – Kisos 2014-10-20 08:53:00

1

你正在通過類名調用你的getters和setter,這就是你將如何調用靜態方法。由於您是通過類的名稱來調用它們,因此編譯器正在尋找名爲Book OUTSIDE的靜態類,因爲您是從類本身內部調用它們的,所以不需要做爲編譯器已經知道在哪裏尋找這些方法。當從同一個類中調用類方法時,只需鍵入方法名稱即可。

getDateOfPublication()