运动员和教练的案例分析
运动运和教练的案例 代码实现
整个分析过程,我是通过画图讲解的。
<span style="color: #008000;">*/<span style="color: #008000;">//<span style="color: #008000;">定义一个说英语的接口
<span style="color: #0000ff;">interface<span style="color: #000000;"> SpeakEnglish {
<span style="color: #008000;">//<span style="color: #008000;">说英语
<span style="color: #0000ff;">public <span style="color: #0000ff;">abstract <span style="color: #0000ff;">void<span style="color: #000000;"> speak();
}
<span style="color: #008000;">//<span style="color: #008000;">定义人的抽象类
<span style="color: #0000ff;">abstract <span style="color: #0000ff;">class<span style="color: #000000;"> Person {
<span style="color: #0000ff;">private<span style="color: #000000;"> String name;
<span style="color: #0000ff;">private <span style="color: #0000ff;">int<span style="color: #000000;"> age;
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Person() {}
</span><span style="color: #0000ff;">public</span> Person(String name,<span style="color: #0000ff;">int</span><span style="color: #000000;"> age) {
</span><span style="color: #0000ff;">this</span>.name =<span style="color: #000000;"> name;
</span><span style="color: #0000ff;">this</span>.age =<span style="color: #000000;"> age;
}
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getName() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> name;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setName(String name) {
</span><span style="color: #0000ff;">this</span>.name =<span style="color: #000000;"> name;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getAge() {
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> age;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> setAge(<span style="color: #0000ff;">int</span><span style="color: #000000;"> age) {
</span><span style="color: #0000ff;">this</span>.age =<span style="color: #000000;"> age;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">睡觉</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> sleep() {
System.out.println(</span>"人都是要睡觉的"<span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">吃饭,运动员和教练吃的不一样。这里使用抽象<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a></span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> eat();
}
<span style="color: #008000;">//<span style="color: #008000;">定义运动员抽象类
<span style="color: #0000ff;">abstract <span style="color: #0000ff;">class Player <span style="color: #0000ff;">extends<span style="color: #000000;"> Person {
<span style="color: #0000ff;">public<span style="color: #000000;"> Player() {}
</span><span style="color: #0000ff;">public</span> Player(String name,<span style="color: #0000ff;">int</span><span style="color: #000000;"> age) {
</span><span style="color: #0000ff;">super</span><span style="color: #000000;">(name,age);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">学习,乒乓器和篮球运动员学习的东西不一样。这里使用抽象<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a></span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> study();
}
<span style="color: #008000;">//<span style="color: #008000;">定义教练抽象类
<span style="color: #0000ff;">abstract <span style="color: #0000ff;">class Coach <span style="color: #0000ff;">extends<span style="color: #000000;"> Person {
<span style="color: #0000ff;">public<span style="color: #000000;"> Coach() {}
</span><span style="color: #0000ff;">public</span> Coach(String name,age);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">教,乒乓器和篮球运动员教的东西不一样。这里使用抽象<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a></span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> teach();
}
<span style="color: #008000;">//<span style="color: #008000;">定义乒乓球运动员具体类
<span style="color: #0000ff;">class PingPangPlayer <span style="color: #0000ff;">extends Player <span style="color: #0000ff;">implements<span style="color: #000000;"> SpeakEnglish {
<span style="color: #0000ff;">public<span style="color: #000000;"> PingPangPlayer(){}
</span><span style="color: #0000ff;">public</span> PingPangPlayer(String name,age);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">吃</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> eat() {
System.out.println(</span>"乒乓球运动员吃大白菜,喝小米粥"<span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">学习</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> study() {
System.out.println(</span>"乒乓球运动员学习如何发球和接球"<span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">说英语</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> speak() {
System.out.println(</span>"乒乓球运动员说英语"<span style="color: #000000;">);
}
}
<span style="color: #008000;">//<span style="color: #008000;">定义篮球运动员具体类
<span style="color: #0000ff;">class BasketballPlayer <span style="color: #0000ff;">extends<span style="color: #000000;"> Player {
<span style="color: #0000ff;">public<span style="color: #000000;"> BasketballPlayer(){}
</span><span style="color: #0000ff;">public</span> BasketballPlayer(String name,age);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">吃</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> eat() {
System.out.println(</span>"篮球运动员吃牛肉,喝牛奶"<span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">学习</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> study() {
System.out.println(</span>"篮球运动员学习如何运球和投篮"<span style="color: #000000;">);
}
}
<span style="color: #008000;">//<span style="color: #008000;">定义乒乓球教练具体类
<span style="color: #0000ff;">class PingPangCoach <span style="color: #0000ff;">extends Coach <span style="color: #0000ff;">implements<span style="color: #000000;"> SpeakEnglish {
<span style="color: #0000ff;">public<span style="color: #000000;"> PingPangCoach(){}
</span><span style="color: #0000ff;">public</span> PingPangCoach(String name,age);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">吃</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> eat() {
System.out.println(</span>"乒乓球教练吃小白菜,喝大米粥"<span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">教</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> teach() {
System.out.println(</span>"乒乓球教练教如何发球和接球"<span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">说英语</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> speak() {
System.out.println(</span>"乒乓球教练说英语"<span style="color: #000000;">);
}
}
<span style="color: #008000;">//<span style="color: #008000;">定义篮球教练具体类
<span style="color: #0000ff;">class BasketballCoach <span style="color: #0000ff;">extends<span style="color: #000000;"> Coach {
<span style="color: #0000ff;">public<span style="color: #000000;"> BasketballCoach(){}
</span><span style="color: #0000ff;">public</span> BasketballCoach(String name,age);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">吃</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> eat() {
System.out.println(</span>"篮球教练吃羊肉,喝羊奶"<span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">教</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> teach() {
System.out.println(</span>"篮球教练教如何运球和投篮"<span style="color: #000000;">);
}
}
<span style="color: #0000ff;">class<span style="color: #000000;"> InterfaceDemo {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #008000;">//<span style="color: #008000;">测试运动员(乒乓球运动员和篮球运动员)
<span style="color: #008000;">//<span style="color: #008000;">乒乓球运动员
PingPangPlayer ppp = <span style="color: #0000ff;">new<span style="color: #000000;"> PingPangPlayer();
ppp.setName("王浩"<span style="color: #000000;">);
ppp.setAge(33<span style="color: #000000;">);
System.out.println(ppp.getName()+"---"+<span style="color: #000000;">ppp.getAge());
ppp.eat();
ppp.sleep();
ppp.study();
ppp.speak();
System.out.println("----------------"<span style="color: #000000;">);
<span style="color: #008000;">//<span style="color: #008000;">通过带参构造给数据
ppp = <span style="color: #0000ff;">new PingPangPlayer("刘洋",36<span style="color: #000000;">);
System.out.println(ppp.getName()+"---"+<span style="color: #000000;">ppp.getAge());
ppp.eat();
ppp.sleep();
ppp.study();
ppp.speak();
System.out.println("----------------"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">篮球运动员</span>
BasketballPlayer bp = <span style="color: #0000ff;">new</span><span style="color: #000000;"> BasketballPlayer();
bp.setName(</span>"姚明"<span style="color: #000000;">);
bp.setAge(</span>34<span style="color: #000000;">);
System.out.println(bp.getName()</span>+"---"+<span style="color: #000000;">bp.getAge());
bp.eat();
bp.sleep();
bp.study();
</span><span style="color: #008000;">//</span><span style="color: #008000;">bp.speak(); </span><span style="color: #008000;">//</span><span style="color: #008000;">没有该<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>
</span>
<span style="color: #000000;"> }
}
原文链接:https://www.f2er.com/java/403343.html