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

31
Background

Upload: rey-richman

Post on 14-Dec-2015

254 views

Category:

Documents


5 download

TRANSCRIPT

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

Background

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

Background

7 月 20 日

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

Background

7 月 20 日

我原以为这是平凡的一天

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

Background

但我举了不该举的手

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

Background

但我举了不该举的手

说了不该说的话

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

Background

但我举了不该举的手

说了不该说的话

听了不该听的怂恿

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

这是轮子 —— Java 泛型完善

2011 级 ACM 班 陈志鹏

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

Outline

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

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

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 日 我原以为这是平凡的一天

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 日 我原以为这是平凡的一天

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 日 我原以为这是平凡的一天

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 日 我原以为这是平凡的一天

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 日 我原以为这是平凡的一天

Motivation & Criteria

• 完善 Java 的泛型• 基于 Java• 封装与安全

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

Design & Implementation

• 所有类继承 Object• 多态• 反射

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

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 日 我原以为这是平凡的一天

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 日 我原以为这是平凡的一天

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 日 我原以为这是平凡的一天

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 日 我原以为这是平凡的一天

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 日 我原以为这是平凡的一天

Advancement

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

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

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 日 我原以为这是平凡的一天

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 日 我原以为这是平凡的一天

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 日 我原以为这是平凡的一天

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 日 我原以为这是平凡的一天

Discussion & Conclusion

• Generic Programming– 1980 Ada -> 1983 C++– 2004 jdk 1.5

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

Discussion & Conclusion

• Generic Programming– 1980 Ada -> 1983 C++– 2004 jdk 1.5

• WHY?

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

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 日 我原以为这是平凡的一天

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 日 我原以为这是平凡的一天

Reference

• 《 Java 核心技术》…• Wiki…

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

END

THANKS