Java 枚举功能的抽象
我有两个类似的枚举:Java 枚举功能的抽象,java,enums,interface,Java,Enums,Interface,我有两个类似的枚举: public enum enum1{ A(1,1), B(2,2); private final int x,y; enum1(int x,int y){ this.x=x; this.y=y; } int func(){ return this.x+this.y; } } public enum enum2{ C(3,3), D(4,4);
public enum enum1{
A(1,1),
B(2,2);
private final int x,y;
enum1(int x,int y){
this.x=x;
this.y=y;
}
int func(){ return this.x+this.y; }
}
public enum enum2{
C(3,3),
D(4,4);
private final int x,y;
enum2(int x,int y){
this.x=x;
this.y=y;
}
int func(){ return this.x+this.y;}
}
public class test{
void method(enum1 e){ /*something using func*/ }
void method(enum2 e){ /*something using func*/ }
}
如何在接口中抽象这些枚举的功能,并通过传递接口在测试类中仅使用一个方法而不是两个方法
编辑:我不希望合并枚举。我需要单独的枚举。有一件事是可以肯定的:只要
x
和y
是私有的
,并且无法从类外读取它们,您就必须为每个枚举实现相同的方法。如果允许对枚举中的这些字段添加访问器进行合理更改,则可以使用接口的default
方法来实现伞式行为:
interface Addable {
int getX();
int getY();
default int add() {
return getX() + getY();
}
}
enum Enum1 implements Addable {
A(1, 1), B(2, 2);
private final int x, y;
Enum1(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
}
然后您可以进行类似于Enum1.B.add()
的调用,该调用将给出4
。至于你的考试班:
public class Test {
void method(Addable e) { e.add(); }
}
现在对实现可添加的的任何内容都有效
如果您担心代码膨胀,我建议使用:
注意:类型名称应以大写字母开头enum1
和test
应该是enum1
和test
有一件事是肯定的:只要x
和y
是私有的,并且没有办法从类外读取它们,您就必须为每个enum
实现相同的方法。如果允许对枚举中的这些字段添加访问器进行合理更改,则可以使用接口的default
方法来实现伞式行为:
interface Addable {
int getX();
int getY();
default int add() {
return getX() + getY();
}
}
enum Enum1 implements Addable {
A(1, 1), B(2, 2);
private final int x, y;
Enum1(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
}
然后您可以进行类似于Enum1.B.add()
的调用,该调用将给出4
。至于你的考试班:
public class Test {
void method(Addable e) { e.add(); }
}
现在对实现可添加的的任何内容都有效
如果您担心代码膨胀,我建议使用:
注意:类型名称应以大写字母开头enum1
和test
应该是enum1
和test
创建一个由每个enum实现的接口,只有“问题”方法必须是公共的:
public interface Inter {
public void func();
}
public enum Enum1 implements Inter {
A(1,1),
B(2,2);
private final int x,y;
Enum1(int x,int y) {
this.x=x;
this.y=y;
}
@Overwrite
public int func() { return this.x+this.y; }
}
public enum Enum2 implements Inter {
...
@Overwrite
public int func() { ... }
}
public class Test {
void method(Inter e) {
// something using func
int sum = e.func();
}
}
创建由每个枚举实现的接口,只有“问题”方法必须是公共的:
public interface Inter {
public void func();
}
public enum Enum1 implements Inter {
A(1,1),
B(2,2);
private final int x,y;
Enum1(int x,int y) {
this.x=x;
this.y=y;
}
@Overwrite
public int func() { return this.x+this.y; }
}
public enum Enum2 implements Inter {
...
@Overwrite
public int func() { ... }
}
public class Test {
void method(Inter e) {
// something using func
int sum = e.func();
}
}
那么,创建一个接口并让两个枚举实现它?func在两个枚举中的实现是相同的。如果添加更多这样的枚举,那么函数需要实现多次,我不想这样做,因为函数依赖于私有字段,所以需要重写。如果它是一个复杂的函数,您可以在某个地方编写一个静态方法,它实现基于参数的所有逻辑。但如果不编写特定方法,则无法访问私有成员。是否必须使用枚举?如果你使用标准类,你可以有一个抽象的基类,你可以把常见的行为。我必须使用枚举。因为在实际的用例中,我没有像A,B这样的两个实例。我有将近50个实例。好吧,创建一个接口,让两个枚举实现它?func在两个枚举中的实现是相同的。如果添加更多这样的枚举,那么函数需要实现多次,我不想这样做,因为函数依赖于私有字段,所以需要重写。如果它是一个复杂的函数,您可以在某个地方编写一个静态方法,它实现基于参数的所有逻辑。但如果不编写特定方法,则无法访问私有成员。是否必须使用枚举?如果你使用标准类,你可以有一个抽象的基类,你可以把常见的行为。我必须使用枚举。因为在实际的用例中,我没有像A,B这样的两个实例。我有将近五十个例子。