Java SE学习笔记-多态

时间:2023-02-25 18:36:31
package com.cute;
/**
 *  多态
 */
public class Polymorphic {

public void run(Car car){
car.run();
}

public static void main(String[] args) {
Polymorphic test = new Polymorphic();
Car BMW = new BMW();
test.run(BMW);//多态--向上类型转换
Car QQ = new QQ();
test.run(QQ);//多态--向上类型转换
}

}


//定义父类
class Car{
public void run(){
System.out.println("car is running.");
}
}


//定义子类
class BMW extends Car{
public void run() {
System.out.println("BMW is running.");
}
}


//定义子类
class QQ extends Car{
public void run(){
System.out.println("QQ is running.");
}
}