java图形界面和安卓_Android/Java/移动端(图形图像)/跨平台(RN、Flutter、Vue等)-刷题系列-2...

目录:

直接用Eclipse来做简单测试复习,有问题可以看之前的注解系列...MonkeyLei:Android-自定义注解-控件注解

变量注解:

package com.test.hl;

import java.lang.annotation.Documented;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

* 自定义注解

* @author hl

* 1. @Target 指明是属性定义,你只能用来注解变量,不能用来注解方法。

* 2. @Retention 指明生效时候,RetentionPolicy.RUNTIME表示运行时生效

*/

@Target(ElementType.FIELD)

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface AnotationSelf {

public String Name();

}

方法注解:

package com.test.hl;

import java.lang.annotation.Documented;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

* 自定义注解

* @author hl

* 1. @Target 指明是是方法注解,只能用来注解方法

* 2. @Retention 指明生效时候,RetentionPolicy.RUNTIME表示运行时生效

*/

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface AnotationSelfMethod {

public String Name();

}

实际测试:

package com.test.hl;

import java.lang.reflect.Field;

public class AnotationTest {

@AnotationSelf(Name = "test.db")

public String Mingziya;

// 这是注解方法,具体参数传递同样类似属性,用反射来搞

@AnotationSelfMethod(Name = "test.db")

public String MehthodTest(String test) {

return test;

}

public String qqlaji;

public static void main(String[] args) {

// TODO Auto-generated method stub

try {

//Class clss = Class.forName("com.test.hl");

Class> clss = AnotationTest.class.getClassLoader().loadClass("com.test.hl.AnotationTest");

Field[] fields = clss.getFields();

Object obj = clss.newInstance();

for (Field fd : fields) {

if (fd.isAnnotationPresent(AnotationSelf.class)){

System.out.println("fd: " + fd.getName());

// 根据注解的数据库名称信息,然后查询数据库,然后获取数据库值赋值给name变量, 进而实现注解赋值

AnotationSelf bs = fd.getAnnotation(AnotationSelf.class);

String dbName = bs.Name();

System.out.println("数据库名称:" + dbName);

// 查询数据库,获取值,比如获取root名称root,然后设置给注解的变量Mingziya

fd.set(obj, "root");

} else {

System.out.println("非注解 fd: " + fd.getName());

}

}

AnotationTest at = (AnotationTest) obj;

// 此时此刻,你可以打印这个变量了

System.out.println("Mingziya= " + at.Mingziya);

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InstantiationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

至此,你可以放手试试。然后仿Butterknife效果来搞搞呀。。。嘻嘻。。肺炎无情,人有情。。加油。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值