黑马程序员技术交流社区
标题:
黑马程序员—Java基础(接口,类,抽象类综合小练习)
[打印本页]
作者:
942932576
时间:
2015-7-6 13:58
标题:
黑马程序员—Java基础(接口,类,抽象类综合小练习)
------<a target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
题目:教练和运动员案例:分为篮球与乒乓球两种,其中乒乓球运动员与教练需要学习英语,篮球运动员则不需要
具体分析:
QQ截图20150706135633.jpg
(97.16 KB, 下载次数: 63)
下载附件
2015-7-6 13:57 上传
package day06;
public class Ex1 {
public static void main(String[] args){
//测试运动员(测试方法一样)
//乒乓球运动员
PingPangPlayer ppp = new PingPangPlayer();
ppp.setName("张三");
ppp.setAge(22);
System.out.println(ppp.getName()+"---"+ppp.getAge());
ppp.eat();
ppp.sleep();
ppp.study();
ppp.speak();
System.out.println("----------------");
//通过带参构造给数据
PingPangPlayer ppp1 = new PingPangPlayer("李四",23);
System.out.println(ppp1.getName()+"---"+ppp.getAge());
ppp1.eat();
ppp1.sleep();
ppp1.study();
ppp1.speak();
System.out.println("----------------");
}
}
//定义说英语接口
interface SpeakEnglish{
public abstract void speak();
}
//定义抽象人类
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 study();
}
//定义教练抽象类
abstract class Coach extends Person{
public Coach(){
}
public Coach(String name, int age){
super(name,age);
}
public abstract void teach();
}
//定义乒乓球运动员具体类
class PingPangPlayer extends Player implements SpeakEnglish{
public PingPangPlayer(){
}
public PingPangPlayer(String name, int age){
super(name, age);
}
@Override
public void speak() {
// TODO Auto-generated method stub
System.out.println("乒乓球运动员说英语");
}
@Override
public void study() {
// TODO Auto-generated method stub
System.out.println("乒乓球运动员学习如何发球和接球");
}
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("乒乓球运动员吃大白菜,喝小米粥");
}
}
//定义篮球运动员具体类
class BasketballPlayer extends Player{
public BasketballPlayer(){
}
public BasketballPlayer(String name, int age){
super(name, age);
}
@Override
public void study() {
// TODO Auto-generated method stub
System.out.println("篮球运动员学习如何运球和投篮");
}
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("篮球运动员吃牛肉,喝牛奶");
}
}
//定义乒乓球教练具体类
class PingPangCoach extends Coach implements SpeakEnglish{
public PingPangCoach(){
}
public PingPangCoach(String name, int age){
super(name, age);
}
@Override
public void speak() {
// TODO Auto-generated method stub
System.out.println("乒乓球教练说英语");
}
@Override
public void teach() {
// TODO Auto-generated method stub
System.out.println("乒乓球教练教如何发球和接球");
}
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("乒乓球教练吃小白菜,喝大米粥");
}
}
//定义篮球教练具体类
class BasketballCoach extends Coach{
@Override
public void teach() {
// TODO Auto-generated method stub
System.out.println("篮球教练教如何运球和投篮");
}
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("篮球教练吃羊肉,喝羊奶");
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2