public class Class1{
public enum Enum1 {
A("title") {
public void process(SomeClass a) {
String value = a.getTitle();
}
}, B("name") {
public void process(SomeClass b) {
String value = b.getName();
}
}
public class Class2{
public enum Enum2 {
A("title") {
public void process(SomeClass a) {
String value = a.getTitle();
}
}, C("desc") {
public void process(SomeClass b) {
String value = b.getDesc();
}
}
Class1和Class2,枚举A为常用。 如何将两个常用枚举变为通用枚举?
答案 0 :(得分:0)
为什么不制作一个包含以下内容的自己的枚举文件:
public enum Enum {
A {
public void process(Object b) {
//Do something
}
},
B {
public void process(Object b) {
//Do something
}
},
C {
public void process(Object b) {
//Do something
}
};
public void process(Object b) {
//Do something
}
}
这个枚举你可以在其他类中导入并使用非常简单:
public static void main(String[] args) {
Enum a = Enum.A;
a.process("123");
}
答案 1 :(得分:0)
这段代码快照怎么样?你在找这个吗?
public class Component {
private String name;
private String title;
public Component(String name, String title) {
this.name = name;
this.title = title;
}
public String getName() {
return this.name;
}
public String getTitle() {
return this.title;
}
public static void main(String[] args) {
Class1 class1 = new Class1(FieldsEnum.NAME);
class1.process();
Class2 class2 = new Class2(FieldsEnum.TITLE);
class2.process();
}
}
enum FieldsEnum {
TITLE {
public void process(Component component) {
System.out.println("Process with : " + component.getTitle());
}
},
NAME {
public void process(Component component) {
System.out.println("Process with : " + component.getName());
}
};
public abstract void process(Component component);
}
class Base {
public Base(FieldsEnum field) {
this.field = field;
}
private FieldsEnum field;
public FieldsEnum getField() {
return field;
}
public void setField(FieldsEnum field) {
this.field = field;
}
public void process() {
this.field.process(new Component("HomePage", "HomeTitle"));
}
}
class Class1 extends Base {
public Class1(FieldsEnum field) {
super(field);
}
// do you class1 specific work here
}
class Class2 extends Base {
public Class2(FieldsEnum field) {
super(field);
}
// do you class2 specific work here
}