使用参数作为界面项

在我正在编写的程序中,我试图获取给定季节的格式化季节名称(格式化后的格式。我将格式化名称保留在界面中,因为如果要使用地图,它将被不必要地重新生成,因为我没有创建TeamBuilder的实例


季节界面:


public interface Seasons {

    /*

     * Contains a formatted list of seasons.

     * 

     * An interface is being used as an alternative to using a Map in the 

     * TeamBuilder class,  since calling parseTeam would have to build 

     * mappings for the seasons each time it

     * was called. This way, the formatted name can simply be grabbed

     */

    final String Skyrise = "Skyrise";

    final String Toss_Up = "Toss%20Up";

    final String Sack_Attack = "Sack%20Attack";

    final String GateWay = "Gateway";

    final String Round_Up = "Round%20Up";

    final String Clean_Sweep = "Clean%20Sweep";

    final String Elevation = "Elevation";

    final String Bridge_Battle = "Bridge%20Battle";

    final String Nothing_But_Net = "Nothing%20But%20Net";

    final String Starstruck = "Starstruck";

    final String In_The_Zone = "In%20The%20Zone";

    final String Turning_Point = "Turning%20Point";

}

当我尝试抓住这些季节时,问题就来了。我的TeamBuilder类接受一个无格式的参数(字符串季节)。我的问题是,有什么方法可以使用String参数作为方法从接口获取特定项?这是使用HashMap最可取的方法,因为HashMap会不必要地重新生成相同的信息


慕姐8265434
浏览 156回答 2
2回答

BIG阳

如果要以键入方式进行操作,则可以使用Enum进行此操作:enum Season{&nbsp; &nbsp; Skyrise,Toss_Up, Sack_Attack;&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; switch(this){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case Skyrise: return "Skyrise";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case Toss_Up: return "Toss%20Up";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case Sack_Attack: return "Sack_Attack";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default: return "";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public class main{&nbsp; &nbsp; public static void printSeason(Seasons seasons){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(seasons);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Seasons e = Seasons.Skyrise;&nbsp; &nbsp; &nbsp; &nbsp; printSeason(e);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e);&nbsp; &nbsp; }}由于编译器在内部调用toString(),因此可以像我的示例一样将参数作为Seasons或String传递。而且,如果您仍想使用没有“不必要地重新生成”的地图,则可以将静态字段与静态初始值设定项一起使用,如下所示:class Seasons {&nbsp; &nbsp; private static Map<String,String> map = new HashMap<>();&nbsp; &nbsp; static {&nbsp; &nbsp; &nbsp; &nbsp; map.put("Skyrise", "Skyrise");&nbsp; &nbsp; &nbsp; &nbsp; map.put("Toss_Up", "Toss%20Up");&nbsp; &nbsp; }&nbsp; &nbsp; public static String getFormatted(String key){&nbsp; &nbsp; &nbsp; &nbsp; return map.getOrDefault(key,"");&nbsp; &nbsp; }}class main{&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Seasons.getFormatted("Skyrise"));&nbsp; &nbsp; }}

ibeautiful

只是为了与Snoob答案集成,您可以使用字段枚举,因此:enum Season{&nbsp; &nbsp;Skyrise("Skyrise"),&nbsp; &nbsp;Toss_Up("Toss%20Up"),&nbsp;&nbsp; &nbsp;Sack_Attack("Sack%20Attack")&nbsp; &nbsp;;&nbsp; &nbsp;public final String fancyName;&nbsp; &nbsp;private Season(String fancyName)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp;this.fancyName = fancyName;&nbsp; &nbsp;}}您确实拥有所有好处,而没有任何缺点。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java