天天看點

黑馬程式員_java1.5新特性

---------------------- android教育訓練、 java教育訓練、期待與您交流! ----------------------

什麼是注解?

注解就是在java程式系統中,注解就是向編譯器描述java程式之間的相容的。下邊就通過例子來看看實實在在的注解:

例子:package com.heima;

public class Annotation {

public static void main(String[] args) {

     @SuppressWarnings("deprecation");

     System.runFinalizersOnExit(true);

     Annotation.print();

}

@Deprecated

  public static void print(){

  System.out.print("Hellowrld");

  }

}

還有一種java本身的注解就是Override是讓JVM檢驗方法是從父類中的覆寫還是重載。

 進一步了解注解:

  注解的使用流程是這樣的:定義注解——讓一個類使用注解——讓一個反射類反射這個注解的類。下邊是個例子:

定義注解:package com.heima;

import java.lang.annotation.ElementType;

import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)//向編譯器打招呼說明這個注解是在運作階段。

@Target(ElementType.METHOD);//也可以這樣({ElementType.METHOD,ElementType.TYPE})

public @interface AnnotationTest {

   String decration()default "east";//設定的預設值。

   int value();//如果注解中隻有這一個屬性,可以這樣寫AnnotationTest(2)

   int []Arrays() default {3,3,3};//【】不能放在數組名的後邊;調用

   DecrationEnum enm()default DecrationEnum.next("東");//枚舉類型的屬性

   AnnotTest test()default @AnnotTest(2);//注解類型的屬性

}

使用注解的類以及反射:

package com.heima;

@AnnotationTest(3)

public class Annotation {

    public static void main(String[] args) {

     if( Annotation.class.isAnnotationPresent(AnnotationTest.class)){

     AnnotationTest test=Annotation.class .getAnnotation(AnnotationTest.class);

     System.out.println(test);

     }

       System.runFinalizersOnExit(true);

       Annotation.print();

    }

    @AnnotationTest(value=1,Arrays={2,3,4,4})//如果你的值隻有一個可以這麼寫Arrays=3;

  public static void print(){

      System.out.print("Hellowrld");

  }

}

java1.5中的泛型應用:

首先通過一個例子來認識泛型:

  package com.heima

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

public class Generics {

    public static void main(String[] args) {

    Map<String,String> map=new HashMap<String,String>();

        map.put("name","姓名");

        map.put("age","年齡");

        map.put("dress","位址");

       Set<Map.Entry<String,String>>set= map.entrySet();

          for(Map.Entry<String, String>entry:set){

         System.out.println(entry.getKey()+" : "+entry.getValue());

          }

        Set<String> set1= map.keySet();

          for(String str:set1){

        System.out.println(str+"---->"+  map.get(str));

          }

    }

}

泛型的使用可以為我們提高代碼的安全,泛型作用在編譯階段的,在運作時沒沒有限定了。是以,我們用反射來操作結合類的話泛型是不起作用的。

注意:java中還擴充符?和extends限定符

下邊就進入了怎麼去定義自己的泛型方法:

還是一段代碼:

package com.heima;

public class GenericsTest {

    public static void main(String[] args) {

       String []arrays=new String[]{"agb","234","abc"};

       Integer igr[]=new Integer[]{2,3,4};

       GenericsTest.ergod(igr);

    GenericsTest.ergod(arrays);

    }

public static <T>void ergod(T arrays[]){

    for(T t:arrays){

       System.out.println(t);

    }

}

}

注意:給ergod方法傳進來的數組不能是基本資料類型的數組,比如:int float等。

java中的代理;

代理就是比如我們定義好了一個類,但是我們想給這個類中的方法增加其他的功能,而我們有不想修改類的本身,這是我們就可以這個類動态的則加一個代理類就行了,下邊通過代碼來看看:

例子:

package com.heima;

import java.awt.List;

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

import java.lang.reflect.Proxy;

import java.util.ArrayList;

public class CopyPictuer {

    public static void main(String[] args) {

     ArrayList list=new ArrayList();

       list.add(2);

       list.add(3);

       list.add(4);

       getproxy(list,new Action());

       list.size();

}

    public static void getproxy(final Object goj,final Action acioin){

    Proxy.newProxyInstance(goj.getClass().getClassLoader(),

                         goj.getClass().getInterfaces(),

                         new InvocationHandler() {

                            public Object invoke(Object arg0, Method arg1, Object[] arg2)

                                   throws Throwable {

                                acioin.beforruntime(arg1);

                                Object relvalue=arg1.invoke(goj, arg2);

                                acioin.endruntime(arg1);

                                // TODO Auto-generated method stub

                                return relvalue;

                            }

                         }

                         );

    }

}

interface Actiontest{

    void beforruntime(Method method);

    void endruntime(Method method);

}

class Action implements Actiontest{

  long starttime;

    public void beforruntime(Method method) {

       // TODO Auto-generated method stub

       starttime=System.currentTimeMillis();

    }

    public void endruntime(Method method) {

       // TODO Auto-generated method stub

       long endtime=System.currentTimeMillis();

       System.out.println(method.getName()+"the program si runing time:" + (endtime-starttime));

    }

}

---------------------- android教育訓練、 java教育訓練、期待與您交流! ----------------------