【你不知道的Java】-【枚举】

你不知道的Java环节,大部分采用dota作为背景,如有错误,请谅解并指正。


一:基本

public enum Hero {
	
	AM,
	BM,
	AA;

}
这样,我们就把部分英雄放入到一个 枚举中

System.out.println(Hero.AA);
		System.out.println(Hero.AM);
		System.out.println(Hero.BM);
		
		String a = Hero.AM.toString();
		System.out.println(a);

//遍历一下枚举中的值

for (Hero h :Hero.values()){
			System.out.println(h);
		}


二:扩展一下

搞一个英雄类型,

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(),闭嘴

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;
    }

}


测试:

	for (Hero h :Hero.values()){
			System.out.println(h);
		}

敏捷型英雄:敌法师
力量型英雄:兽王

智力型英雄:冰魂


三:看看这个有木有

多个构造方法,也是可以的。但是注意不要空指针了。

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;
	}

}

四:那这个呢


public interface Active {
	
	void att();

}

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()+"攻击");
	}

}

		for (Hero h :Hero.values()){
//			System.out.println(h.getDeclaringClass());
			h.att();
//			System.out.println(h);
		}

敌法师攻击
兽王攻击
冰魂攻击

注:可能还有好多,慢慢来


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值