1. 程式人生 > 實用技巧 >JDK1.5新特性(列舉,泛型,可變引數)

JDK1.5新特性(列舉,泛型,可變引數)

1.可變引數...

package com.javaboy.demo1;

import java.util.Arrays;

/**
 * 可變引數:注意兩點
 * 1.可變引數在引數列表的結尾
 * 2.可變引數在引數列表中只能有一個
 * 讓程式碼更加簡介,優雅,靈活
 *
 * 3.後面在寫BaseDao-->DBUtils 時候,做增刪改查時條件引數不確定,所以可以用這種方式來做
 */
public class TeachDemo1 {
    public static void main(String[] args) {
        fun("javaboy");
        fun2(
"javagirl",new int[]{}); TeachDemo1.fun("javaboy2",1); fun("javagirl2:"+1,2,3); int[] arr = new int[]{1,2,3,4}; fun("javaboy520:",arr); } public static void fun(String name,int... num){ System.out.println("name:"+name); System.out.println("arr:"
+ Arrays.toString(num)); /* for (int n:num) { System.out.println("n:"+n); }*/ } public static void fun2(String name,int[] num){ System.out.println("name:"+name); for (int n : num) { System.out.println("n:" + n); } /*if (num!=null) { for (int n : num) { System.out.println("n:" + n); } }
*/ } }

2.列舉 可以數得清(數量確定),可以舉例子的這種場景(少量)我們可以考慮使用列舉

package work2;

public enum WeekDay {
   MONDAY,//星期一
   TUESDAY,//
   WEDNESDAY,//
   THURSDAY,//
   FRIDAY,//
   SATURDAY,//
   SUNDAY;//星期日
   
}
package work2;

import java.util.Scanner;

public class TestWeekDay {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // 1.
      WeekDay day1 = WeekDay.MONDAY;
      printWeekDay(day1);

      Scanner sc = new Scanner(System.in);
      System.out.println("輸入表示日期的列舉字串:");
      String tmp1 = sc.next();
      WeekDay day2 = WeekDay.valueOf(tmp1);
      printWeekDay(day2);

      
      System.out.println("輸入表示日期的列舉數字:");
      int tmp2 = sc.nextInt();
      WeekDay day3 = WeekDay.values()[tmp2];
      printWeekDay(day3);
   }

   /*
    * @param weekDay 
    */
   public static void printWeekDay(WeekDay weekDay) {
      switch (weekDay) {
      case MONDAY:
         System.out.println("Today is Monday!");
         break;
      case TUESDAY:
         System.out.println("Today is Tuesday!");
         break;
      }
   }

}

3.泛型

    package com.javaboy.test;
    
    public class Student {
        private int id;
        private String name;
        private int gender;//0-女 1-男
    
        public Student(){
    
        }
        public Student(String name){
            this.name=name;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getGender() {
            return gender;
        }
    
        public void setGender(int gender) {
            this.gender = gender;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", gender=" + gender +
                    '}';
        }
    }

    package com.javaboy.test;
    
    import org.junit.Test;
    
    public class TestDemo {
        @Test
        public void test(){
            System.out.println("Hello boy1!");
        }
    
        @Test
        public void test0(){
            System.out.println("Hello boy2!");
        }
    
        @Test
        public void test2(){
            System.out.println("Hello boy3!");
        }
    
        @Test
        public void test3(){
            System.out.println("Hello boy4!");
        }
    }



    package com.javaboy.test;
    
    import org.junit.Test;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 泛型
     * 測試類的使用:
     * 加@Test
     * public(一般是public) void(不能有返回值) test(不能有引數){
     *
     * }
     */
    public class TestDemo2 {
    
        //java.lang.Exception: Method test should have no parameters
     /*   @Test
        public void test(String args){
            System.out.println("Hello World!");
        }*/
     //正確
        @Test
        public void test2(){
            System.out.println("Hello World!");
        }
    
        //java.lang.Exception: Method test3() should be void
     /*   @Test
        public String test3(){
            System.out.println("Hello World!");
            return "haha";
        }*/
     //泛型(Generic Types) 指標型別(Pointer Types) 一旦我們已經轉換指標為void*,我們就不能輕易將其轉換回原類。
        @Test
        public void testGenericType(){
            List list = new ArrayList();
            Student stu = new Student();
    
            list.add("a");
            list.add(1);
            list.add(true);
            list.add(stu);
            for (Object obj:list){
                System.out.println(obj);
            }
        }
    
        @Test
        public void testGenericType2(){
            //List<String> list = new ArrayList<String>();
            List list = new ArrayList();
            Student stu = new Student();
    
            list.add("a");//1.資料型別約束,讓資料更安全
            list.add("b");
            list.add("c");
            list.add("d");
            list.add("e");
            for (Object str:list) {
                System.out.println(str);
            }
            /*list.add(1);
            list.add(true);
            list.add(stu);*/
            for (Object obj:list){
                System.out.println(obj);
            }
        }
    
        @Test
        public void testGenericType3(){
            //List<String> list = new ArrayList<String>();
            List list = new ArrayList();
            Student stu1 = new Student("文海濤");
            Student stu2 = new Student("戴戴代代");
            Student stu3 = new Student("劉寧");
    
            list.add(stu1);//1.資料型別約束,讓資料更安全
            list.add(stu2);//
            list.add(stu3);
    
            for (Object stu:list) {
                System.out.println(stu);
            }
            /*list.add(1);
            list.add(true);
            list.add(stu);*/
            for (Object obj:list){
                System.out.println(((Student)obj).getName());
            }
        }
        //泛型好
        @Test
        public void testGenericType4(){
            List<Student> list = new ArrayList<Student>();
            Student stu1 = new Student("文海濤");
            Student stu2 = new Student("戴戴代代");
            Student stu3 = new Student("劉寧");
    
            list.add(stu1);//1.資料型別約束,讓資料更安全
            list.add(stu2);//
            list.add(stu3);
    
            for (Student stu:list) {
                System.out.println(stu.getName());
            }
    
        }
    
    }

世上最重要的事,不在於我們在何處,而在於我們朝著什麼方向走!!!