(1) 关于Java Enum:学过C/C++等语言的人,应该都对Enum类型略知一二。JDK1.5之前没有Enum这个类型,那时候一般用接口常量来替代。有了JavaEnum之后,可以更贴近的表示这种常量。
(2)如何使用Java Enum。
简单的用法:JavaEnum简单的用法一般用于代表一组常用常量。如:
性别:
public enum SexEnum {
male, female;
}
颜色:
public enum Color {
RED, BLUE,GREEN,BLACK;
}
枚举对象里面的值都必须是唯一的。可以通过Enum类型名直接引用该常量,如SexEnum.male,Color.RED.
复杂用法:Java为枚举类型提供了一些内置的方法,同时枚举常量还可以有自己的方法,可以很方便的遍历枚举对象。
(3)实例
enum类文件WeekDay.java:
public enum WeekDay { Mon("Monday"), //两个应该都是String类型 Tue("Tuesday"), Wed("Wednesday"), Thu("Thursday"), Fri("Friday"), Sat("Saturday"), Sun("Sunday"); private final String day; private WeekDay(String day) { //enum类的构造函数 this.day = day; } public static void printDay(int i){ switch(i){ case 1: System.out.println(WeekDay.Mon); break; case 2: System.out.println(WeekDay.Tue);break; case 3: System.out.println(WeekDay.Wed);break; case 4: System.out.println(WeekDay.Thu);break; case 5: System.out.println(WeekDay.Fri);break; case 6: System.out.println(WeekDay.Sat);break; case 7: System.out.println(WeekDay.Sun);break; //输出的其实是enum的值,而不是成员对应的string值 default:System.out.println("wrong number!"); } } public String getDay() { return day; } }
测试文件 WeekDayTest.java:
public class WeekDayTest { public static void main(String[] args) { // TODO Auto-generated method stub for (WeekDay day : WeekDay.values()) { //循环计数 System.out.println(day + "====>" + day.getDay()); //enum值 enum对应的string值 } WeekDay.printDay(5); } }
执行结果是:
Mon====>Monday
Tue====>Tuesday
Wed====>Wednesday
Thu====>Thursday
Fri====>Friday
Sat====>Saturday
Sun====>Sunday
Fri
(4)java enum本质
Java Enum类型的语法结构尽管和java类的语法不一样,但是经过编译器编译之后产生的是一个class文件。该class文件经过反编译可以看到实际上是生成了一个类,该类继承了java.lang.Enum。
public final class WeekDay extends java.lang.Enum{
public static final WeekDay Mon;
public static final WeekDay Tue;
public static final WeekDay Wed;
public static final WeekDay Thu;
public static final WeekDay Fri;
public static final WeekDay Sat;
public static final WeekDay Sun;
static {};
public static void printDay(int);
public java.lang.String getDay();
public static WeekDay[] values();
public static WeekDay valueOf(java.lang.String);
}
所以实际上Enum类型就是以Java类来实现的,没有什么新的特点,只不过java编译器帮我们做了语法的解析和编译。
(5)java的ordinal函数
Enum类提供了一个ordinal()方法,用来返回枚举对象的序数。
private static enum gene_actions{
ACTION0,
ACTION1,
ACTION2,
ACTION3,
}
public final static int INVALID_ACTION = gene_actions.ACTION0.ordinal();
public final static int SMART_HANDFREE_ACTION = gene_actions.ACTION1.ordinal();
public final static int ANTI_EAVESDROP_ACTION = gene_actions.ACTION2.ordinal();
public final static int DIRECT_CALL_ACTION = gene_actions.ACTION3.ordinal();
得到序数值就是0 1 2 3
================================================================================================================
在ECLIPSE中的某个工程使用json,需要导入额外的json包,比如json-20090211.jar。从网上下到这个库之后,放到工程目录下,在project导入的方法是:点击工程右键-》属性-》Java build path-》libraries,再点击右边的add external JARs,选中刚才放置的Jar包就可以了。
比如如下的工程,从网站地理位置数据中返回特定数据
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.nio.charset.Charset; import org.json.JSONException; import org.json.JSONObject; /** * java根据url获取json对象 * @author openks * @since 2013-7-16 * 需要添加java-json.jar才能运行 */ public class TestJava { private static String readAll(Reader rd) throws IOException { StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } return sb.toString(); } public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { InputStream is = new URL(url).openStream(); try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONObject json = new JSONObject(jsonText); return json; } finally { is.close(); // System.out.println("同时 从这里也能看出 即便return了,仍然会执行finally的!"); } } public static void main(String[] args) throws IOException, JSONException { //这里调用百度的ip定位api服务 详见 http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htm JSONObject json = readJsonFromUrl("http://api.map.baidu.com/location/ip?ak=F454f8a5efe5e577997931cc01de3974&ip=202.198.16.3"); System.out.println(json.toString()); System.out.println(); System.out.println(((JSONObject) json.get("content")).get("address")); } }
执行结果是
{"content":{"point":{"y":"5419815.34","x":"13950002.65"},"address":"吉林省长春市","address_detail":{"street":"","province":"吉林省","city_code":53,"street_number":"","district":"","city":"长春市"}},"status":0,"address":"CN|吉林|长春|None|CERNET|0|0"} 吉林省长春市
参考原文:http://blog.sina.com.cn/s/blog_60452f7d0100it3h.html
参考原文:http://malinhu.blog.51cto.com/3568860/701927