抽象:我的認知是提前描述需要重載的眾多方法。每一個對象重載的內(nèi)容不一樣,但是類型一樣,這樣就可以使用抽像類來將這些內(nèi)容進行封裝。
abstract class People {
People() {
System.out.println("我是人");
}
abstract void sleep();
abstract void eat();
}
class Student extends People {
public void sleep() {
System.out.println("我要睡覺");
}
public void eat() {
System.out.println("睡好覺了,我要吃飯");
}
}
class Student2 extends People {
public void sleep() {
System.out.println("我想吃過飯再睡覺");
}
public void eat() {
System.out.println("睡好覺了,我還想吃飯");
}
}
public class Demo {
public static void main(String[] args) {
Student s = new Student();
s.sleep();
s.eat();
Student2 s2 = new Student2();
s2.sleep();
s2.eat();
}
}
抽象類中可以沒有抽象方法,但是有抽象方法的類一定是抽象類。
抽象類一定是父類,但不一定是頂層父類。
抽象類不能夠創(chuàng)建對象,但是有構(gòu)造方法,為子類對象初始化使用的。
抽象關(guān)鍵字不能與static final private共存