首先,Book类你不能也来个public关键字进行声明。其次,你在main()中没有对Book类的Page和Price成员进行初始化,当然就输出默认值0了。以下是我修改后的代码:
import java.util.Date
class Book
{
private String name;
private String id;
private String editor;
private String publisher;
private Date date;
private int page;
private double price;
private String BookName;
public Book()
{
this.name="java";
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public String getEditor()
{
return editor;
}
public void setEditor(String editor)
{
this.editor=editor;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id=id;
}
public String getPublisher()
{
return publisher;
}
public void setPublisher(String publisher)
{
this.publisher="清华大学";
}
public Date getDate()
{
return date;
}
public void setdate(Date date)
{
this.date=date;
}
public int getPage()
{
return page;
}
public void setPage(int page)
{
if(page==200)
{
this.page=200;
}else
{
System.out.println("图书输入信息错误");
}
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price=200;
}
}
public class Test
{
public static void main(String[] args)
{
Book book =new Book();
book.setName("java");
book.setId("23");
book.setEditor("徐琪");
book.setPublisher("清华大学");
book.setdate(new Date());
//**************
book.setPage(200);
book.setPrice(200);
//**************
System.out.println("name"+book.getName());
System.out.println("id"+book.getId());
System.out.println("editor"+book.getEditor());
System.out.println("publisher"+book.getPublisher());
System.out.println("date"+book.getDate());
System.out.println("page"+book.getPage());
System.out.println("price"+book.getPrice());
}
}
特别注意上面代码中*号之间的那两句代码。