day06--面向对象的案例

This commit is contained in:
2026-09-21 13:49:19 +08:00
parent 7add132b4b
commit aae9ed021b
2 changed files with 83 additions and 0 deletions
+56
View File
@@ -15,4 +15,60 @@ package com.inmind.javabean04;
*/
public class Book {
private String bid;
private String bName;
private double price;
private String date;
public Book() {
}
public Book(String bid, String bName, double price, String date) {
this.bid = bid;
this.bName = bName;
this.price = price;
this.date = date;
}
public String getBid() {
return bid;
}
public void setBid(String bid) {
this.bid = bid;
}
public String getbName() {
return bName;
}
public void setbName(String bName) {
this.bName = bName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public void showBook() {
System.out.println("图书信息{" +
"图书编号='" + bid + '\'' +
", 书名='" + bName + '\'' +
", 价格=" + price +
", 日期='" + date + '\'' +
'}');
}
}
@@ -0,0 +1,27 @@
package com.inmind.javabean04;
/*
*定义测试类,使用满参构造方法,创建三(多)个Book对象,
* 判断价格最贵的图书(存放书的数组的遍历),并输出图书信息。
*/
public class Test05 {
public static void main(String[] args) {
Book book1 = new Book("b001", "java从入门到入土", 99.9, "2020-01-01");
Book book2 = new Book("b002", "数据库删库跑路", 69.9, "2023-01-01");
Book book3 = new Book("b003", "java编程思想", 199.9, "2025-01-01");
//定义一个存放书的数组
Book[] bookArr = {book1,book2,book3};
//判断哪本书最贵
Book maxBook = bookArr[0];
//遍历数组,判断谁最贵
for (int i = 0; i < bookArr.length; i++) {
//获取对应索引的书对象
Book temp = bookArr[i];
if (temp.getPrice() > maxBook.getPrice()){
maxBook = temp;
}
}
//调用展示书的方法即可
maxBook.showBook();
}
}