JAVA学习笔记
01.JAVA基础
1.1多态:
多态的前提:有类的继承;在子类中存在对父类方法的重写,
多态的优势:提高了程序的扩展性和可维护性。
实例代码:
public class Test06_Doutai {
public static void main(String[] args) {
//多态(向上造型):父类引用指向子类对象
Animal d = new Dog();
//编译看左边,运行看右边
d.run();
Animal c = new Cat();
c.run();
}
}
//创建夫类
class Animal{
public void run() {
System.out.println("Animal is running!");
}
}
//创建子类
class Dog extends Animal{
//重新父类方法
public void run() {
System.out.println("Dog is running!");
}
}
//创建子类
class Cat extends Animal{
//重写父类方法
public void run() {
System.out.println("Cat is running!");
}
}