A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 冷风然 中级黑马   /  2015-7-14 21:02  /  429 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
乒乓秋运动员/教练(说英语)
篮球运动员/教练
都有姓名/年龄
运动员 训练        教练 指导
人 吃饭/睡觉
*/
class  YunDongDemo
{
        public static void main(String[] args)
        {
                PingPongPlayer ppp = new PingPongPlayer();
                ppp.study();
                ppp.eat();
                ppp.speak();
                ppp.sleep();
                System.out.println("Hello World!");
                PingPongCoach ppc = new PingPongCoach();
                ppc.eat();
                ppc.teach();
                ppc.sleep();
                ppc.speak();
                System.out.println("Hello World!");
                BasketCoach bc = new BasketCoach();
                bc.eat();
                bc.teach();
                bc.sleep();
                System.out.println("Hello World!");
                BasketPlayer bp = new BasketPlayer();
                bp.eat();
                bp.study();
                bp.sleep();
                System.out.println("Hello World!");
        }
}

interface Speak//定义接口interface接口+接口名--interface interface abstract抽象abstract
{
        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 void sleep()//成员方法
        {
                System.out.println("睡觉");
        }
        public abstract void eat();//抽象方法没有方法体{},修饰词abstract
        public void setName(String name)
        {
                this.name = name;
        }
        public String getName()
        {
                return name;
        }
        public void setAge(int age)
        {
                this.age = age;
        }
        public int getAge()
        {
                return age;
        }

}
//运动员
abstract class Player extends Person
{
        public Player(){};
        public abstract void study();//方法名第一个单词首字母小写抽象没有方法体,用abstract修饰       
}
//教练
abstract class Coach extends Person
{
        public Coach(){};
        public abstract void teach();
}
//乒乓球运动员
class PingPongPlayer extends Player implements Speak//implements(实现)继承类;实现接口
{
        public void study()
        {
                System.out.println("乒乓球运动员学习接发乒乓球");
        }
        public void eat()
        {
                System.out.println("乒乓球运动员吃生煎包");
        }
        public void speak()
        {
                System.out.println("乒乓球运动员说英语");
        }
}
//篮球运动员
class BasketPlayer extends Player //implements(实现)继承类;实现接口
{
        public void study()
        {
                System.out.println("篮球运动员学习三分突破");
        }
        public void eat()
        {
                System.out.println("篮球运动员吃牛肉面");
        }

}
//篮球教练
class BasketCoach extends Coach
{
        public void eat()
        {
                System.out.println("篮球教练吃炒饼");
        }
        public void teach()
        {
                System.out.println("篮球教练指导运球突破三分");
        }
}
//乒乓球教练
class PingPongCoach extends Coach
{
        public void eat()
        {
                System.out.println("乒乓球教练吃盖饭");
        }
        public void teach()
        {
                System.out.println("乒乓球教练指导接发球");
        }
        public void speak()
        {
                System.out.println("乒乓球教练说英语");
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马