310 likes | 870 Views
Background. Background. 7 月 20 日. Background. 7 月 20 日. 我原以为这是平凡的一天. Background. 但我举了不该举的手. Background. 但我举了不该举的手. 说了不该说的话. Background. 但我举了不该举的手. 说了不该说的话. 听了不该听的怂恿. 这是轮子 —— Java 泛型完善. 2011 级 ACM 班 陈志鹏. Outline. Background Motivation & Criteria
E N D
Background 7月20日
Background 7月20日 我原以为这是平凡的一天
Background 但我举了不该举的手
Background 但我举了不该举的手 说了不该说的话
Background 但我举了不该举的手 说了不该说的话 听了不该听的怂恿
这是轮子 ——Java泛型完善 2011级ACM班 陈志鹏
Outline • Background • Motivation & Criteria • Design & Implementation • One More Example • Advancement • Discussion & Conclusion • Reference
Background class cat { public: void say() { printf("I am a cat.\n"); } }; class mouse { public: void say() { printf("I am a mouse.\n"); } }; template <class T> void say(T a) { a.say(); } int main() { cat acat; mouse amouse; say(acat); say(amouse); return 0; } C++
Background class cat { public void say() { System.out.println ("I am a cat.\n"); } }; class mouse { public void say() { System.out.println ("I am a mouse.\n"); } }; public class example { public static <T> void say(T a) { a.say(); } Public static void main(String[] arg) { cat acat; mouse amouse; say(acat); say(amouse); return 0; } } Java
Background class cat { public void say() { System.out.println ("I am a cat.\n"); } }; class mouse { public void say() { System.out.println ("I am a mouse.\n"); } }; public class example { public static <T> void say(T a) { a.say(); } Public static void main(String[] arg) { cat acat; mouse amouse; say(acat); say(amouse); return 0; } } The method say() is undefined for type T Java
Background class cat { public void say() { System.out.println ("I am a cat.\n"); } }; class mouse { public void say() { System.out.println ("I am a mouse.\n"); } }; public class example { public static <T> void say(T a) { a.say(); } Public static void main(String[] arg) { cat acat; mouse amouse; say(acat); say(amouse); return 0; } } The method say() is undefined for type T Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method say() is undefined for the type T Java
Background class cat { public void say() { System.out.println ("I am a cat.\n"); } }; class mouse { public void say() { System.out.println ("I am a mouse.\n"); } }; public class example { public static <T> void say(T a) { a.say(); } Public static void main(String[] arg) { cat acat; mouse amouse; say(acat); say(amouse); return 0; } } Java
Motivation & Criteria • 完善Java的泛型 • 基于Java • 封装与安全
Design & Implementation • 所有类继承Object • 多态 • 反射
Design & Implementation public static <T> void say(T a) { Class<?> demo = a.getClass(); try { Method met = demo.getMethod(“say”); met.invoke(a); } catch (Exception ex) {} } say(acat); Java
One More Example class cat { public : int key; }; class mouse { public : int key; }; template <class T> boolcmp(T a, T b) { return a.key < b.key; } int main() { cat cats[100]; mouse mice[100]; //Initialization… sort(cats, cats + 100, cmp); sort(mice, mice + 100, cmp); //Output… } C++
One More Example class cat { public : int key; }; class mouse { public : int key; }; template <class T> boolcmp(T a, T b) { return a.key < b.key; } int main() { cat cats[100]; mouse mice[100]; //Initialization… sort(cats, cats + 100, cmp); sort(mice, mice + 100, cmp); //Output… } error: no matching function for call to `sort(cat[100], cat*, <unknown type>)' C++
One More Example class cat { public : int key; }; class mouse { public : int key; }; template <class T> boolcmp(T a, T b) { return a.key < b.key; } int main() { cat cats[100]; mouse mice[100]; //Initialization… sort(cats, cats + 100, cmp); sort(mice, mice + 100, cmp); //Output… } C++
One More Example public static class comp implements Comparator{ public int compare(Object o1, Object o2) { Class<?> demo = o1.getClass; Object a = null, b = null; try { Field field = demo.getDeclaredField("key"); a = field.get(o1); b = field.get(o2); } catch (Exception ex) { } return (int)a - (int)b; }} public static void main(String[] arg) { //Initialization… Arrays.sort(cats, new comp()); //Output… } 泛型在这里 Java 那些C++做不到的事
Advancement • 多参数 • 返回值 • 良好封装 • 权限修饰符 public & private • 异常处理
Advancement • 多参数 • public static void say(T a, Class[] cla, Object… objs) • Method met = demo.getMethod(“…”, cla); • met.invoke(a, objs); • say(acat, new Class[] {String.class, String.class}, “Hi”, “I am a cat”) • 返回值 • 良好封装 • 权限修饰符public&private • 异常处理 Class[] cla = new Class[objs.length] cla[i] = objs[i].getClass; Java
Advancement • 多参数 • 返回值 • public static int say(T a) • return met.invoke(a); • intans = say(acat); • 良好封装 • 权限修饰符public&private • 异常处理 Java
Advancement 良好封装 public static <T, S> S run(T a, String method, Class[] cla, Object… objs) { Class<?> x = a.getClass(); Object ans = null; try { Method met = x.getMethod(method, cla); ans = met.invoke(a, objs); } catch (Exception ex) {} return (S) ans; } returnValue = run(acat, “say”, new Class[] {String.class, String.class}, “Hi”, “I am a cat.”); Java
Advancement • 多参数 • 返回值 • 良好封装 • 权限修饰符public&private • int mo = field.getModifiers(); • String priv = Modifier.toString(mo); • int mo = met.getModifiers(); • 异常处理 Java
Discussion & Conclusion • Generic Programming • 1980 Ada-> 1983 C++ • 2004jdk 1.5
Discussion & Conclusion • Generic Programming • 1980 Ada -> 1983 C++ • 2004jdk 1.5 • WHY?
Discussion & Conclusion • Generic Programming • 1980 Ada -> 1983 C++ • 2004JDK 1.5 & J2SE 5.0 • WHY? • No requirement • Impact of mainstream • Safety & Elegant Can be implemented by other ways Functional programming CLU
Discussion & Conclusion • Generic Programming • 1980 Ada -> 1983 C++ • 2004JDK 1.5 & J2SE 5.0 • WHY? • No requirement • Impact of mainstream • Safety & Elegant • Other generic features Can be implemented by other ways Functional programming CLU ?通配符
Reference • 《Java核心技术》… • Wiki…
END THANKS