六月丁香五月婷婷,丁香五月婷婷网,欧美激情网站,日本护士xxxx,禁止18岁天天操夜夜操,18岁禁止1000免费,国产福利无码一区色费

學(xué)習(xí)啦>知識(shí)大全>知識(shí)百科>百科知識(shí)>

java什么是實(shí)例意思指的是

時(shí)間: 歐東艷656 分享

  1. 首先看下定義的注解類(lèi):

  類(lèi)注解定義, MyClassAnnotation.java:

  [java] view plain copy

  package com.ross.annotation;

  import java.lang.annotation.*;

  /**

  * Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com

  * Date: 2012-1-29

  * Since: MyJavaExpert v1.0

  * Description: class annotation

  */

  @Retention (RetentionPolicy.RUNTIME)

  @Target (ElementType.TYPE)

  public @interface MyClassAnnotation

  {

  String uri();

  String desc();

  }

  默認(rèn)構(gòu)造方法注解定義,MyConstructorAnnotation.java:

  [java] view plain copy

  package com.ross.annotation;

  import java.lang.annotation.ElementType;

  import java.lang.annotation.Retention;

  import java.lang.annotation.RetentionPolicy;

  import java.lang.annotation.Target;

  /**

  * Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com

  * Date: 2012-1-29

  * Since: MyJavaExpert v1.0

  * Description: Constructor annotation

  */

  @Retention (RetentionPolicy.RUNTIME)

  @Target (ElementType.CONSTRUCTOR)

  public @interface MyConstructorAnnotation

  {

  String uri();

  String desc();

  }

  方法注解定義,MyMethodAnnotation.java:

  [java] view plain copy

  package com.ross.annotation;

  import java.lang.annotation.ElementType;

  import java.lang.annotation.Retention;

  import java.lang.annotation.RetentionPolicy;

  import java.lang.annotation.Target;

  /**

  * Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com

  * Date: 2012-1-29

  * Since: MyJavaExpert v1.0

  * Description: method annotation

  */

  @Retention (RetentionPolicy.RUNTIME)

  @Target (ElementType.METHOD)

  public @interface MyMethodAnnotation

  {

  String uri();

  String desc();

  }

  字段注解定義, MyFieldAnnotation.java:

  [java] view plain copy

  package com.ross.annotation;

  import java.lang.annotation.ElementType;

  import java.lang.annotation.Retention;

  import java.lang.annotation.RetentionPolicy;

  import java.lang.annotation.Target;

  /**

  * Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com

  * Date: 2012-1-29

  * Since: MyJavaExpert v1.0

  * Description: field annotation

  */

  @Retention (RetentionPolicy.RUNTIME)

  @Target (ElementType.FIELD)

  public @interface MyFieldAnnotation

  {

  String uri();

  String desc();

  }

  2. 再看下我們注解的應(yīng)用和測(cè)試:

  在類(lèi)上面使用了MyClassAnnotation注解, 默認(rèn)構(gòu)造方法上使用了MyConstructorAnnotation注解, 自定義方法上使用了MyMethodAnnotation注解, 自定義字段上使用了MyFieldAnnotation注解, 在Mail函數(shù)中則實(shí)現(xiàn)了訪(fǎng)問(wèn)這些注解,并打印注解信息.

  MySample.java:

  [java] view plain copy

  package com.ross.annotation;

  import java.lang.reflect.*;

  /**

  * Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com

  * Date: 2012-1-29

  * Since: MyJavaExpert v1.0

  * Description: This class is used to show how to use the annotation of each level

  */

  @MyClassAnnotation (uri = "com.ross.MySample" , desc = "The class name" )

  public class MySample

  {

  @MyFieldAnnotation (uri = "com.ross.MySample#id" , desc = "The class field" )

  public String id;

  /**

  * Description: default constructor

  */

  @MyConstructorAnnotation (uri = "com.ross.MySample#MySample" , desc = "The default constuctor" )

  public MySample()

  {

  }

  /**

  * Description: normal method

  */

  @MyMethodAnnotation (uri = "com.ross.MySample#setId" , desc = "The class method" )

  public void setId(String id)

  {

  this .id = id;

  }

  /**

  * Description: MyAnnotation test

  * @throws NoSuchMethodException

  * @throws SecurityException

  * @throws NoSuchFieldException

  */

  public static void main(String[] args) throws SecurityException,

  NoSuchMethodException, NoSuchFieldException

  {

  MySample oMySample = new MySample();

  // get class annotation

  MyClassAnnotation oMyAnnotation = MySample.class

  .getAnnotation(MyClassAnnotation.class );

  System.out.println("Class's uri: " + oMyAnnotation.uri() + "; desc: "

  + oMyAnnotation.desc());

  // get constructor annotation

  Constructor oConstructor = oMySample.getClass().getConstructor();

  MyConstructorAnnotation oMyConstructorAnnotation = (MyConstructorAnnotation) oConstructor

  .getAnnotation(MyConstructorAnnotation.class );

  System.out.println("Constructor's uri: "

  + oMyConstructorAnnotation.uri() + "; desc: "

  + oMyConstructorAnnotation.desc());

  // get method annotation

  Method oMethod = oMySample.getClass().getDeclaredMethod("setId" ,String. class );

  MyMethodAnnotation oMyMethodAnnotation = oMethod

  .getAnnotation(MyMethodAnnotation.class );

  System.out.println("Method's uri: " + oMyMethodAnnotation.uri()

  + "; desc: " + oMyMethodAnnotation.desc());

  // get field annotation

  Field oField = oMySample.getClass().getDeclaredField("id" );

  MyFieldAnnotation oMyFieldAnnotation = oField

  .getAnnotation(MyFieldAnnotation.class );

  System.out.println("Field's uri: " + oMyFieldAnnotation.uri()

  + "; desc: " + oMyFieldAnnotation.desc());

  }

  }

  控制臺(tái)打印結(jié)果:

  [plain] view plain copy

  Class's uri: com.ross.MySample; desc: The class name

  Constructor's uri: com.ross.MySample#MySample; desc: The default constuctor

  Method's uri: com.ross.MySample#setId; desc: The class method

  Field's uri: com.ross.MySample#id; desc: The class field

  至此本實(shí)例就完成了, 其實(shí)就是抓住兩點(diǎn)一個(gè)是定義注解類(lèi),另外一個(gè)是如何訪(fǎng)問(wèn)注解, 就算是學(xué)會(huì)了.

java什么是實(shí)例意思指的是

1. 首先看下定義的注解類(lèi): 類(lèi)注解定義, MyClassAnnotation.java: [java] view plain copy package com.ross.annotation; import java.lang.annotation.*; /** * Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com * Date: 2012-1
推薦度:
點(diǎn)擊下載文檔文檔為doc格式
245681