创建一个Java图书类
答案:2 悬赏:60 手机版
解决时间 2021-01-24 01:48
- 提问者网友:欲劫无渡
- 2021-01-23 17:07
创建一个Java图书类
最佳答案
- 五星知识达人网友:人類模型
- 2021-01-23 17:12
Java程序:
class BookTest {
public static void main(String args[]) {
Book book = new Book("Java", "张三", 30, 2000);
System.out.println("图书总价:" + book.totalPrice());
}
}
class Book {
private String name;
private String author;
private double price;
private int amount;
public Book(String name, String author, double price, int amount) {
this.name = name;
this.author = author;
this.price = price;
this.amount = amount;
}
public String getName() {
return this.name;
}
public String getAuthor() {
return this.author;
}
public double getPrice() {
return this.price;
}
public int getAmount() {
return this.amount;
}
public double totalPrice() {
return this.price * this.amount;
}
}
运行测试:
图书总价:60000.0
class BookTest {
public static void main(String args[]) {
Book book = new Book("Java", "张三", 30, 2000);
System.out.println("图书总价:" + book.totalPrice());
}
}
class Book {
private String name;
private String author;
private double price;
private int amount;
public Book(String name, String author, double price, int amount) {
this.name = name;
this.author = author;
this.price = price;
this.amount = amount;
}
public String getName() {
return this.name;
}
public String getAuthor() {
return this.author;
}
public double getPrice() {
return this.price;
}
public int getAmount() {
return this.amount;
}
public double totalPrice() {
return this.price * this.amount;
}
}
运行测试:
图书总价:60000.0
全部回答
- 1楼网友:掌灯师
- 2021-01-23 18:47
public class TestBook {
public static void main(String [] agrs){
Book book = new Book("Java","张三",30,2000);
System.out.println(book.totalPrice());
}
}
class Book{
private String name;
private String author;
private double price;
private int amount;
//构造器
public Book(String name,String author,double price,int amount){
this.name = name;
this.author = author;
this.price = price;
this.amount = amount;
}
public void setPrice(double price){
this.price = price;
}
public void setPrice(int amount){
this.amount = amount;
}
public double totalPrice(){
return this.price * this.amount;
}
}
运行结果:
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯