以下简单的代码段使用包含两个方法Named
和name()
的接口order()
,该方法由名为enum
的{{1}}实现。 Days
不允许实现其实现接口的enum Days
方法。这样做会导致编译时错误,虽然name()
没有实现name()
方法,但它不会发出任何错误。
enum
代码无法在package enumpkg;
interface Named
{
public String name();
public int order();
}
enum Days implements Named
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday;
public int order()
{
return ordinal()+1;
}
}
final public class Main
{
public static void main(String[] args)
{
System.out.println("Monday = "+Days.Monday.order());
System.out.println("Tuesday = "+Days.Tuesday.order());
System.out.println("Wednesday = "+Days.Wednesday.order());
System.out.println("Thursday = "+Days.Thursday.order());
System.out.println("Friday = "+Days.Friday.order());
System.out.println("Saturday = "+Days.Saturday.order());
System.out.println("Sunday = "+Days.Sunday.order());
}
}
中实施name()
方法。怎么样?
输出非常明显,如下所示。
enum