我最近不得不为我的大学编程课程编写一个程序。几个小时后,我终于完成了它,一切正常(所以我认为)。该程序实际上应该包含两个类之一(主)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());
}
}
随着你的答案我能弄明白。现在我明白了为什么我的属性不应该是静态的,以及如何改变其他代码,以便它的工作。感谢一百万! – Kisos 2014-10-20 08:53:00