测试类中为什么不能用多态测试? 就像这样:Player p = new PingpangPlayer(); 或者Person p = new PingpangPlayer();
//创建说英语接口
interface SpeakEnglish {
public abstract void speakenglish();
}
//抽象人类
abstract class Person {
//姓名,年龄
private String name;
private int age;
public Person() {}
public Person(String name,int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//睡觉
public void sleep() {
System.out.println("人都是要睡觉的");
}
//吃饭
public abstract void eat();
}
//抽象运动员类
abstract class Player extends Person {
public Player() {}
public Player(String name,int age) {
super(name,age);
}
public abstract void eat();
public abstract void study();
}
//抽象教练类
abstract class Coach extends Person {
public Coach() {}
public Coach(String name,int age) {
super(name,age);
}
public abstract void eat();
public abstract void teach();
}
//具体乒乓球运动员类
class PingpangPlayer extends Player implements SpeakEnglish {
public PingpangPlayer() {}
public PingpangPlayer(String name,int age) {
super(name,age);
}
public void eat() {
System.out.println("乒乓球运动员吃榴莲,喝椰汁");
}
public void study() {
System.out.println("乒乓球运动员学发球和接球");
}
public void speakenglish() {
System.out.println("乒乓球运动员说英语");
}
}
//具体篮球运动员类
class BasketballPlayer extends Player {
public BasketballPlayer() {}
public BasketballPlayer(String name,int age) {
super(name,age);
}
public void eat() {
System.out.println("篮球运动员吃牛肉,喝牛奶");
}
public void study() {
System.out.println("篮球运动员学运球和投篮");
}
}
//具体乒乓球教练类
class PingpangCoach extends Coach implements SpeakEnglish {
public PingpangCoach() {}
public PingpangCoach(String name,int age) {
super(name,age);
}
public void eat() {
System.out.println("乒乓球教练吃猪大肠,喝豆汁");
}
public void teach() {
System.out.println("乒乓球教练教发球和接球");
}
public void speakenglish() {
System.out.println("乒乓球教练说英语");
}
}
//具体篮球教练类
class BasketballCoach extends Coach {
public BasketballCoach() {}
public BasketballCoach(String name,int age) {
super(name,age);
}
public void eat() {
System.out.println("篮球教练吃生醉蟹,喝黄酒");
}
public void teach() {
System.out.println("篮球教练教运球和投篮");
}
}
class HomeWork2 {
public static void main(String[] ages) {
//创建具体乒乓球运动员对象
//方式1
PingpangPlayer ppp = new PingpangPlayer();
ppp.setName("马龙");
ppp.setAge(28);
System.out.println(ppp.getName()+"---"+ppp.getAge());
ppp.eat();
ppp.sleep();
ppp.study();
ppp.speakenglish();
//方式2
PingpangPlayer ppp1 = new PingpangPlayer("马龙",28);
System.out.println(ppp1.getName()+"---"+ppp1.getAge());
ppp1.eat();
ppp1.sleep();
ppp1.study();
ppp1.speakenglish();
System.out.println("-----------------");
//创建具体篮球运动员对象
//方式1
BasketballPlayer bp = new BasketballPlayer();
bp.setName("姚明");
bp.setAge(26);
System.out.println(bp.getName()+"---"+bp.getAge());
bp.eat();
bp.sleep();
bp.study();
BasketballPlayer bp1 = new BasketballPlayer("易建联",23);
System.out.println(bp1.getName()+"---"+bp1.getAge());
bp1.eat();
bp1.sleep();
bp1.study();
//bp1.speakenglish();
System.out.println("-----------------");
//创建具体乒乓球教练对象
//方式1
PingpangCoach pc= new PingpangCoach();
pc.setName("刘国梁");
pc.setAge(41);
System.out.println(pc.getName()+"---"+pc.getAge());
pc.eat();
pc.sleep();
pc.teach();
pc.speakenglish();
PingpangCoach pc1= new PingpangCoach("刘国梁",41);
System.out.println(pc1.getName()+"---"+pc1.getAge());
pc1.eat();
pc1.sleep();
pc1.teach();
pc1.speakenglish();
System.out.println("-----------------");
//创建具体篮球教练对象
//方式1
BasketballCoach bc= new BasketballCoach();
bc.setName("阿的江");
bc.setAge(51);
System.out.println(bc.getName()+"---"+bc.getAge());
bc.eat();
bc.sleep();
bc.teach();
BasketballCoach bc1= new BasketballCoach("蒋兴权",61);
System.out.println(bc1.getName()+"---"+bc1.getAge());
bc1.eat();
bc1.sleep();
bc1.teach();
}
}
|
|