`

实例178 - 选择合适的枚举元素

 
阅读更多

心法领悟178:枚举类型与switch语句。

package com.mingrisoft.enums;

public enum JDBCInfo {
    DRIVER, URL, USERNAME, PASSWORD;
    
    public String getJDBCInfo(JDBCInfo info) {
        switch (info) {
            case DRIVER:
                return "com.mysql.jdbc.Driver";
            case URL:
                return "jdbc:mysql://localhost:3306/db_database";
            case USERNAME:
                return "mr";
            case PASSWORD:
                return "mingrisoft";
            default:
                return null;
        }
    }
    
    public static void main(String[] args) {
        for (JDBCInfo info : JDBCInfo.values()) {
            System.out.println(info + ":" + info.getJDBCInfo(info));
        }
    }
}

 

//Output:
DRIVER:com.mysql.jdbc.Driver
URL:jdbc:mysql://localhost:3306/db_database
USERNAME:mr
PASSWORD:mingrisoft

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics