packagemonsterTestDrive;publicclassMonsterTestDrive{publicstaticvoidmain(String[]args){finalMonster[]ma=newMonster[3];ma[0]=newVampire();//报错,没有任何类型MonsterTestDr...
package monsterTestDrive;
public class MonsterTestDrive {
public static void main(String[] args){
final Monster[] ma = new Monster[3];
ma[0] = new Vampire();//报错,没有任何类型MonsterTestDrive的外层示例可访问
ma[1] = new Dragon();
ma[3] = new Monster();
for(Monster m : ma)
m.frighten();
}
package monsterTestDrive;
public class MonsterTestDrive {
public static void main(String[] args){
final Monster[] ma = new Monster[3];
ma[0] = new Vampire();
ma[1] = new Dragon();
ma[3] = new Monster();
for(Monster m : ma)
m.frighten();
}
class Monster{
boolean frighten() {
System.out.println("arrgh");
return true;
}
}
class Vampire extends Monster{
boolean frighten() {
System.out.println("a bite?");
return true;
}
}
class Dragon extends Monster{
boolean frighten() {
System.out.println("breath fire");
return true;
}
}
}
展开