实体类:
实体类相当于一个容器,用来装具有多个属性的内容,当使用者使用的时候直接将整个容器 调用即可
public class Student {
//属性
private int stuId;
private String stuName;
private String stuSex;
private int stuAge;
private String stuAddr;
//IDEA自动构造代码快捷键: alt + insert
有参/无参
有无参构造方法:
当没有在类中创建构造方法时,系统会创建一个空的无参构造方法,不然无法new一个 类;当创建带参构造方法时,系统就不会自动提供无参构造方法,但是不同的是
无参构造的作用是:实例化一个对象
参构造的作用是:初始化类中的属性
//有参
public Books(int bookId, String bookName, String bookAuthor, int bookNum, String bookPrice, String introduce) {
this.bookId = bookId;
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookNum = bookNum;
this.bookPrice = bookPrice;
this.introduce = introduce;
}
//无参
public Books() {
}
get/set方法
得到/设置,由于创建实体类的时候属性设置私有,别人无法直接得到它,得到这些属性,使用get;设置这些属性的值,使用set
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public int getBookNum() {
return bookNum;
}
public void setBookNum(int bookNum) {
this.bookNum = bookNum;
}
public String getBookPrice() {
return bookPrice;
}
public void setBookPrice(String bookPrice) {
this.bookPrice = bookPrice;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
toString方法
就是可以把实体类,或者其他类,以字符串或者规定的方式输出,否则输出的将会是地址。
@Override
public String toString() {
return "Books{" +
"bookId=" + bookId +
", bookName='" + bookName + '\'' +
", bookAuthor='" + bookAuthor + '\'' +
", bookNum=" + bookNum +
", bookPrice='" + bookPrice + '\'' +
", introduce='" + introduce + '\'' +
'}';
}
}