Tiger语言新特性

Posted on 2006-04-04 09:02  火鸟  阅读(427)  评论(0)    收藏  举报

 
1. 自动装箱与拆箱  

 1   Integer i = 10;
 2   int j = i;
 3 
 4   Map<String,Date> map=new HashMap<String,Date>();
 5   map.put("key",new Date());
 6   Date d=map.get("key");
 7 
 8   List<String> list=new ArrayList<String>();
 9   list.add("one");
10   String value=list.get(0);


 
2. 新的for循环 
例2.1

 1  String[] names = {"BadBoy","GoodBoy","HappyGirl","sadGirl"};
 2    
 3    for(String option: names) 
 4   {
 5        System.out.println(option);
 6   }
 7  
 8   List<String> list=new ArrayList<String>();
 9   list.add("one");
10   String value=list.get(0);
11   for(String item : list)
12   {
13        System.out.println(item);
14   }
15 
16 Map<String,Date> map=new HashMap<String,Date>();
17   map.put("one",new Date());
18   map.put("two",new Date());
19   for(String key : map.keySet())
20   {
21    Date d=map.get(key);
22    System.out.println(d.getTime());
23   }
24 




 例2.2 加泛型 
  

 1 import java.util.*;
 2   
 3   ArrayList<String> animals = new   ArrayList<String>();
 4   animals.add("Dog");
 5   animals.add("Cat");
 6   animals.add("Chick");
 7   animals.add("Cow");
 8   for(String option : animals) 
 9   {
10     System.out.println(option);
11   }
12 

 
3.参数可变的方法和printf
 例3.1
  定义:

 1 public int sum(int n) {  //传过来n为一个int型数组
 2    int tempSum;
 3    for(int option : n) {
 4     tempSum+=option;
 5    }
 6    /*
 7    for(int i = 0; i < n.length; i++) {
 8     tempSum+=n[i];
 9    }
10    */
11    return tempSum;
12   }
13 

  
  调用1: sum(1);
  调用2: sum(1,2);
  调用3: sum(1,2,3,4);
 例3.2 printf方法,  对应c语言的printf

1   int x = 10;
2   int y = 20;
3   int sum = x + y;
4   System.out.printf("%d + %d = %d",x,y,sum);



 4. 枚举
 例4.1
Color.java

 1   public enum Color
 2 {
 3  Red
 4  {
 5   public String toString()
 6   {
 7    return "Color.Red";
 8   }
 9  },
10  Green
11  {
12   public String toString()
13   {
14    return "Color.Green";
15   }
16  },
17  Blue
18  {
19   public String toString()
20   {
21    return "Color.Blue";
22   }
23  };
24  public static int sizeOf()
25  {
26   return Color.values().length;
27  }
28 
29  public static Color getRandom()
30  {
31   long random = System.currentTimeMillis() % sizeOf();
32   switch ((int) random)
33   {
34   case 0:
35    return Color.Red;
36   case 1:
37    return Color.Green;
38   case 2:
39    return Color.Blue;
40   default:
41    return Color.Red;
42   }
43  }
44 }
45 
46 

使用:

 1   
 2   Color color = Color.Red;
 3   for (Color item : Color.values())
 4   {
 5    System.out.println(item);
 6   }
 7   switch (color)
 8   {
 9   case Red:
10    System.out.println("Red");
11    break;
12   case Blue:
13    System.out.println("blue");
14    break;
15   case Green:
16    System.out.println("green");
17    break;
18   }
19 
20 


/**不能在switch语句里这样写case MyColors.red:
 *这样编译器不会让你通过*/

5.静态引用
 例5.1
 StaticRef.java

1   public class StaticRef
2 {
3  public static final String NAME="Hello World!";
4  public static String getName(String name)
5  {
6   return name;
7  }
8 }



使用

1 import static test.StaticRef.getName;
2 import static test.StaticRef.NAME;
3 
4 System.out.println("static is " + NAME);
5 System.out.println("name is " + getName("Hello!"));


   
 
 

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3