满足多态的基本条件
1、要有继承
2、要有重写
3、父类引用指向子类对象
/*
* 多态例子*/
//定义游戏抽象类
abstract class gameObject {
String gameName;
abstract String getGameName();
}
//红警游戏
class redAlert extends gameObject {
String gameName = "red Alert";
String getGameName(){
return gameName;
}
}
//帝国时代游戏
class ageEmpires extends gameObject {
String gameName = "Age of Empres";
String getGameName(){
return gameName;
}
}
//玩游戏
class playGame {
void Play(gameObject game, String name){
System.out.println("玩家"+name+"在玩"+game.getGameName());
}
}
public class Test {
public static void main(String [] args){
playGame p1 = new playGame();
p1.Play(new redAlert(),"张三");
playGame p2 = new playGame();
p2.Play(new redAlert(),"李四");
p2.Play(new ageEmpires(),"李四");
}
}