1 / 31

Background

Background. Background. 7 月 20 日. Background. 7 月 20 日. 我原以为这是平凡的一天. Background. 但我举了不该举的手. Background. 但我举了不该举的手. 说了不该说的话. Background. 但我举了不该举的手. 说了不该说的话. 听了不该听的怂恿. 这是轮子 —— Java 泛型完善. 2011 级 ACM 班 陈志鹏. Outline. Background Motivation & Criteria

lida
Télécharger la présentation

Background

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Background

  2. Background 7月20日

  3. Background 7月20日 我原以为这是平凡的一天

  4. Background 但我举了不该举的手

  5. Background 但我举了不该举的手 说了不该说的话

  6. Background 但我举了不该举的手 说了不该说的话 听了不该听的怂恿

  7. 这是轮子 ——Java泛型完善 2011级ACM班 陈志鹏

  8. Outline • Background • Motivation & Criteria • Design & Implementation • One More Example • Advancement • Discussion & Conclusion • Reference

  9. 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++

  10. 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

  11. 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

  12. 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

  13. 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

  14. Motivation & Criteria • 完善Java的泛型 • 基于Java • 封装与安全

  15. Design & Implementation • 所有类继承Object • 多态 • 反射

  16. 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

  17. 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++

  18. 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++

  19. 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++

  20. 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++做不到的事

  21. Advancement • 多参数 • 返回值 • 良好封装 • 权限修饰符 public & private • 异常处理

  22. 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

  23. Advancement • 多参数 • 返回值 • public static int say(T a) • return met.invoke(a); • intans = say(acat); • 良好封装 • 权限修饰符public&private • 异常处理 Java

  24. 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

  25. Advancement • 多参数 • 返回值 • 良好封装 • 权限修饰符public&private • int mo = field.getModifiers(); • String priv = Modifier.toString(mo); • int mo = met.getModifiers(); • 异常处理 Java

  26. Discussion & Conclusion • Generic Programming • 1980 Ada-> 1983 C++ • 2004jdk 1.5

  27. Discussion & Conclusion • Generic Programming • 1980 Ada -> 1983 C++ • 2004jdk 1.5 • WHY?

  28. 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

  29. 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 ?通配符

  30. Reference • 《Java核心技术》… • Wiki…

  31. END THANKS

More Related