package com.lq003;
import java.util.Scanner;
class Book {
String title;
String author;
double price;
public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
public Book() {
this.title =" default";
this.author = "noname";
this.price =50.0;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "("+title+","+author+","+price+")";
}
}
public class BookApp {
public static void main(String[] args) {
Book book;
Scanner scan=new Scanner(System.in);
int sel;
sel=scan.nextInt();
switch (sel){
case 1:
book=new Book();
book.setAuthor("ligang");
book.setTitle("java");
book.setPrice(101.8);
System.out.println("Compile pass!");
break;
case 2:
new Book("java", "lixianghua", 89.9);
System.out.println("params constructor pass");
break;
case 3:
book=new Book();
book.setAuthor("Ligang");
book.setTitle("JAVA");
book.setPrice(101.8);
System.out.println(book.getAuthor()+","+book.getTitle()+","+book.getPrice());
break;
case 4:
book=new Book();
System.out.println(book);
break;
case 5:
book=new Book("C++","Tanhaoqiang",80);
System.out.println(book);
break;
}
scan.close();
}
}
Java Book类
最新推荐文章于 2024-04-19 16:54:44 发布