JAVA实验5-接口开发(开闭原则)

public class Application {
    public static void main(String args[]){
        User user=new User();
        for(int i=0;i<3;i++){
            user.setAccount(i);
            user.select();
            user.playGame();
            NPC npc =new NPC();
            npc.talk(user);
            System.out.println();
        }
    }
}

interface Task

public interface Task {
    public void post();
    default void getPrize() {
        System.out.println("得到奖励");
    }
    default void getTask() {
        System.out.println("接受任务");
    }
}

NPC.java

public class NPC {
    Task task;
    boolean flag=false;
    void talk(User user){
        int account1=user.account;
        System.out.println("与NPC交谈");
        switch (account1){
            case 0:task=new Arrest();
            task.post();
            task.getTask();
            flag=true;break;
            case 1:task=new Kill();
                task.post();
                task.getTask();
                flag=true;break;
            case 2:task=new Plant();
                task.post();
                task.getTask();
                flag=true;break;
        }
    }
}

Kill.java

public class Kill implements Task{
          public void post(){
              System.out.println("发布杀人任务");
          }
}

HuntTreasure.java

public class HuntTreasure implements Task{
    public void post(){
        System.out.println("发布寻宝任务");
    }
}

Arrest.java

public class Arrest implements Task{
    public void post(){
        System.out.println("发布抓捕任务");
    }
}

Plant.java

public class Plant implements Task{
    public void post(){
        System.out.println("发布种地任务");
    }
}

User.java

public class User {
    Character role;
    int account;
    void setRole(Character role) {
        this.role = role;
    }
    void select(){
        int account1=account%6;
        switch (account1){
            case 0:Swordsman swordsman=new Swordsman("帅气的服装","帅气的头发","帅气的外表","Swordsman");
                   setRole(swordsman);
                   break;
            case 1:Thief thief=new Thief("黑色的服装","黑色的头发","伪装的外表","Thief");
                setRole(thief);
                    break;
            case 3:Constable constable=new Constable("官府的服装","官方的发型","官方权威的外表","Constable");
                setRole(constable);
                    break;
            case 2:Civilian civilian=new Civilian("普通的服装","普通的头发","普通的外表","Civilian");
                setRole(civilian);
                   break;
            default:OfficerZhiFu zhiFu=new OfficerZhiFu("官方的服装","官方的头发","官方的外表","ZhiFu");
                  setRole(zhiFu);
                  break;
        }
    }
    void setAccount(int account1){
        account=account1;
    }
    void playGame(){
        role.playGame();
    }
}

Character.java

abstract public class Character {
    String clothes;
    String hairColor;
    String appearance;
    String name;
    abstract void walk();
    abstract void run();
    abstract void show();
    abstract void speak();
    abstract  void playGame();
    public Character(String clothes,String hairColor,String appearance,String name){
        this.clothes=clothes;
        this.hairColor=hairColor;
        this.appearance=appearance;
        this.name=name;
    }
}

Civilian.java

class Civilian extends Character{
        public Civilian(String clothes,String hairColor,String appearance,String name){
            super(clothes,hairColor,appearance,name);
        }
        void walk() {
                System.out.println("普通地走");
        }
        void run() {
                System.out.println("普通地跑:时速10Km/h");
        }
        void show() {
                System.out.println("普通地展示:"+this.clothes+" "+this.hairColor+" "+this.appearance+" "+this.name);
        }
        void speak() {
                System.out.println("普通地说话:我赢了");
        }
        void playGame(){
                walk();
                run();
                show();
                speak();
        }
    }

Thief.java

class Thief extends Character{
    public Thief(String clothes,String hairColor,String appearance,String name){
        super(clothes,hairColor,appearance,name);
    }
    void walk() {
        System.out.println("偷偷摸摸地走");
    }
    void run() {
        System.out.println("偷偷摸摸地跑:时速40Km/h");
    }
    void show() {
        System.out.println("偷偷摸摸地展示:"+this.clothes+" "+this.hairColor+" "+this.appearance+" "+this.name);
    }
    void speak() {
        System.out.println("偷偷摸摸地说话:我赢了");
    }
    void playGame(){
        walk();
        run();
        show();
        speak();
    }
}

Swordsman.java

class Swordsman extends Character{
    public Swordsman(String clothes,String hairColor,String appearance,String name){
        super(clothes,hairColor,appearance,name);
    }
    void walk() {
        System.out.println("帅气地走");
    }
    void run() {
        System.out.println("帅气地跑:时速50Km/h");
    }
    void show() {
        System.out.println("帅气地展示:"+this.clothes+" "+this.hairColor+" "+this.appearance+" "+this.name);
    }
    void speak() {
        System.out.println("依本大侠看……"); }
    void playGame(){
        walk();
        run();
        show();
        speak();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值