老師學生類案例作業(抽象類,多態,接口)
乒乓球教練和籃球教練。
為了出國交流,跟乒乓球相關的人員都需要學習英語。
請用所學知識:
分析,這個案例中有哪些抽象類,哪些接口,哪些具體類
代碼中的註釋
分析:
教練類:
姓名,年齡
有參/無參
setXXX/getXXX
play()..
籃球教練:
姓名,年齡
有參/無參
setXXX/getXXX
play()..
乒乓球教練類:
姓名,年齡
有參/無參
setXXX/getXXX
play()..
Person:抽象類
姓名,年齡
有參/無參
setXXX/getXXX
play()..
接口: SpeakEnglish
SpeakEnglish() ;
抽煙的老師 extends 老師 implements SpeakEnglish{...}
- */
public class ZY719 {
public static void main(String[] args) {
//接口多態開發中直接調用
PingPangStudent pps=new PingPangStudent();
pps.setAge(18);
pps.setName("如花");
System.out.println(pps.getAge()+"---"+pps.getName());
pps.play();
pps.study();
PingPangTeacher ppt=new PingPangTeacher();
ppt.setAge(38);
System.out.println(ppt.getAge()+"---"+ppt.getName());
ppt.play();
ppt.study();
BallStudent bs=new BallStudent();
bs.setAge(18);
bs.setName("小三");
System.out.println(bs.getAge()+"---"+bs.getName());
bs.play();
bs.study();
BallTeacher bt=new BallTeacher();
bt.setName("大三");
System.out.println(bt.getAge()+"---"+bt.getName());
bt.play();
bt.study();
}
}
//創建一個抽象的類
public abstract class Person {
//共有屬性
private String name;
private int age;
//無參
public Person() {
super();
}
//有參
public Person(String name, int age) {
super();
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;
}
//play();
public abstract void play();
}
//老師類
public class Teacher extends Person {
//無參
public Teacher() {
super();
}
//有參
public Teacher(String name, int age) {
super(name, age);}
@Override
br/>}
@Override
System.out.println("老師玩捉迷藏");
}
}
//籃球教練類
public class BallTeacher extends Teacher {
//無參
public BallTeacher() {
super();
}
//有參
public BallTeacher(String name, int age) {
super(name, age);
}
public void study() {
System.out.println("籃球老師一邊學英語一邊玩藍球");
}
public void play() {
System.out.println("籃球教練愛玩籃球");
}
}
//乒乓球教練類
public class PingPangTeacher extends Teacher {
//無參
public PingPangTeacher() {
super();
}
//有參
public PingPangTeacher(String name, int age) {
super(name, age);
}
public void study() {
System.out.println("乒乓球老師一邊學英語一邊玩乒乓球");
}
public void play() {
System.out.println("乒乓球教練愛玩乒乓球");
}
}
//學生類
public class Student extends Person {
//無參
public Student() {
super();
}
//有參
public Student(String name, int age) {
super(name, age);}
@Override
br/>}
@Override
System.out.println("學生玩過家家");
}
}
//籃球學生類
public class BallStudent extends Student {
//無參
public BallStudent() {
super();
}
//有參
public BallStudent(String name, int age) {
super(name, age);
}
public void study() {
System.out.println("藍球學生一邊學英語一邊玩小藍球");
}
public void play() {
System.out.println("籃球學生愛玩小籃球");
}
}
//乒乓球學生類
public class PingPangStudent extends Student {
//無參
public PingPangStudent() {
super();
}
//有參
public PingPangStudent(String name, int age) {
super(name, age);
}
public void study() {
System.out.println("乒乓球學生一邊學英語一邊玩小乒乓球");
}
public void play() {
System.out.println("乒乓球學生愛玩小乒乓球");
}
}
老師學生類案例作業(抽象類,多態,接口)