编写Book.java

Python017

编写Book.java,第1张

public class Book {

private String title

private Date pdate

private int words

public double price(){

日期这里用一个类处理比较简单,不知道你用没用过

SimpleDateFormat sdf = new SimpleDateFormat("MM")

month = sdf.format(pdate)

double xs = 0d

if (month <= 6){

xs = 1.2

} else {

xs = 1.17

}

return words / (1000 * 4.5) * xs

}

}

这里不好空格,你自己格式化一下吧。

get/set方法没写

自己定义个main方法测一下吧

public class Book {

private String title

private String author

private int sell

public Book(){

}

public Book(String title,String author,int sell){

this.title=title

this.author=author

this.sell=sell

}

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 int getSell() {

return sell

}

public void setSell(int sell) {

this.sell = sell

}

public String toString() {

return "D [title=" + title + ", author=" + author + ", sell=" + sell

+ "]"

}

}

Book是一个类型,book为Book类型的一个对象

java是面向对象的语言

Book是一个自定义的类型(因为系统没有提供一个这样的类)

需要手动去定义后才能使用

定义方式:

class Book{

....

}

存在定义后,就可以声明 Book类型的对象,就如 Book book 这样

当然,如果要使用对象还需要将对象实例化

方法:

Book book = new Book()