看了一本书《Java 5.0 tiger 程序高手秘笈》下面是我的笔记:有以下10部分内容:
1. Generic 2. Enumerated 3. autoboxing 与Unboxing 4. vararg 5. Annotation 6. for/in语句 7. 静态的import 8. 格式化 9. Threading Ps: 该书源代码地址:http://www.oreilly.com/catalog/javaadn/中文版本由中南大学出版社出版; 下面分依次总结::::
一:Generic以前没有听说过,不知道是干什么的; 作用: Generic提供对Java的类型安全性的支持;也是许多Java5.0新特性的基石,支撑着vararg,annotation enumeration,collection,甚是是语言中的一些并行功能。 Type-safe 的 List: 语法: List listOfStrings= new LinkedList(); 例子1: package com.oreilly.tiger.ch02;
import java.util.ArrayList; import java.util.List;
public class BadIdea {
private static List ints = new ArrayList();
public static void fillList(List list) { for (Integer i : list) { ints.add(i); } }
public static void printList() { for (Integer i : ints) { System.out.println(i); } }
public static void main(String[] args) { List myInts = new ArrayList(); myInts.add(1); myInts.add(2); myInts.add(3);
System.out.println("Filling list and printing in normal way..."); fillList(myInts); printList();
try { List list = (List)BadIdea.class.getDeclaredField("ints").get(null); list.add("Illegal Value!"); } catch (Exception e) { e.printStackTrace(); }
System.out.println("Printing with illegal values in list..."); printList(); } } 总结:可以限定list可以接受的类型,避免频繁的类型转换。但List以及其他collection class仍然不能接受primitive value; Type-safe 的Map语法: Mapstrings=new HashMap< String,String >(); //key and value are all String Mapstrings=new HashMap< String,Object >(); //key isString,value is Object; Mapstrings=new HashMap< Lang,String >(); //key is Long,value is String Interating over parameterized Type For/in 循环提供了一个避免用java.util.Iterator 这个class的方法; 例子: package com.oreilly.tiger.ch02;
import java.io.IOException; import java.io.PrintStream; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map;
public class GenericsTester {
public GenericsTester() { }
public void testTypeSafeMaps(PrintStream out) throws IOException { Map squares = new HashMap();
for (int i=0; i<100; i++) { squares.put(i, i*i); }
for (int i=0; i<10; i++) { int n = i*3; out.println("The square of " + n + " is " + squares.get(n)); } }
public void testTypeSafeLists(PrintStream out) throws IOException { List listOfStrings = getListOfStrings(); for (Iterator i = listOfStrings.iterator(); i.hasNext(); ) { String item = (String)i.next();// 这里是关键 // Work with that string }
List onlyStrings = new LinkedList(); onlyStrings.add("Legal addition"); /** * Uncomment these two lines for an error onlyStrings.add(new StringBuilder("Illegal Addition")); onlyStrings.add(25); */ }
public void testTypeSafeIterators(PrintStream out) throws IOException { List listOfStrings = new LinkedList(); listOfStrings.add("Happy"); listOfStrings.add("Birthday"); listOfStrings.add("To"); listOfStrings.add("You");
for (Iterator i = listOfStrings.iterator(); i.hasNext(); ) { String s = i.next(); out.println(s); }
printListOfStrings(getListOfStrings(), out); }
private List getList() { List list = new LinkedList(); list.add(3); list.add("Blind"); list.add("Mice");
return list; }
private List getListOfStrings() { List list = new LinkedList(); list.add("Hello"); list.add("World"); list.add("How"); list.add("Are"); list.add("You?");
return list; }
public void testTypeSafeReturnValues(PrintStream out) throws IOException { List strings = getListOfStrings(); for (String s : strings) { out.println(s); } }
private void printListOfStrings(List list, PrintStream out) throws IOException {
for (Iterator i = list.iterator(); i.hasNext(); ) { out.println(i.next()); } }
public void printList(List> list, PrintStream out) throws IOException { for (Iterator> i = list.iterator(); i.hasNext(); ) { out.println(i.next().toString()); } }
public static void main(String[] args) { GenericsTester tester = new GenericsTester();
try { tester.testTypeSafeLists(System.out); tester.testTypeSafeMaps(System.out); tester.testTypeSafeIterators(System.out); tester.testTypeSafeReturnValues(System.out);
List ints = new LinkedList(); ints.add(1); ints.add(2); ints.add(3); tester.printList(ints, System.out);
/** Uncomment for an error NumberBox illegal = new NumberBox(); */ } catch (Exception e) { e.printStackTrace(); } } } + 接受parameterized Type 作为参数 如:private void printListOfStrings(List list, PrintStream out)
返回parameterized Type 如:private List getListOfStrings()
使用parameterized Type作为类型参数 Example: Map> map=new HashMap>(); Example: Map>> map =getWeirdMap();
Check lint: 如果使用了可以被参数化的类型,你却没有进行参数化,就会被javac 给出一个警告: 如果加上-Xlint参数开启警告,就会显示警告的详细信息,
Generic and 类型转换 关键在于:如同一般的非Generic一样,他们是有层次的,generic不一样的地方在于层次来自于本身的基本类型,而不是该类型的参数。 对于: LinkedList floatList=new LinkedList();转换是基于LinkedList 而非Float;所以下面这个是合法的: List moreFloats= floatList; 而这个是不合法的: LinkedList numberList=floatList; 参看例子一: 使用类型通配符:(当要使用旧式的list,map或其他项目没有被参数化的时候,为了避免引发unchecked错误,要采用Generic通配符) public void printList(List> list, PrintStream out) throws IOException { for (Iterator> i = list.iterator(); i.hasNext(); ) { out.println(i.next().toString()); } } 你想要表达的是printList()可以接受任意的List,这是Generic中以?来表示通配符的目的 这是没有unchecked警告。
撰写Generic类型: //例子: package com.oreilly.tiger.ch02;
import java.util.ArrayList; import java.util.List;
public class Box {
protected List contents;
public Box() { contents = new ArrayList(); }
public int getSize() { return contents.size(); }
public boolean isEmpty() { return (contents.size() == 0); }
public void add(T o) { contents.add(o); } public T grab() { if (!isEmpty()) { return contents.remove(0); } else return null; } } //使用这个类型:Box box=new Box(); // Box> box=new Box< List>();
package com.oreilly.tiger.ch02;
import java.util.Iterator;
public class NumberBox extends Box {
public NumberBox() { super(); } // Sum everything in the box public double sum() { double total = 0; for (Iterator i = contents.iterator(); i.hasNext(); ) { total = total + i.next().doubleValue(); } return total; }
public static double sum(Box box1, Box box2) { double total = 0; for (Iterator i = box1.contents.iterator(); i.hasNext(); ) { total = total + i.next().doubleValue(); } for (Iterator i = box2.contents.iterator(); i.hasNext(); ) { total = total + i.next().doubleValue(); } return total; } }
限制类型参数; public class NumberBox extends Box 下面的是不合法的: NumberBox illegal=new NumberBox();
|