一个枚举类型是不能extends任何类或另一个枚举类型的。但是可以实现一个或多个接口。例如
package
implinterface;
public interface Abbrevable
{
String abbrev();
}
public interface Abbrevable
{
String abbrev();
}
package
implinterface;
public interface Multiplierable
{
double multiplier();
}
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;
}
}
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 );
}
}
public class TestPrefix
{
public static void main(String[] args)
{
Prefix p = Prefix.MILLI;
System.out.println(p.abbrev());
System.out.println(Prefix. class );
}
}