灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2743回复:0

你不知道的Java--枚举

楼主#
更多 发布于:2012-09-08 09:41

你不知道的java环节,大部分采用dota作为背景,如有错误,请谅解并指正。
一:基本
[java]
public enum Hero {
    
    AM,
    BM,
    AA;

}
这样,我们就把部分英雄放入到一个枚举中。
[java]
System.out.println(Hero.AA);
        System.out.println(Hero.AM);
        System.out.println(Hero.BM);
        
        String a = Hero.AM.toString();
        System.out.println(a);
//遍历一下枚举中的值
[java]
for (Hero h :Hero.values()){
            System.out.println(h);
        }

二:扩展一下
搞一个英雄类型,
[java]
public enum Type {

    Agility("敏捷型"),
    Power("力量型"),
    intelligence("智力型");
    
    private String chinses;
    
    private Type(String chinese){
        this.chinses = chinese;
    }

    public String getChinses() {
        return chinses;
    }

    public void setChinses(String chinses) {
        this.chinses = chinses;
    }
    
    
}
增加两个属性,构造方法(必须是private,或者不写,其他的就报错呀),
重写一下toString(),
[java]
public enum Hero {
    
    AM("敌法师",Type.Agility),
    BM("兽王",Type.Power),
    AA("冰魂",Type.Intelligence);
    
    private String name;
    private Type type;
    
    private Hero(String name,Type type) {
        this.name = name;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.getType().getChinses()+"英雄:"+this.getName();
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

}

测试:
[java]
<span style="color:#000000;"> for (Hero h :Hero.values()){
            System.out.println(h);
        }</span>
敏捷型英雄:敌法师
力量型英雄:兽王
智力型英雄:冰魂
三:看看这个有木有
多个构造方法,也是可以的。但是注意不要空指针了。
[java]
public enum Hero {
    
    AM("敌法师",Type.Agility),
    BM("兽王",Type.Power),
    SS(),
    AA("冰魂",Type.Intelligence);
    
    private String name;
    private Type type;
    
    private Hero(String name,Type type) {
        this.name = name;
        this.type = type;
    }
    
    private Hero() {
        // TODO Auto-generated constructor stub
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.getType().getChinses()+"英雄:"+this.getName();
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

}
四:那这个呢
[java]
public interface Active {
    
    void att();

}
[java]
public enum Hero implements Active{
    
    AM("敌法师",Type.Agility),
    BM("兽王",Type.Power),
//  SS(),
    AA("冰魂",Type.Intelligence);
    
    private String name;
    private Type type;
    
    private Hero(String name,Type type) {
        this.name = name;
        this.type = type;
    }
    
    private Hero() {
        // TODO Auto-generated constructor stub
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.getType().getChinses()+"英雄:"+this.getName();
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    @Override
    public void att() {
        System.out.println(this.getName()+"攻击");
    }

}
[java]
for (Hero h :Hero.values()){
            System.out.println(h.getDeclaringClass());
    h.att();
            System.out.println(h);
}
敌法师攻击
兽王攻击
冰魂攻击



喜欢0 评分0
游客

返回顶部