day06-面向对象的案例
This commit is contained in:
@@ -0,0 +1,65 @@
|
|||||||
|
package com.inmind.test04;
|
||||||
|
/*
|
||||||
|
定义一个图书Book类。
|
||||||
|
|
||||||
|
- 属性:图书编号,书名,价格,出版日期
|
||||||
|
- 构造方法:
|
||||||
|
- 无参构造方法,满参构造方法
|
||||||
|
- 成员方法:
|
||||||
|
- get/set方法
|
||||||
|
- showBook方法,输出图书信息
|
||||||
|
*/
|
||||||
|
public class Book {
|
||||||
|
//属性:图书编号,书名,价格,出版日期
|
||||||
|
private String bookId;
|
||||||
|
private String name;
|
||||||
|
private double price;
|
||||||
|
private String publishDate;
|
||||||
|
|
||||||
|
//无参构造方法:创建书对象,对属性赋了默认值
|
||||||
|
public Book() {
|
||||||
|
}
|
||||||
|
//满参构造方法:创建书对象,并给所有属性赋值
|
||||||
|
public Book(String bookId, String name, double price, String publishDate) {
|
||||||
|
this.bookId = bookId;
|
||||||
|
this.name = name;
|
||||||
|
this.price = price;
|
||||||
|
this.publishDate = publishDate;
|
||||||
|
}
|
||||||
|
//get/set方法
|
||||||
|
public String getBookId() {
|
||||||
|
return this.bookId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBookId(String bookId) {
|
||||||
|
this.bookId = bookId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return this.price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPublishDate() {
|
||||||
|
return this.publishDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPublishDate(String publishDate) {
|
||||||
|
this.publishDate = publishDate;
|
||||||
|
}
|
||||||
|
//自定义成员方法
|
||||||
|
public void showBook(){
|
||||||
|
System.out.println("编号:"+this.bookId+",书名:"+this.name+",价格:"+this.price+",出版日期:"+this.publishDate);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
package com.inmind.test04;
|
package com.inmind.test04;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 最贵的书。
|
* 最贵的书。
|
||||||
* 定义一个图书Book类。
|
* 定义一个图书Book类。
|
||||||
@@ -13,4 +15,29 @@ package com.inmind.test04;
|
|||||||
* 定义测试类,使用满参构造方法,创建三(多)个Book对象,判断价格最贵的图书(存放书的数组的遍历),并输出图书信息。
|
* 定义测试类,使用满参构造方法,创建三(多)个Book对象,判断价格最贵的图书(存放书的数组的遍历),并输出图书信息。
|
||||||
*/
|
*/
|
||||||
public class Test05 {
|
public class Test05 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//使用满参构造方法,创建三(多)个Book对象
|
||||||
|
Book b1 = new Book("B001", "java从入门到崩溃", 99.9, "2020-01-01");
|
||||||
|
Book b2 = new Book("C003", "数据库从删库到跑路", 59.9, "2022-01-01");
|
||||||
|
Book b3 = new Book("D005", "python-AI大模型开发", 299.9, "2025-01-01");
|
||||||
|
//判断价格最贵的图书(存放书的数组的遍历)
|
||||||
|
//定义一个存放图书的数组
|
||||||
|
// Book[] bookArr = new Book[]{b1,b2,b3};
|
||||||
|
Book[] bookArr = {b1,b2,b3};
|
||||||
|
//遍历数组,求最贵的图书
|
||||||
|
Book maxBook = bookArr[0];
|
||||||
|
for (int i = 1; i < bookArr.length; i++) {
|
||||||
|
//获取当前遍历的图书
|
||||||
|
Book book = bookArr[i];
|
||||||
|
/*double currentPrice = book.getPrice();
|
||||||
|
double maxBookPrice = maxBook.getPrice();
|
||||||
|
if (currentPrice > maxBookPrice){*/
|
||||||
|
if (book.getPrice() > maxBook.getPrice()){
|
||||||
|
//将当前的书,修改为,最贵的书
|
||||||
|
maxBook = book;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//遍历结束,最贵的书就是maxBook
|
||||||
|
maxBook.showBook();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user