The enumeration class that implements the interface [fine]

The enumeration class that implements the interface [fine]

I. Introduction

Enumeration class and common class implements an interface, like it?

Second, enumeration class that implements the interface

The enumeration class that implements the interface with the general class that implements the interface is the same, the abstract interface methods are required to implement.

but! !

With an abstract method enumeration class that implements the interface, the abstract methods different behaviors in different target enumeration class. How to achieve that?

simple

As long as each object enumeration class are overridden method can do different objects to achieve different behavior of the abstract method.

Third, the class implements the interface instances enumerated

  • Define an interface
/**
 * 定义一个接口
 */
interface Info {
    void describe();
}
  • Creates an enum class that implements this interface
/**
 *  枚举类实现接口
 */
public enum EnumImplement implements Info{  }
  • Rewrite interface methods
/**
 *  枚举类实现接口重写抽象方法
 */
public enum EnumImplement implements Info{

    /**
     *  需求:实现接口,在不同的对象实现抽象方法不同的行为。
     *  实现方法:将接口的抽象方法在每个对象中重写实现不同的行为
     */
    SPRING("春天"){
        @Override
        public void describe() {
            System.out.println("春江水阿暖鸭先知");
        }
    },
    SUMMER("夏天"){
        @Override
        public void describe() {
            System.out.println("我爱山中夏,空冥花雨下。");
        }
    },
    AUTUMN("秋天"){
        @Override
        public void describe() {
            System.out.println("停车坐爱枫林晚,霜叶红于二月花。");
        }
    },
    WINTER("冬天"){
        @Override
        public void describe() {
            System.out.println("北国风光,千里冰封,万里雪飘。");
        }
    };
  •  Achieve a complete enumeration class instance of an interface

/**
 * 定义接口
 */
interface Info {
    void describe();
}

/**
 *  枚举类实现接口的抽象方法
 */
public enum EnumImplement implements Info{

    /**
     *  需求:接口的抽象方法,在不同的对象实现不同的行为。
     *  实现方法:将接口的抽象方法在每个对象中重写实现不同的行为
     */
    SPRING("春天"){
        @Override
        public void describe() {
            System.out.println("春江水阿暖鸭先知");
        }
    },
    SUMMER("夏天"){
        @Override
        public void describe() {
            System.out.println("我爱山中夏,空冥花雨下。");
        }
    },
    AUTUMN("秋天"){
        @Override
        public void describe() {
            System.out.println("停车坐爱枫林晚,霜叶红于二月花。");
        }
    },
    WINTER("冬天"){
        @Override
        public void describe() {
            System.out.println("北国风光,千里冰封,万里雪飘。");
        }
    };

    //申明 EnumTest 对象的属性
    final String name;

    //提供有参构造器
    EnumImplement(String name) {
        this.name = name;
    }

    // 获取类对象属性
    public String getName() {
        return name;
    }
}
  •  Define the test class to obtain enumerated classes of objects and object properties
public class App {
    public static void main(String[] args) {

        //获取 EnumImplement 所有对象
        EnumImplement[] values = EnumImplement.values();
        for (EnumImplement v : values) {
            //获取对象属性
            System.out.println(v.getName());
            v.describe();
        }
    }
}
  • Gets an enumeration of the class object and property results
春天
春江水阿暖鸭先知
夏天
我爱山中夏,空冥花雨下。
秋天
停车坐爱枫林晚,霜叶红于二月花。
冬天
北国风光,千里冰封,万里雪飘。

 

Published 316 original articles · won praise 117 · views 420 000 +

Guess you like

Origin blog.csdn.net/m0_38039437/article/details/104898390