Files
javaSE251223/day09/src/com/inmind/abstract06/Demo08.java

29 lines
1.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind.abstract06;
/*
如何使用抽象类和抽象方法?
1.抽象类不能被创建,只能由子类“实现”了抽象类之后才能创建子类对象
2.在子类中必须“实现重写”抽象类中的抽象方法
3.这时创建子类的对象,并调用方法
-------------------------------------------------
注意事项:(在线教程中查看!!!!)
1. 抽象类不能创建对象,如果创建,编译无法通过而报错。只能创建其非抽象子类的对象
2. 抽象类中,可以有构造方法,是供子类创建对象时,初始化父类成员使用的
3. 抽象类中,不一定包含抽象方法,但是有抽象方法的类必定是抽象类
4. 抽象类的子类,必须重写抽象父类中所有的抽象方法,否则,编译无法通过而报错。除非该子类也是抽象类。
---------------------------------------
抽象类的意义
抽象类的意义是对某一些功能添加约束
重写,我已经实现了自己该有的功能,子类,可以在此基础选择性扩展功能
*/
public class Demo08 {
public static void main(String[] args) {
//创建一个动物类对象调用它的eat方法
// Animal animal = new Animal();//错误抽象类不能创建对象
Dog dog = new Dog();
dog.eat();
HelloKitty kitty = new HelloKitty();
kitty.eat();
}
}