Java Enumeration (枚举类型) (4) -- 实现接口

本文介绍如何让枚举类型实现多个接口,并通过具体示例展示了枚举类型结合接口的使用方法。示例中定义了两个接口Abbrevable和Multiplierable,以及一个枚举类型Prefix来实现这两个接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个枚举类型是不能extends任何类或另一个枚举类型的。但是可以实现一个或多个接口。例如
package implinterface;

public interface Abbrevable
{
    String abbrev();
}

package implinterface;

public interface Multiplierable
{
    
double multiplier();
}

package implinterface;
public enum Prefix implements Abbrevable, Multiplierable
{
    
// These are the values of this enumerated type.
    
// Each one is followed by constructor arguments in parentheses.
    
// The values are separated from each other by commas, and the
    
// list of values is terminated with a semicolon to separate it from
    
// the class body that follows.
    MILLI("m", .001), 
    CENTI(
"c", .01), 
    DECI(
"d"), 
    DECA(
"D"10.0), 
    HECTA(
"h"100.0), 
    KILO(
"k"1000.0);  // Note semicolon

                                                                                                                                                                                                                        
// semicolon

    
// This is the constructor invoked for each value above.
    private Prefix(String abbrev, double multiplier)
    {
        
this.abbrev = abbrev;
        
this.multiplier = multiplier;
    }
    
    
//Another constructor
    private Prefix(String abbrev)
    {
        
this.abbrev = abbrev;
        
this.multiplier = .1;
    }
    
    
// These are the private fields set by the constructor
    private String abbrev;
    
private double multiplier;

    
// These are accessor methods for the fields. They are instance methods
    
// of each value of the enumerated type.
    public String abbrev()
    {
        
return abbrev;
    }

    
public double multiplier()
    {
        
return multiplier;
    }
}


package implinterface;
public class TestPrefix
{
    
public static void main(String[] args)
    {
        Prefix p 
= Prefix.MILLI;
        System.out.println(p.abbrev());
        System.out.println(Prefix.
class);
    }
}


在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、付费专栏及课程。

余额充值