我有两个枚举类:
public enum dog{
Sound("barks"),
Eat("meat"),
Drink("water");
private String attribute;
private dog (String attribute){
this.attribute = attribute; }
public String returnAttribute(){
return attribute;}
}
和
public enum cat{
Sound("meows"),
Eat("fish"),
Drink("milk");
private String attribute;
private cat (String attribute){
this.attribute = attribute; }
public String returnAttribute(){
return attribute;}
}
我想在运行时动态地在两个枚举之间切换,好像它是一个对象(我知道它不是)。因此,我会根据“条件”为狗或猫分配一个枚举“持有者”,然后获取属性值,即enumholder.Sound.returnAttribute;
如果它会返回“喵喵”,它会返回“吠叫”。可能吗?
答案 0 :(得分:1)
简短的回答是否定的。请参阅Java enum inheritance
而不是使用枚举使用类和继承。要做你想要的,你需要三个课程,@charset "utf-8";
/* CSS Document */
body{
width:100%;
margin:auto;
}
.container{
width:85%;
margin:auto;
}
.header{
background-color:#900;
width:100%;
height:17%;
top:0;
position:fixed;
text-decoration:none;
}
.logo{
float:left;
}
.navBar{
float:right;
height: 10px;
}
li{
float:left;
margin-left:2%;
padding-top:20px;
color:#FFF;
text-transform:uppercase;
font-size:12px;
font-stretch:ultra-condensed;
padding-right:0;
}
.content{
padding-top:20%;
}
.home_icon{
height:50px;
width:50px;
}
,Animal
和Cat
。
Dog
您可以使用这些类来获得所需的功能。
class Animal {
public String getSound() {
return "Animal sound";
}
// Other mehtods...
}
class Cat extends Animal {
@Override
public String getSound() {
return "Meow";
}
// Other mehtods...
}
class Dog extends Animal {
@Override
public String getSound() {
return "Bark";
}
// Other mehtods...
}
答案 1 :(得分:1)
您似乎只是在寻找普通物体。你为什么需要enum
?
class AnimalType {
String sound, eat, drink;
}
// in some method somewhere
AnimalType cat = new AnimalType();
cat.sound = "meows";
cat.eat = "fish";
cat.drink = "milk";
AnimalType dog = new AnimalType();
dog.sound = "barks";
dog.eat = "meat";
dog.drink = "water";
// then use either cat or dog
答案 2 :(得分:1)
在我看来,您应该将此建模为您的枚举继承的接口 - 这些方法将成为您当前作为成员建模的方法。类似的东西:
interface Animal {
String getFood();
int getNumberOfLegs();
}
enum Dog implements Animal {
POODLE,
GOLDEN_RETRIEVER;
public String getFood() {
return "dog food";
}
public int getNumberOfLegs() {
return 4;
}
}
enum Bird implements Animal {
CANARY("seed"),
EAGLE("meat");
private final String food;
Bird(String food) {
this.food = food;
}
public String getFood() {
return food;
}
public int getNumberOfLegs() {
return 2;
}
}
然后,您可以将狗或猫分配给Animal
变量,它会表现正常:
Animal animal;
animal = Bird.CANARY;
assertEquals("seed", animal.getFood());
animal = Dog.POODLE;
assertEquals(4, animal.getNumberOfLegs());
这具有enum的所有优点(JVM在构建它们时确定了一组固定的实例),同时还能够以不同方式处理不同的动物组。
答案 3 :(得分:0)
如果你想要使用枚举,你应该做这样的事情:
public enum AnimalTraits {
DOG(/*sound*/"barks",
/*eat */"meat",
/*drink*/"water"),
CAT(/*sound*/"meows",
/*eat */"fish",
/*drink*/"milk");
private final String sound;
private final String eat;
private final String drink;
private AnimalTraits(String sound, String eat, String drink) {
this.sound = sound;
this.eat = eat;
this.drink = drink;
}
public String getSound() {
return this.sound;
}
public String getEat() {
return this.eat;
}
public String getDrink() {
return this.drink;
}
}