英文:
How to instantiate an object tied to java enum
问题
I want to instantiate object for an enum. For example:
TYPE1("type_name_1", new TypeObj1()),
TYPE2("type_name_2", new TypeObj2());
TypeInterface typeObj;
String value;
}
...
}
...
}
Is this a correct way to do it? I want a single object of TypeInterface attached to enum. Please suggest alternatives.
英文:
I want to instantiate object for an enum. For example:
public enum Type {
TYPE1("type_name_1", new TypeObj1()),
TYPE2("type_name_2", new TypeObj2());
TypeInterface typeObj;
String value;
}
public class TypeObj1 implements TypeInterface {
...
}
public class TypeObj2 implements TypeInterface {
...
}
Is this a correct way to do it? I want a single object of TypeInterface attached to enum. Please suggest alternatives.
答案1
得分: 1
是的,你正朝着正确的方向前进。
你需要一个构造函数来将传递的对象分配给你的成员字段。
将你的成员字段设为final
,以防止它们被重新分配给另一个对象引用。
public enum Type {
// 枚举对象。
TYPE1("type_name_1", new TypeObj1()),
TYPE2("type_name_2", new TypeObj2());
// 成员字段。
final TypeInterface typeObj;
final String value;
// 构造函数。
Type(final String value, final TypeInterface typeInterface) {
this.typeObj = typeInterface;
this.value = value;
}
}
提示:这种思考练习如果设计一个简单但半现实的情境,而不是你的抽象“TypeInterface”和“value”,会更清晰和有成效。
public enum Pet {
// 枚举对象。
FLUFFY("tuxedo", new Cat()),
ROVER("chocolate Lab", new Dog());
// 成员字段。
final Animal animal;
final String description;
// 构造函数。
Pet(final String desc, final Animal animal) {
this.animal = animal;
this.description = desc;
}
}
英文:
Yes, you are headed in the right direction.
You’ll need a constructor to assign the passed objects to your member fields.
Make your member fields final
to prevent their reassignment to another object reference.
public enum Type {
// Enum objects.
TYPE1("type_name_1", new TypeObj1()),
TYPE2("type_name_2", new TypeObj2());
// Member fields.
final TypeInterface typeObj;
final String value;
// Constructor.
Type( final String value , final TypeInterface typeInterface ) {
this.typeObj = typeInterface ;
this.value = value ;
}
}
Tip: This kind of thought exercise is more clear and productive if you devise a simple but semi-realistic scenario rather than your amorphous “TypeInterface” and “value”.
public enum Pet {
// Enum objects.
FLUFFY( "tuxedo" , new Cat() ),
ROVER( "chocolate Lab" , new Dog() );
// Member fields.
final Animal animal;
final String description;
// Constructor.
Type( final String desc , final Animal animal ) {
this.animal = animal ;
this.description = desc ;
}
}
答案2
得分: 0
为您的 enum
创建一个构造函数,该构造函数与您指定的参数相匹配。
没有访问修饰符;private
是多余的。
枚举类型(Java™ 教程 > 学习 Java 语言 > 类和对象)。
public enum Type {
TYPE1("type_name_1", new TypeObj1()),
TYPE2("type_name_2", new TypeObj2());
final TypeInterface typeObj;
final String value;
Type(String value, TypeInterface typeObj) {
this.value = value;
this.typeObj = typeObj;
}
}
英文:
Create a constructor for your enum
, which matches the arguments you're specifying.
There is no access-modifier; and private
is redundant.
Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects).
public enum Type {
TYPE1("type_name_1", new TypeObj1()),
TYPE2("type_name_2", new TypeObj2());
final TypeInterface typeObj;
final String value;
Type(String value, TypeInterface typeObj) {
this.value = value;
this.typeObj = typeObj;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论