• 策略模式


    package lloo;
    
    public class Application1 {
      public static  void  main  (String  args[])
      {
    	  Character   character=new   Character();
    	  character.setWeaponBehavior(new KnifeBehavior());
    	  character.setName("king");
    	  int n=200;   //武器伤害值
    	  character.display(n);  
    	  System.out.printf("%s的武器装备:%4d
    ",character.getName(),character.fight());
    	  System.out.println("匕首刺杀");
    	  character.setWeaponBehavior(new SwordBehavior());
    	  character.setName("Queen");
    	  int n1=400;   //武器伤害值
    	  character.display(n1);  
    	  System.out.printf("%s的武器装备:%4d
    ",character.getName(),character.fight());
    	  System.out.println("长剑刺杀");
    	  character.setWeaponBehavior(new AxeBehavior());
    	  character.setName("Knight");
    	  int n2=300;//武器伤害值
    	  character.display(n2); 
    	  System.out.printf("%s的武器装备:%4d
    ",character.getName(),character.fight());
    	  System.out.println("斧头劈砍");
    	  character.setWeaponBehavior(new BowAndArrowBehavior());
    	  character.setName("Troll");
    	  int n3=100;   //武器伤害值
    	  character.display(n3);  
    	  System.out.printf("%s的武器装备:%4d
    ",character.getName(),character.fight());
    	  System.out.println("弹弓射击");
      }
    }
    

      

    package lloo;
    
    public class AxeBehavior   implements WeaponBehavior {
    public  void useWeapon(int n)
    {
    }
    }
    package lloo;
    
    public class BowAndArrowBehavior  implements  WeaponBehavior {
    
    	
    	public void useWeapon(int n)
    	{
    		
    	}
    
    }
    package lloo;
    
    public class Character {
            WeaponBehavior    weaponBehavior;
            	String  name;
            	int  f;
            public    void  setWeaponBehavior(WeaponBehavior  weaponBehavior)
            {
            	this.weaponBehavior=weaponBehavior;
            }
    public void  display(int n)
    {
    	f=n;
    	if (weaponBehavior!=null)
    		   weaponBehavior.useWeapon(n);
    	   else 
    		   System.out.println("没有武器可用");
    }
    public int  fight( )
    {
    	return f;
    }
    public void setName(String s)
    {
    	name=s;
    }
    public  String getName()
    {
    	 return  name;
    }
    }
    

      

    package lloo;
    
    public class  KnifeBehavior   implements  WeaponBehavior {
    	
    	public void useWeapon(int n) {
    	
    		
    	}
    
    }
    

      

    package lloo;
    
    public class SwordBehavior  implements WeaponBehavior
    {
    	public void useWeapon(int n) {
    		
    		
    	}
    }
    

      

    package lloo;
    
    public interface WeaponBehavior {
        public  void   useWeapon(int  n);
    }
    

      

      

  • 相关阅读:
    阿里巴巴数据库分库分表的最佳实践
    Tomcat控制台日志乱码解决方案
    区块链:多链体系在提升性能的同时,怎么去保证单链被攻击性问题
    在 CentOS 7 1801 中安装 PostgreSQL-11
    为什么即使现在生意不太好做,还是有一批批的人开始做生意?
    投资十几万可以做点什么生意?
    理发店真是一个暴利行业吗?
    今日头条是怎么盈利的?
    中国都有哪些著名的风投失败的案例?
    基于语音应用的10项最佳实践
  • 原文地址:https://www.cnblogs.com/fanerna/p/5400661.html
Copyright © 2020-2023  润新知