面向对象
面向对象的理解:
把数据及对数据的操作方法放在一起,作为一个相互依存的整体——对象。对同类对象抽象出其共性,形成类
面向对象的基本特征:
封装,继承,多态。
封装:
1.把属性私有化,实现了对外界的隐藏,然后通过共有的getter和setter方法对属性进行获取和赋值,保证了操作的安全性,一般属性私有化(private),方法公有化(public),除非方法只是用来内部调用,可以private修饰
2.对属性使用private私有化进行修饰,实现了对外界的隐藏,外界不能够直接操作类的属性
3.定义针对属性的getter和setter方法,完成对属性的获取和设置值,在发放中可以完成完全验证
继承
继承是面向对象软件技术当中的一个概念,与多态、封装共为面向对象的三个基本特征。继承可以使得子类具有父类的属性和方法或者重新定义、追加属性和方法等。
1、子类拥有父类得特征,而父类没有,父类更通用,子类更具体,(特征包括属性和方法,自身的特性,拥有父类没有的)
2、使用extends继承父类,语句格式:class 子类名 extends 父类名
3、父类中一般只定义一般属性和方法(这个一般可以理解为是子类共有的,这就是父类更通用,而子类拥有其他的,所以子类更具体)
4、子类中通过super关键字来调用父构造方法
5、在子类中可以继承父类中,父类中public,protected修饰的属性,方法可以继承,private修饰的属性和方法不能被继承
6、规则: 创建子类对象的时候,首先调用的是父类的无参构造方法创建一个父类对象
7、可以在子类中显示调用父类的有参构造方法
8、如果父类的属性均为private修饰,则可以通过共有的getter,setter方法来调用
多态
多态性,就是一个父类的引用变量,可以指向其任意一个子类对象。
类
类的定义:
类是对相同特性或属性,具有相同行为能力的一类具体事物的描述或称呼
类的属性
类的属性,类的属性按使用范微分为公有属性和私有属性,类的属性范围取决于属性的名称。
公有属性:在类中和类外都能调用的属性。
私有属性:不能在类外以及类以外的函数调用。
package text;
public class Books {
//属性
private int bookId;
private String bookName;
private String bookAuthor;
private int bookNum;
private double bookPrice;
private String introduce;
//构造方法
public Books(){
}
public Books(int bookId, String bookName, String bookAuthor, int bookNum, double bookPrice, String introduce) {
this.bookId = bookId;
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookNum = bookNum;
this.bookPrice = bookPrice;
this.introduce = introduce;
}
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 double getBookPrice() {
return bookPrice;
}
public void setBookPrice(double bookPrice) {
this.bookPrice = bookPrice;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
@Override
public String toString() {
return "school{" +
"bookId=" + bookId +
", bookName='" + bookName + '\'' +
", bookAuthor='" + bookAuthor + '\'' +
", bookNum=" + bookNum +
", bookPrice=" + bookPrice +
", introduce='" + introduce + '\'' +
'}';
}
public static void main(String[]args){
Books b1 = new Books();//无参的构造方法
System.out.println(b1);
}
}