package to.abstracts;
public class TestAbstract {
public static void main(String[] args) {
// new Animal();//不该被创建成实例,独立存在
/**
* cannot instantiate the type Animal不能实例化对象
* */
Animal a = new Dog();//构建子类对象时,要先构建父类对象(父类共性 + 子类独有 = 完整的子类对象)
//new Dog()的时候先调用Animal的构造方法,然后自然调用Dog的构造方法,因为Dog类是Animal的子类,所以自然调用super();
a.eat();
a.sleep();
}
}
abstract class Animal{//使用abstract修饰成抽象类,不能new对象(不能独立存在)
//抽象类的含义:不够完整,不够具体,不能独立存在
String breed;//品种
int age;
String sex;
public Animal(){
System.out.println("Animal() Executed");
}
//抽象方法:只有方法声明,没有方法实现,强制子类必须实现该抽象方法,提供完整的、具体的调用版本
public abstract void eat();
public abstract void sleep();
}
class Dog extends Animal{//抽象类,可以被子类继承,可以为子类提供共性
public Dog() {
super();
System.out.println("Dog() Executed");
}
public void eat() {
System.out.println("狗在吃...");
}
public void sleep() {
System.out.println("狗在睡...");
}
}