OpenGL高階版本學習日誌3:網格模型的載入與顯示
阿新 • • 發佈:2020-12-09
技術標籤:javase
註解
一、三種java自帶註解
@Override
@Deprecated ------>方法上面加上這個註解說明該方法已棄用。
@SuppressWarnings ------>加上這個註解,idea的警告就看不到了。
二、元註解
@Target 該註解標明瞭自定義註解的使用範圍
@Target(ElementType.TYPE)
//可以選擇的列舉型別
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}
@Target({ElementType.TYPE,ElementType.FIELD})
@Target內部是一個集合的形式,可以放多個引數。上面程式碼的意思是,該註解既可用於類也可用於屬性。
@Retention 該註解標明瞭自定義註解的生命週期,一般為RUNTIME
@Retention(RetentionPolicy.RUNTIME)
@Inherited 繼承
@Document 是否將註解加入javadoc
前兩個是必須有的註解。
三、自定義註解
@Target({ElementType.TYPE,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
int id();
String name();
}
使用註解時,要麼必須要把定義的屬性都寫上,要麼在定義的屬性的時候後面加上default 數值,否則會報錯。
@MyAnnotation(id=1,name="abc")
public class AnnotationDemo {
private int a_id;
private String a_name;
}
設定屬性預設值
@Target({ElementType.TYPE,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
int id() default 1;
String name() default "abc";
}
此時使用@MyAnnotation註解不帶引數也是可以的。
四、反射解析註解
1、類註解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyRepositoryAnnotation {
String value();
}
2、屬性註解
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyColumnAnnotation {
String column();
String type();
}
3、在實體類上應用註解
@MyRepositoryAnnotation("MyAccount")
public class Account {
@MyColumnAnnotation(column = "account_Id",type = "int")
private int accountId;
@MyColumnAnnotation(column = "account_Name",type = "String")
private String accountName;
@MyColumnAnnotation(column = "account_Owner",type = "String")
private String accountOwner;
@MyColumnAnnotation(column = "account_Value",type = "int")
private int accountValue;
public Account(int accountId, String accountName, String accountOwner, int accountValue) {
this.accountId = accountId;
this.accountName = accountName;
this.accountOwner = accountOwner;
this.accountValue = accountValue;
}
public Account() {
}
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getAccountOwner() {
return accountOwner;
}
public void setAccountOwner(String accountOwner) {
this.accountOwner = accountOwner;
}
public int getAccountValue() {
return accountValue;
}
public void setAccountValue(int accountValue) {
this.accountValue = accountValue;
}
@Override
public String toString() {
return "Account{" +
"accountId=" + accountId +
", accountName='" + accountName + '\'' +
", accountOwner='" + accountOwner + '\'' +
", accountValue='" + accountValue + '\'' +
'}';
}
}
4、反射解析註解
public class MyAnnotationReader {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class<?> account = Class.forName("com.ouc.a302.bean.Account");
MyRepositoryAnnotation annotation = account.getAnnotation(MyRepositoryAnnotation.class);
String value = annotation.value();
System.out.println("value = " + value);
Field accountId = account.getDeclaredField("accountId");
MyColumnAnnotation name = accountId.getAnnotation(MyColumnAnnotation.class);
String column = name.column();
System.out.println("column = " + column);
}
}
ount.getDeclaredField("accountId");
MyColumnAnnotation name = accountId.getAnnotation(MyColumnAnnotation.class);
String column = name.column();
System.out.println("column = " + column);
}
}