zoukankan      html  css  js  c++  java
  • 类、接口作为成员变量类型——Java

    一:类作为成员变量类型

     1 // 游戏当中的英雄角色类
     2 public class Hero {
     3 
     4     private String name; // 英雄的名字
     5     private int age; // 英雄的年龄
     6     private Weapon weapon; // 英雄的武器
     7 
     8     public Hero() {
     9     }
    10 
    11     public Hero(String name, int age, Weapon weapon) {
    12         this.name = name;
    13         this.age = age;
    14         this.weapon = weapon;
    15     }
    16 
    17     public void attack() {
    18         System.out.println("年龄为" + age + "的" + name + "用" + weapon.getCode() + "攻击敌方。");
    19     }
    20 
    21     public String getName() {
    22         return name;
    23     }
    24 
    25     public void setName(String name) {
    26         this.name = name;
    27     }
    28 
    29     public int getAge() {
    30         return age;
    31     }
    32 
    33     public void setAge(int age) {
    34         this.age = age;
    35     }
    36 
    37     public Weapon getWeapon() {
    38         return weapon;
    39     }
    40 
    41     public void setWeapon(Weapon weapon) {
    42         this.weapon = weapon;
    43     }
    44 }
    public class Weapon {
    
        private String code; // 武器的代号
    
        public Weapon() {
        }
    
        public Weapon(String code) {
            this.code = code;
        }
    
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    }
    public class DemoMain {
    
        public static void main(String[] args) {
            // 创建一个英雄角色
            Hero hero = new Hero();
            // 为英雄起一个名字,并且设置年龄
            hero.setName("盖伦");
            hero.setAge(20);
    
            // 创建一个武器对象
            Weapon weapon = new Weapon("AK-47");
            // 为英雄配备武器
            hero.setWeapon(weapon);
    
            // 年龄为20的盖伦用多兰剑攻击敌方。
            hero.attack();
        }
    
    }

    二:接口作为成员变量

    public interface Skill {
    
        void use(); // 释放技能的抽象方法
    
    }
    public class SkillImpl implements Skill {
        @Override
        public void use() {
            System.out.println("Biu~biu~biu~");
        }
    }
    public class Hero {
    
        private String name; // 英雄的名称
        private Skill skill; // 英雄的技能
    
        public Hero() {
        }
    
        public Hero(String name, Skill skill) {
            this.name = name;
            this.skill = skill;
        }
    
        public void attack() {
            System.out.println("我叫" + name + ",开始施放技能:");
            skill.use(); // 调用接口中的抽象方法
            System.out.println("施放技能完成。");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Skill getSkill() {
            return skill;
        }
    
        public void setSkill(Skill skill) {
            this.skill = skill;
        }
    }
    public class DemoGame {
    
        public static void main(String[] args) {
            Hero hero = new Hero();
            hero.setName("艾希"); // 设置英雄的名称
    
            // 设置英雄技能
    //        hero.setSkill(new SkillImpl()); // 使用单独定义的实现类
    
            // 还可以改成使用匿名内部类
    //        Skill skill = new Skill() {
    //            @Override
    //            public void use() {
    //                System.out.println("Pia~pia~pia~");
    //            }
    //        };
    //        hero.setSkill(skill);
    
            // 进一步简化,同时使用匿名内部类和匿名对象
            hero.setSkill(new Skill() {
                @Override
                public void use() {
                    System.out.println("Biu~Pia~Biu~Pia~");
                }
            });
    
            hero.attack();
        }
    
    }
  • 相关阅读:
    WinAPI: 钩子回调函数之 GetMsgProc
    WinAPI: 钩子回调函数之 MouseProc
    WinAPI: 钩子回调函数之 CBTProc
    WinAPI: 钩子回调函数之 ShellProc
    WinAPI: 钩子回调函数之 ForegroundIdleProc
    WinAPI: 钩子回调函数之 CallWndProc
    WinAPI: 钩子回调函数之 DebugProc
    WinAPI: 钩子回调函数之 HardwareProc
    CodeIgniter入门案例之简单新闻系统三
    CodeIgniter 类库
  • 原文地址:https://www.cnblogs.com/zeon/p/13522349.html
Copyright © 2011-2022 走看看