Cách sử dụng get và set giá trị cho các thuộc tính trong java
import java.util.Scanner;
public class Book {
private String tile;
private String author;
private int pages;
private double price;
public Book(String tile, String author, int pages, double price) {
this.tile = tile;
this.author = author;
this.pages = pages;
this.price = price;
}
public Book() {
}
public String getTile() {
return tile;
}
public String getAuthor() {
return author;
}
public int getPages() {
return pages;
}
public 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;
}
@Override
public String toString() {
return "Book{" + "tile=" + tile + ", author=" + author + ", pages=" + pages + ", price=" + price + '}';
}
public 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: