目录
Draft(思路与主控制台)
任务: 乒乓球运动员:姓名,年龄,学打乒乓球,说英语 篮球运动员:姓名,年龄,学打篮球 乒乓球教练:姓名,年龄,教打乒乓球,说英语 篮球教练:姓名,年龄,教打篮球 用接口与继承编写其JavaBean类 分解思路: (1) 人可以分为两类,运动员和教练 行为可以分为,学和教 (2) 人分为 篮球行业和乒乓球行业 行为分为 学和教,乒乓球类独有个说英语类 紧接着,人可以概括为个人这个总类 执行操作: 一个父类Person,继承给子类运动员和教练,其内包含对应的虚方法,运动员和教练再继承给不同行业的,对于乒乓球行 业额外编写一个说英语的接口 具体细节: Person包含基本信息:姓名,年龄;包含抽象行为:behave 运动员包含:具体行业名
package practice1;
public class Draft {
public static void main(String[] args) {
Person pps = new PingPangSporter("刘诗雯", 23);
Person bs = new BasketballSporter();
Person ppc = new PingPangCoach();
Person bc = new BasketballCoach();
System.out.println(pps.getName() + "," + pps.getAge());
((PingPangSporter) pps).study();
//强制转换Person类的多态子类回归至单一子类,才可使用子类中特有的方法
((PingPangSporter) pps).speakEng();
}
}
Person(最大父类)
package practice1;
public 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;
}
}
SayEng(接口)
package practice1;
public interface SayEng {
public abstract void speakEng();
}
Spoter(二级父类之一)
package practice1;
public abstract class Sporter extends Person{
public Sporter() {
}
public Sporter(String name, int age) {
super(name, age);
}
public abstract void study();
}
Coach(二级父类之二)
package practice1;
public abstract class Coach extends Person{
public Coach() {
}
public Coach(String name, int age) {
super(name, age);
}
public abstract void teach();
}
具体的Sporter
PingPong的
package practice1;
public class PingPangSporter extends Sporter implements SayEng{
public PingPangSporter() {
}
public PingPangSporter(String name, int age) {
super(name, age);
}
@Override
public void speakEng() {
System.out.println("乒乓球运动员说英文");
}
@Override
public void study() {
System.out.println("乒乓球运动员学乒乓球");
}
}
Basketball的
package practice1;
public class BasketballSporter extends Sporter{
public BasketballSporter() {
}
public BasketballSporter(String name, int age) {
super(name, age);
}
@Override
public void study() {
System.out.println("篮球运动员在学习篮球");
}
}
具体的Coach
pingpang的
package practice1;
public class PingPangCoach extends Coach implements SayEng{
public PingPangCoach() {
}
public PingPangCoach(String name, int age) {
super(name, age);
}
@Override
public void teach() {
System.out.println("乒乓球教练教乒乓球");
}
@Override
public void speakEng() {
System.out.println("乒乓球教练说英文");
}
}
basketball的
package practice1;
public class BasketballCoach extends Coach{
public BasketballCoach() {
}
public BasketballCoach(String name, int age) {
super(name, age);
}
@Override
public void teach() {
System.out.println("篮球教练在教篮球");
}
}
运行结果
刘诗雯,23
乒乓球运动员学乒乓球
乒乓球运动员说英文Process finished with exit code 0