- 面向对象最重要的就是多态,我们都知道接口和抽象不能被实例化,但是它们可以创建一个指向自己的对象引用,它们的实现类或子类就在充当这样的角色,我想这就是面向对象编程中多态的优势。
- 什么是指向自己对象的引用呢?
public class test {
public static void main(String[] args) {
Animal a2=new bird();//指向自己对象的引用
a2.shout();
a2.guaid();//出错这时就不能被引用子类中的方法了
}
}
abstract class Animal{
String name;
public void shout(){
System.out.println("叫声");
}
}
class bird extends Animal{
public void guard(){
System.out.println("我是美丽的小鸟");
}
}
- 在这里就相当于
List<Character> list = new ArrayList<>();
的用处。