Oop java.generics

Download Oop java.generics

Post on 27-Jul-2015

44 views

Category:

Software

0 download

Embed Size (px)

TRANSCRIPT

<p> 1. , 2004 () , 1 Java java.lang, , (generics) Java 2. , 2004 () , 2 java.lang ErrorException Comparable Cloneable Double Character Byte Boolean Compiler Float Integer Long Math NumberNullPointerException Object Package ProcessRuntimeRuntimeException Short String StringBuffer System Runnable ClassLoader Thread -target Throwable Class Void 3. , 2004 () , 3 ClassLoader Runnable Thread , Package , Class Runtime - System , public Object clone() Cloneable .Object 4. , 2004 () , 4 Number Byte, Short, Integer, Long, Float, Double , Comparable voidVoid Boolean Character , StringBuffer String 5. , 2004 () , 5 java.lang.System ( ) native (dll , ld.so ) load(), loadLibrary(), mapLibraryName() gc() exit() 00 1 1970 currentTimeMillis() () java getProperties(), getProperty(), setProperties(), setProperty() arraycopy() , err, in, out 6. , 2004 () , 6 java.lang.Runtime ( ) gc(), runFinalization() , addShutdownHook(), removeShutdownHook() native loadLibrary(), load() freeMemory() totalMemory() Shutdown java exit(int status), halt() () Process exec() Runtime getRuntime() 7. , 2004 () , 7 java.lang.Class ( ) ( , ..) jar getResource(), getResourceAsStream() , getSuperClass(), getInterfaces(), getPackage() ClassLoader getClassLoader() . . Object newInstance() static Class forName(String name) , 8. , 2004 () , 8 - , , , ( java.lang.Number) , String . 9. , 2004 () , 9 java.lang.Integer toBinaryString(), toHexString(), toOctalString() doubleValue(), shortValue(), longValue(), doubleValue(), floatVlaue() . java.lang.NumberFormatException int parseInt() Integer valueOf() compareTo() Integer(int value), Integer(String s) , Class MAX_VALUE, MIN_VALUE, TYPE 10. boxing/unboxing : Integer i = 5; // Integer i = new Integer(5); Integer array[] = new Integer[] { new Integer(1), new Integer(2), new Integer(3) }; // : Integer array [] = { 1, 2, 3}; NullPointerException class Foo { static Integer doSomething() { } } int i = Foo.doSomething(); // NullPointerException Integer theI = Foo.doSomething(); int i = (theI != null) ? theI, 0; , 2004 () , 10 11. for class Foo { public static void main(String args[] ) { for (String s : args ) { System.out.println(s); } } } // for (int i = 0; i &lt; args.length; i++) { System.out.println( args[i] ) ; } , 2004 () , 11 12. class Bar { public static void someFunction( String s, int array) { int sum = 0; for (int i : array ) { sum += i; } } } Bar.someFunction(one, 1, 2, 3, 4, 5); // // static void someFunction(String s, int[ ] array) {} , 2004 () , 12 13. , 2004 () , 13 java java.lang.StrictMath -, . java.lang.Math -, StrictMath ( ) . 1.6 java.lang.Math StrictMath java.math.BigDecimal, java.math.BigInteger 14. , 2004 () , 14 (generics) , , , , Object (, ) ( ) 15. java 1.5: public class Envelope { private Object ref; public Envelope (Object arg) { ref = arg; } public Object get() { return ref; } } Envelope e = new Envelope(new MyClass()); MyClass m = (MyClass) e.get(); //OK // ClassCastException in runtime: Another a = (Another) e.get(); , 2004 () , 15 1.5: public class Envelope { private T ref; public Envelope (T arg) { ref = arg; } public T get() { return ref; } } Envelope e = new Envelope(new MyClass()); MyClass m = e.get(); //compile time error: Another a = e.get(); 16. , 2004 () , 16 ClassDeclaration: NormalClassDeclaration EnumDeclaration NormalClassDeclaration: ClassModifiersopt class Identifier TypeParametersopt Superopt Interfacesopt ClassBody TypeParameters : &lt; TypeParameterList &gt; TypeParameterList : TypeParameterList , TypeParameter | TypeParameter TypeParameter: TypeVariable TypeBoundopt TypeBound: extends ClassOrInterfaceType AdditionalBoundListopt AdditionalBoundList: AdditionalBound AdditionalBoundList AdditionalBound AdditionalBound: &amp; InterfaceType 17. InterfaceDeclaration: NormalInterfaceDeclaration AnnotationTypeDeclaration NormalInterfaceDeclaration: InterfaceModifiersopt interface Identifier TypeParametersopt ExtendsInterfacesopt InterfaceBody MethodDeclaration: MethodHeader MethodBody MethodHeader: MethodModifiersopt TypeParametersopt ResultType MethodDeclarator Throwsopt , 2004 () , 17 18. , 2004 () , 18 C++ () / () java 19. - class MyGeneric { private T t; private AnotherGeneric&lt; T &gt; at = new AnotherGeneric(); } : class SomeGeneric { T t = new T(); //compile time error T array = new T[10]; //compile time error } class HasStatic { static T t; //compile time error } , 2004 () , 19 20. class Stats { private T [ ] nums; public Stats ( T [] array ) { nums = array; } public double average ( ) { double sum = 0.0; for (T n : nums ) { // Number doubleValue() sum += n.doubleValue(); } return sum / nums.length(); } } , 2004 () , 20 21. - wildcards Stats: class Stats { private T [ ] nums; public Stats ( T [] array ) { nums = array; } public double average ( ) { } public boolean sameAvg ( Stats another) { return average() == another.average(); } } // : Integer intArray[] = { 1, 2, 4, 7 }; Double doubleArray [] = { 6.0, 7.8, 22.0 }; Stats intStats = new Stats (intArray); intStats.sameAvg (new Stats(doubleArray) ); //Compile time error , 2004 () , 21 22. ? class Stats { private T [ ] nums; public Stats ( T [] array ) { nums = array; } public double average ( ) { } public boolean sameAvg ( Stats another) { return average() == another.average(); } } ? generic-type Stats anyStats = new Stats ( ); Stats array[] = new Stats [10]; ? instanceof Stats iStats = new Stats ( new Integer[]{ 1, 2, 3, 4} ); iStats instanceof Stats // true , 2004 () , 22 23. wildcards abstract class Shape { public abstract void draw(Canvas c); } public class Ellipse extends Shape { } public class Polygon extends Shape { } public static void drawAll (List c); public boolean addAll (Collection</p>