鱼C论坛

 找回密码
 立即注册
查看: 2916|回复: 0

[学习笔记] JavaDemo_0911_2323

[复制链接]
发表于 2018-9-11 23:34:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
abstract class Action
{
        public static final int EAT=1;
        public static final int SLEEP=5;
        public static final int WORK=10;
        public void command(int code)
        {
                switch(code)
                {
                        case EAT:
                        {
                                this.eat();
                                break;
                        }
                        case SLEEP:
                        {
                                this.sleep();
                                break;
                        }
                        case WORK:
                        {
                                this.work();
                                break;
                        }
                        case EAT + SLEEP +WORK:
                        {
                                this.eat();
                                this.sleep();
                                this.work();
                                break;
                        }
                }
        }
        public abstract void eat();
        public abstract void sleep();
        public abstract void work();
}
class Robot extends Action
{
        public void eat()
        {
                System.out.println("机器人需要接通电源充电");
        }
        public void sleep(){}
        public void work()
        {
                System.out.println("机器人按照固定的套路工作");
        }
}
class Person extends Action
{
        public void eat()
        {
                System.out.println("饿的时候安静的坐下吃饭");
        }
        public void sleep()
        {
                System.out.println("安静的躺下,慢慢的睡觉");
        }       
        public void work()
        {
                System.out.println("人类是高级脑类动物,所以要有想法的工作");
        }
}
class Pig extends Action
{
        public void eat()
        {
                System.out.println("食槽里吃食");
        }
        public void sleep()
        {
                System.out.println("倒地就睡");
        }       
        public void work()
        {
                System.out.println("不工作");
        }
}
public class JavaDemo_0911_2323
{
        public static void main(String[] args)
        {
                Action robotAction=new Robot();
                Action personAction=new Person();
                Action pigAction=new Pig();
                System.out.println("----------------机器人行为-----------");
                robotAction.command(Action.SLEEP);
                robotAction.command(Action.WORK);
        }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 09:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表