java 枚举的继承_枚举“继承”

我在低级名称空间中有一个枚举。 我想在“继承”低级枚举的中级命名空间中提供一个类或枚举。

namespace low

{

public enum base

{

x, y, z

}

}

namespace mid

{

public enum consume : low.base

{

}

}

我希望这是可能的,或者希望可以使用某种类来代替枚举消耗,这将为枚举提供抽象层,但仍允许该类的实例访问枚举。

有什么想法吗?

编辑:我没有将其切换为类中的const的原因之一是我必须使用的服务需要低级枚举。 我得到了WSDL和XSD,它们将结构定义为枚举。 该服务无法更改。

#1楼

我知道这个答案有点晚了,但这就是我最终要做的事情:

public class BaseAnimal : IEquatable

{

public string Name { private set; get; }

public int Value { private set; get; }

public BaseAnimal(int value, String name)

{

this.Name = name;

this.Value = value;

}

public override String ToString()

{

return Name;

}

public bool Equals(BaseAnimal other)

{

return other.Name == this.Name && other.Value == this.Value;

}

}

public class AnimalType : BaseAnimal

{

public static readonly BaseAnimal Invertebrate = new BaseAnimal(1, "Invertebrate");

public static readonly BaseAnimal Amphibians = new BaseAnimal(2, "Amphibians");

// etc

}

public class DogType : AnimalType

{

public static readonly BaseAnimal Golden_Retriever = new BaseAnimal(3, "Golden_Retriever");

public static readonly BaseAnimal Great_Dane = new BaseAnimal(4, "Great_Dane");

// etc

}

然后,我可以执行以下操作:

public void SomeMethod()

{

var a = AnimalType.Amphibians;

var b = AnimalType.Amphibians;

if (a == b)

{

// should be equal

}

// call method as

Foo(a);

// using ifs

if (a == AnimalType.Amphibians)

{

}

else if (a == AnimalType.Invertebrate)

{

}

else if (a == DogType.Golden_Retriever)

{

}

// etc

}

public void Foo(BaseAnimal typeOfAnimal)

{

}

#2楼

上面使用具有int常量的类的解决方案缺乏类型安全性。 即,您可以发明实际上在类中未定义的新值。 此外,例如不可能编写将这些类之一作为输入的方法。

你需要写

public void DoSomethingMeaningFull(int consumeValue) ...

但是,当没有枚举可用时,有一个基于Java的老式解决方案。 这提供了几乎类似于枚举的行为。 唯一的警告是这些常量不能在开关语句中使用。

public class MyBaseEnum

{

public static readonly MyBaseEnum A = new MyBaseEnum( 1 );

public static readonly MyBaseEnum B = new MyBaseEnum( 2 );

public static readonly MyBaseEnum C = new MyBaseEnum( 3 );

public int InternalValue { get; protected set; }

protected MyBaseEnum( int internalValue )

{

this.InternalValue = internalValue;

}

}

public class MyEnum : MyBaseEnum

{

public static readonly MyEnum D = new MyEnum( 4 );

public static readonly MyEnum E = new MyEnum( 5 );

protected MyEnum( int internalValue ) : base( internalValue )

{

// Nothing

}

}

[TestMethod]

public void EnumTest()

{

this.DoSomethingMeaningful( MyEnum.A );

}

private void DoSomethingMeaningful( MyBaseEnum enumValue )

{

// ...

if( enumValue == MyEnum.A ) { /* ... */ }

else if (enumValue == MyEnum.B) { /* ... */ }

// ...

}

#3楼

另一个可能的解决方案:

public enum @base

{

x,

y,

z

}

public enum consume

{

x = @base.x,

y = @base.y,

z = @base.z,

a,b,c

}

// TODO: Add a unit-test to check that if @base and consume are aligned

高温超导

#4楼

这就是我所做的。 我所做的不同之处是在“ consuming” enum上使用了相同的名称和new关键字。 由于enum的名称相同,因此您可以不加思索地使用它,它是正确的。 另外,您还将获得智能感知。 您只需在设置时手动小心,将值从基础复制过来并保持同步即可。 您可以在代码注释中提供帮助。 这是为什么在数据库中存储enum值时,我总是存储字符串而不是值的另一个原因。 因为如果您使用的是自动分配的递增整数值,则这些整数值会随着时间变化。

// Base Class for balls

public class BaseBall

{

// keep synced with subclasses!

public enum Sizes

{

Small,

Medium,

Large

}

}

public class VolleyBall : BaseBall

{

// keep synced with base class!

public new enum Sizes

{

Small = BaseBall.Sizes.Small,

Medium = BaseBall.Sizes.Medium,

Large = BaseBall.Sizes.Large,

SmallMedium,

MediumLarge,

Ginormous

}

}

#5楼

这是不可能的(就像已经提到的@JaredPar一样)。 试图使逻辑变通解决此问题是一种不好的做法。 如果您有一个具有enum的base class ,则应在此列出所有可能的enum-values ,并且该类的实现应使用它知道的值。

例如,假设您有一个基类BaseCatalog ,并且它有一个enum ProductFormats ( Digital , Physical )。 然后你就可以有一个MusicCatalog或BookCatalog可能同时包含Digital和Physical产品,但如果类是ClothingCatalog ,它应该只包含Physical产品。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在JDK1.5 之前,我们定义常量都是: publicstaticfianl.... 。现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法。 public enum Color { RED, GREEN, BLANK, YELLOW } 用法二:switch JDK1.6之前的switch语句只支持int,char,enum类型,使用枚举,能让我们的代码可读性更强。 enum Signal { GREEN, YELLOW, RED } public class TrafficLight { Signal color = Signal.RED; public void change() { switch (color) { case RED: color = Signal.GREEN; break ; case YELLOW: color = Signal.RED; break ; case GREEN: color = Signal.YELLOW; break ; } } } 用法三:向枚举中添加新方法 如果打算自定义自己的方法,那么必须在enum实例序列的最后添加一个分号。而且 Java 要求必须先定义 enum实例。 public enum Color { RED("红色" , 1 ), GREEN( "绿色" , 2 ), BLANK( "白色" , 3 ), YELLO( "黄色" , 4 ); // 成员变量 private String name; private int index; // 构造方法 private Color(String name, int index) { this .name = name; this .index = index; } // 普通方法 public static String getName( int index) { for (Color c : Color.values()) { if (c.getIndex() == index) { return c.name; } } return null ; } // get set 方法 public String getName() { return name; } public void setName(String name) { this .name = name; } public int getIndex() { return index; } public void setIndex( int index) { this .index = index; } } 用法四:覆盖枚举的方法 下面给出一个toString()方法覆盖的例子。 public enum Color { RED("红色" , 1 ), GREEN( "绿色" , 2 ), BLANK( "白色" , 3 ), YELLO( "黄色" , 4 ); // 成员变量 private String name; private int index; // 构造方法 private Color(String name, int index) { this .name = name; this .index = index; } //覆盖方法 @Override public String toString() { return this .index+ "_" + this .name; } } 用法五:实现接口 所有的枚举继承java.lang.Enum类。由于Java 不支持多继承,所以枚举对象不能再继承其他类。 public interface Behaviour { void print(); String getInfo(); } public enum Color implements Behaviour{ RED("红色" , 1 ), GREEN( "绿色" , 2 ), BLANK( "白色" , 3 ), YELLO( "黄色" , 4 ); // 成员变量 private String name; private int index; // 构造方法 private Color(String name, int index) { this .name = name; this .index = index; } //接口方法 @Override public String getInfo() { return this .name; } //接口方法 @Override public void print() { System.out.println(this .index+ ":" + this .name); } } 用法六:使用接口组织枚举 public interface Food { enum Coffee implements Food{ BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO } enum Dessert implements Food{ FRUIT, CAKE, GELATO } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值