要運行這個程序,你首先需要一個public static void main(String[])
方法的初學者。這是您進入任何Java程序的入口點。因爲,您想要分配callPro
中的值,請在那裏添加main()
方法。
接下來,你要調用getProperty()
這是屬於prop
類的實例方法,所以你需要創建它首先使用新的構造函數()語法的一個實例。
class callPro {
private static String name;
private static String city;
private static String address;
public static void main(String[] args) {
// create prop instance
prop property = new prop();
// call prop's method getValue()
name = property.getValue(prop.property.CITY);
city = property.getValue(prop.property.NAME);
address = property.getValue(prop.property.ADDRESS);
// New York, John, Central Park
System.out.println(name + ", " + city + ", " + address);
}
}
請注意,我是如何使callPro
的成員static
才能夠訪問這些main()
方法中,因爲這是static
了。此外,請注意我如何參考Enums:className.enumType.enumValue。
爲了能夠看到的值從main()
方法打印,您還可以通過枚舉需要爲您的prop
類成員提供值
private String NAME = "John";
private String CITY = "New York";
private String ADDRESS = "Central Park";
public String getValue(property pro) {
switch (pro) {
case NAME:
return NAME;
case CITY:
return CITY;
case ADDRESS:
return ADDRESS;
}
return null;
}
是的,你可以循環的價值觀,並在一個循環中檢索您的屬性
prop property = new prop();
for (prop.property prop : prop.property.values()) {
System.out.println(property.getValue(prop));
}
enumType.v ()返回一個enumType []所有enumValues它可以與for-each
循環一起使用,如上所示。
您是否知道[this](http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html)Oracle網站?你試圖做什麼btw?你不能使用http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#name()嗎? – Fildor 2015-02-23 13:53:11
請格式化您的代碼以使其可讀。此外,使用通用編碼約定 - 如類名稱以大寫字母開頭等。 – BluesSolo 2015-02-23 13:58:14
@BluesSolo我編輯了代碼,希望現在可以。 :) – 2015-02-23 14:06:56