day06-面向对象的案例

This commit is contained in:
2026-07-19 16:31:48 +08:00
parent 1f97875bbc
commit c67a963b0c
2 changed files with 92 additions and 0 deletions
+65
View File
@@ -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);
}
}