黑馬程序員——Java內省和註釋

---------------------- android培訓java培訓、期待與您交流! ----------------------

關鍵字IntroSpector-->JavaBean--->特殊的java類 名字符合某種規則

 

int getAge() 

void setAge(int age)

屬性根據方法來

class Person{

int x,

public int getAge(){

  return x;

}

public setAge(int age){

this.x = age;

}

}

 

Age--->如果第二個字母是小寫,則把第一個字母小寫--》age

gettime  --> time

setTime---> time

getCPU -----> CPU

 

 ReflectPoint1 pt1 = new ReflectPoint1(3, 5);
  String propertyName = "x";
  //x---->X--->getX()---->MethodGetX();
  //屬性描述   pt1.getClass()當做JavaBean , propertyName 屬性 。相當於一個類,一個屬性
  //就容易得到屬性的get set 方法。

 private static Object getProperty(Object pt1, String propertyName)
   throws IntrospectionException, IllegalAccessException,
  InvocationTargetException {
  /* PropertyDescriptor pt = new PropertyDescriptor(propertyName, pt1.getClass());
  //獲得方法
  Method methodGetX = pt.getReadMethod();
  //傳遞對象pt1
  Object retVal = methodGetX.invoke(pt1);*/
  
    BeanInfo  beanInfo = Introspector.getBeanInfo(pt1.getClass());
    PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
    Object retVal = null;
    for(PropertyDescriptor pd:pds){
     if(pd.getName().equals(propertyName)){
    Method methodGetX = pd.getReadMethod();
    //傳遞對象pt1
    retVal = methodGetX.invoke(pt1);
    break;
     }    
    }

 

 private static void setProperties(Object pt1, String propertyName,
   Object value) throws IntrospectionException,
   IllegalAccessException, InvocationTargetException {
  PropertyDescriptor pt2 = new PropertyDescriptor(propertyName, pt1.getClass());

  //獲得方法
  Method methodSetX = pt2.getWriteMethod();
  //傳遞對象pt1
         methodSetX.invoke(pt1,value);
 }

兩個重要工具包的導入

BeanUtils   logging

新建lib 把jar包考入。

add buildPath

getproperty(pt1,"x")  參數爲字符串類型

BeanUtils.getProperty(pt1, propertyName)

setproperty(pt1, "x",  9 )

BeanUtils.setProperty(pt1, "x", "11");

 

 

 

PropertyUtils.setProperty(pt1, "x", 100);//  此處不是字符串

 

記住兩個方法:

set()

get()

 

註解:

關鍵字Annotation

一個註解是一個類,加上一個註解,就代表創建了一個實例對象,註解的創建方式基本等同於@j接口

 

(1) @ suppresswarnings(" depracation" );  壓縮警告,不在提醒過時的方法,命令行中效果明顯。

(2) @Despracated方法過時

(3)@Override覆蓋。

Overload是重載,Override是覆蓋的,也就是重寫。

注意:
     重載Overload表示同一個類中可以有多個名稱相同的方法,但這些方法的參數列表各不相同(即參數個數或類型不同)。這個好理解。。。
     重寫Override表示子類中的方法可以與父類中的某個方法的名稱和參數完全相同,通過子類創建的實例對象調用這個方法時,將調用子類中的定義方法,這相當於把父類中定義的那個完全相同的方法給覆蓋了,這也是面向對象編程的多態性的一種表現。

 

註解就相當於一個標記,加了註解,就等於爲程序打了標記,沒加則等於沒加標記,以後,Java編譯器開發工具和其他程序可以用反射來了解類及各種元素上有無任何標記,看有什麼樣的標記,就去幹相應的事。

標記可以加載包,類,字段,方法,方法的參數以及局部變量上。

 

應用註解:

註解類:                         應用註解類:                                對“應用了註解類的類”進行反射操作的類

@interface A{                    @A                                          Class C{

}                                class B{                                    B.class.isAnnotionPerson(A.class)

                                   }                                         A a = B.class.getAnnotion(A.class);

 

@Retention                     Enum RetentionPolicy

class                          @override                  source

source                         @suppresswarnings          source

runtime                        @depracated                runtime

package cn.itcast.day2;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//註解的註解 元註解
//註解保留的運行時期,
//註解的生命週期有三個階段:SOURCE,CLASS(默認),RUNTIME

import cn.itcast.day1.EnumTest;
@Retention(RetentionPolicy.RUNTIME)
//註解的作用範圍
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface ItcastAnnotation {
 String color() default "red";//設置默認值
 String value();
 int[] arrs();
 EnumTest.TrafficLamp lamp()  default EnumTest.TrafficLamp.RED;
    MetaAnnotation annotationArr() default @MetaAnnotation("hhs") ;
}

 

 

package cn.itcast.day2;
import javax.jws.soap.InitParam;
@SuppressWarnings("deprecation")
@ItcastAnnotation(
annotationArr=@MetaAnnotation("md"),
  color="yellow",value="efg",arrs={1,2,3})
public class AnnotationTest {
 //一個註解是一個類,此處是創建了一個實例。
 @SuppressWarnings("deprecation")
 
//@ItcastAnnotation("xyz")
 public static void main(String[] args) throws Exception {
  System.runFinalizersOnExit(true);

判斷是否含有註解
  if(AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)){

得到註解
   ItcastAnnotation annotation = (ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class);

獲取註解的屬性
      System.out.println(annotation.color());
      System.out.println(annotation.arrs().length);
      System.out.println( annotation.value());
      System.out.println( annotation.lamp().nextLamp());
      System.out.println( annotation.annotationArr().value());
  }
 }

 

 

 


 

 

---------------------- android培訓java培訓、期待與您交流! ----------------------詳細請查看:http://edu.csdn.net/heima
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章