Java接口(3)与图书管理系统
抽象类与接口的区别
1.抽象类包含普通类和抽象方法,子类可以直接调用普通类方法不用重写。接口包含抽象方法和全局变量。
2.抽象类有各种权限,接口只有pubilc。
3.子类使用抽象类用extend,使用接口用implement。
4.一个抽象类可以实现若干个接口,接口不能继承抽象类,但是接口可以继承多个接口
5.一个类只能继承一个抽象类,一个子类可以实现多个接口。
Object类
Object类没有父类,可以引用所有类的对象。
tostring和equal方法也可以通过Object来进行重写。
equal如果比较的是引用类型则比较的是地址
图书管理系统
我们需要实现一个程序输入姓名,身份,从而来管理图书。
Book类
package book;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 45
* Date: 2025-03-05
* Time: 15:03
*/
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed;
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed)==true ? " 已经被借出" : " 还没有被借出") +
/* ", isBorrowed=" + isBorrowed +*/
'}';
}
}
对书的属性进行设置用封装的方法来封装成员变量,增强安全性,再添加get,set方法实现对成员变量的更改。
Booklist类
package book;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 45
* Date: 2025-03-05
* Time: 15:08
*/
public class BookList {
public Book[] books = new Book[10];
private int useSize;
public BookList(){
this.books[0] = new Book("三国演义","罗贯中",10,"小说");
this.books[1] = new Book("西游记","吴承恩",23,"小说");
this.books[2] = new Book("红楼梦","曹雪芹",41,"小说");
this.useSize = 3;
}
public Book getbook(int pos){
return books[pos];
}
public void setBooks(int pos,Book book){
books[pos] = book;
}
public int getUseSize() {
return useSize;
}
public void setUseSize(int useSize) {
this.useSize = useSize;
}
}
我们首先以数组的方式将对象存储起来,并提前存储三个对象,用usesize来记录当前书的数目,这十分关键,对数组下标访问提供了方向。同时设置了四个方法一方面是对书的,另一方面是对于UseSize的,因为封装了usesize,这不用多说,getbook和setbook方便后来进行添加和查找,可见方法的重要性。
User类
package user;
import book.BookList;
import operation.*;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 45
* Date: 2025-03-05
* Time: 15:46
*/
public abstract class User {
protected String name;
public IOpearation[] iOpearations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOpeation(int choice, BookList bookList){
iOpearations[choice].work(bookList);
}
}
在刚开始时我们的思路就是有管理员和普通成员两种选择,两大对象都是使用者,肯定会有共性,为了代码的简洁,和对代码的复用,我们选择创造一个父类,而这个父类设置为抽象类,可以为程序加上一层检验,因为我们真正面向的对象是Normaluser和Adamnuser,所以为了防止我们实例化父类,所以设置为抽象类。
再来到类中,我们发现用protected来修饰了name,它不仅能在包中用还能在包外同一类中用,这一限制修饰符用的很好,我们知道不同的身份所拥有的功能也不一样,所以我们通过数组来存储功能,具体大小得看身份,这也正应对了身份不同功能不同。
doOpeation给方法提供了渠道,通过choice来选择功能,然后选择对应的重写方法。