Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.
-
Upload
rey-richman -
Category
Documents
-
view
255 -
download
5
Transcript of Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.
![Page 1: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/1.jpg)
Background
![Page 2: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/2.jpg)
Background
7 月 20 日
![Page 3: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/3.jpg)
Background
7 月 20 日
我原以为这是平凡的一天
![Page 4: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/4.jpg)
Background
但我举了不该举的手
![Page 5: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/5.jpg)
Background
但我举了不该举的手
说了不该说的话
![Page 6: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/6.jpg)
Background
但我举了不该举的手
说了不该说的话
听了不该听的怂恿
![Page 7: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/7.jpg)
这是轮子 —— Java 泛型完善
2011 级 ACM 班 陈志鹏
![Page 8: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/8.jpg)
Outline
• Background• Motivation & Criteria• Design & Implementation• One More Example• Advancement• Discussion & Conclusion• Reference
![Page 9: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/9.jpg)
class cat{public: void say() { printf("I am a cat.\n"); }};class mouse{public: void say() { printf("I am a mouse.\n"); }};
Backgroundtemplate <class T>void say(T a) { a.say();}int main() { cat acat; mouse amouse; say(acat); say(amouse); return 0;}
C++
C++
![Page 10: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/10.jpg)
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"); }};
Backgroundpublic 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
![Page 11: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/11.jpg)
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"); }};
Backgroundpublic 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
The method say() is undefined for type T
![Page 12: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/12.jpg)
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"); }};
Backgroundpublic 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
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
![Page 13: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/13.jpg)
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"); }};
Backgroundpublic 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
![Page 14: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/14.jpg)
Motivation & Criteria
• 完善 Java 的泛型• 基于 Java• 封装与安全
![Page 15: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/15.jpg)
Design & Implementation
• 所有类继承 Object• 多态• 反射
![Page 16: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/16.jpg)
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
![Page 17: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/17.jpg)
One More Exampleclass cat {public : int key;};class mouse {public : int key;};template <class T>bool cmp(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++
![Page 18: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/18.jpg)
One More Exampleclass cat {public : int key;};class mouse {public : int key;};template <class T>bool cmp(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++
error: no matching function for call to
`sort(cat[100], cat*, <unknown type>)'
![Page 19: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/19.jpg)
One More Exampleclass cat {public : int key;};class mouse {public : int key;};template <class T>bool cmp(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++
![Page 20: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/20.jpg)
One More Examplepublic 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…}
那些 C++ 做不到的事
泛型在这里
Java
![Page 21: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/21.jpg)
Advancement
• 多参数• 返回值• 良好封装• 权限修饰符 public & private• 异常处理
![Page 22: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/22.jpg)
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
![Page 23: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/23.jpg)
Advancement
• 多参数• 返回值– public static int say(T a)– return met.invoke(a);– int ans = say(acat);
• 良好封装• 权限修饰符 public&private• 异常处理 Java
![Page 24: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/24.jpg)
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.”);
![Page 25: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/25.jpg)
Advancement
• 多参数• 返回值• 良好封装• 权限修饰符 public&private– int mo = field.getModifiers(); – String priv = Modifier.toString(mo); – int mo = met.getModifiers();
• 异常处理 Java
![Page 26: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/26.jpg)
Discussion & Conclusion
• Generic Programming– 1980 Ada -> 1983 C++– 2004 jdk 1.5
![Page 27: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/27.jpg)
Discussion & Conclusion
• Generic Programming– 1980 Ada -> 1983 C++– 2004 jdk 1.5
• WHY?
![Page 28: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/28.jpg)
Discussion & Conclusion
• Generic Programming– 1980 Ada -> 1983 C++– 2004 JDK 1.5 & J2SE 5.0
• WHY?– No requirement– Impact of mainstream– Safety & Elegant
Functional programmingCLU
Can be implemented by other ways
![Page 29: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/29.jpg)
Discussion & Conclusion
• Generic Programming– 1980 Ada -> 1983 C++– 2004 JDK 1.5 & J2SE 5.0
• WHY?– No requirement– Impact of mainstream– Safety & Elegant
• Other generic features
Functional programmingCLU
Can be implemented by other ways
? 通配符
![Page 30: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/30.jpg)
Reference
• 《 Java 核心技术》…• Wiki…
![Page 31: Background. 7 月 20 日 Background 7 月 20 日 我原以为这是平凡的一天.](https://reader033.fdocuments.net/reader033/viewer/2022061520/56649c7d5503460f94931aec/html5/thumbnails/31.jpg)
END
THANKS