Sử dụng getter và setter trong Java
class book {
private String tile;
private String author;
private int pages;
private double price;
book(String tile, String author, int pages, double price) {
this.tile = tile;
this.author = author;
this.pages = pages;
this.price = price;
}
public book() {
}
String getTile() {
return tile;
}
String getAuthor() {
return author;
}
int getPages() {
return pages;
}
double getPrice() {
return price;
}
public void setAuthor(String author) {
this.author = author;
}
public void setTile(String tile) {
this.tile = tile;
}
public void setPages(int pages) {
this.pages = pages;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return "Book{" + "tile=" + this.getTile() + ", author=" + this.getAuthor() +
", pages=" + this.getPages() + ", price=" + this.getPrice() + '}';
}
void updatePrice(double rate) {
this.price = price * (1 + rate);
}
public static void main(String[] args) {
book b = new book();
Scanner scan = new Scanner(System.in);
System.out.println("Please input the title of book: ");
b.setTile(scan.nextLine());
System.out.println("book's author: ");
b.setAuthor(scan.nextLine());
System.out.println("book's total pages: ");
b.setPages(scan.nextInt());
System.out.println("book's price: ");
b.setPrice(scan.nextDouble());
System.out.println("The book's information:" + b.toString());
System.out.println("how much discount?");
b.updatePrice(scan.nextDouble());
System.out.println("After update price, the book's information :"
+ b.toString());
}
}
Trương Đình Huy
» Tin mới nhất:
» Các tin khác: