Initialize a new object from class in Enum

前端 未结 8 578
轻奢々
轻奢々 2021-01-03 00:27

I have an Enum called Plugins:

public enum Plugins {

    ROTATING_LINE (plugin.rotatingline.RotatingLine.class),
    SNOW_SYSTEM (plugin.snow.SnowSystem.cla         


        
8条回答
  •  情话喂你
    2021-01-03 01:27

    There are 2 ways:

    1. use Enum.valueOf() static function, then cast it into your enum type.

      Enum v = Enum.valueOf(TheEnumClass, valueInString);
      
    2. Use class.getEnumConstants() function to get the list of the enum constants, and loop this list and get.

      Plugins[] plugins = Plugins.class.getEnumConstants();
      for (Plugins plugin: plugins) {
          // use plugin.name() to get name and compare
      }
      

提交回复
热议问题