-
注解和反射
这两个的概念在Java基础学习中大家就学习了,但很少用到吧,他们主要是为大家学习后面的框架知识打下基础的
注解(Annotation)
首先来说说注解的概念:
我们都知道注释吧,就是给人看的,那注解其实意思差不多,也是一种解释语言,并不是一种程序本身
格式:@注释名; 可以在哪里使用:package、class、method、field相当于给他们添加一些额外的辅助信息,后面我们可以用反射机制编程实现元数据的访问
分类:
内置注解:
@Override 对父类方法的重写
@Deprecated 不推荐程序员使用,但是可以使用
@SuppressWarnings 抑制编译时的警告信息
元注解:
@Target:用于描述注解的可以使用在什么地方
@Retention:一般都用RUNTIME表示在运行时注解(RUNTIME(运行时有效)>class(编译之后有个class有效)>sources(源码级别有效))
@Document:说明该注解将包含在javadoc中
@Inherited:说明子类可以继承父类的该注解
import java.lang.annotation.*; //测试元注解 @MyAnnotationTest public class AnnotationTest { @MyAnnotationTest public void add(){ } } //定义一个注解 //@Target:用于描述注解的可以使用在什么地方 @Target(value = {ElementType.METHOD,ElementType.TYPE})//表示我定义的下面的那个MyAnnotationTest注解可以使用在方法和类上面 //@Retention:一般都用RUNTIME表示在运行时注解 @Retention(value = RetentionPolicy.RUNTIME) //@Document:说明该注解将包含在javadoc中 @Documented //@Inherited:说明子类可以继承父类的该注解 @Inherited @interface MyAnnotationTest{//定义一个注解前面要用@interface }
自定义注解:
直接代码展示了:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class ZdyAnnotation { // 注解可以显示赋值:如果没有默认值,我们就必须要给注解赋值 @MyAnnotation(name = "金涛骇浪",array = {"成都理工","电子科大"}) public void test01(){ } // 如果只有一个值为value,可以不用写,直接赋值即可 @MyAnnotation01("金涛海浪") public void test02(){ } } //自定义第一个注解 @Target(value = {ElementType.METHOD,ElementType.TYPE}) @Retention(value = RetentionPolicy.RUNTIME) @interface MyAnnotation{//定义一个注解前面要用@interface // 注解的参数:参数类型+参数名(); String name() default ""; int age() default 0; int id() default -1;//如果默认值为-1,代表不存在 String[] array(); } //自定义第二个注解 @Target(value = {ElementType.METHOD,ElementType.TYPE}) @Retention(value = RetentionPolicy.RUNTIME) @interface MyAnnotation01{ String value(); }
反射(Reflection)
简单来说就是程序运行时可以改变其结构(Java本身是静态语言,有了反射后,就变成了准动态语言,主要可以获得注解和泛型等等)
代码举例:
public class FanShe { public boolean login(String username,String password){ if ("admin".equals(username)&&"123".equals(password)){ System.out.println("登陆成功"); }else { System.out.println("账号或密码错误"); } return false; } }
反射调用:
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class FanSheTest { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException { //获取类 Class<?> aClass = Class.forName("com.sep24FanShe.FanShe"); //获取某个特定的方法 //通过方法名+形参列表 Method m = aClass.getDeclaredMethod("login", String.class, String.class); //通过反射机制执行login方法 //用一个Object对象接收它的新的实例 Object o = aClass.newInstance(); //调用o对象的m方法 传递 admin 123参数 方法的执行结果是 Object invoke = m.invoke(o, "admin", "123");//invoke:激活的意思 System.out.println(invoke); } } 反射操作注解:
import java.lang.annotation.*; import java.lang.reflect.Field; public class ReflectionAnnotationTest { // 通过反射操作注解 public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { Class c1 = Class.forName("com.sep24FanShe.Person"); Annotation[] annotations = c1.getAnnotations();//获得注解的方法 for (Annotation annotation : annotations) { System.out.println(annotation); } // 获得注解value的值 hxbClass hxbClass = (hxbClass)c1.getAnnotation(hxbClass.class);//获得指定注解 String value = hxbClass.value(); System.out.println(value); // 获得类指定的注解 Field id = c1.getDeclaredField("id"); hxbFiled annotation = id.getAnnotation(hxbFiled.class); System.out.println(annotation.columnName()); System.out.println(annotation.type()); System.out.println(annotation.length()); } } //写一个javaBean @hxbClass("db_person")//自定义的类名注解 class Person{ @hxbFiled(columnName = "db_id",type = "int",length = 10)//自定义的属性注解 private int id; @hxbFiled(columnName = "db_age",type = "int",length = 10) private int age; @hxbFiled(columnName = "db_name",type = "varchar",length = 3) private String name; public Person(int id, int age, String name) { this.id = id; this.age = age; this.name = name; } @Override public String toString() { return "Person{" + "id=" + id + ", age=" + age + ", name='" + name + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person() { } } //定义一个类名注解 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface hxbClass{ String value(); } //定义一个属性注解 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface hxbFiled{ String columnName(); String type(); int length(); }
注解和反射就先说道这里,具体后面框架的时候会用到的,那时再有什么问题再看谢谢大家;饿
来源:https://www.cnblogs.com/hxbhxb/p/15363010.html
最新更新
带有参数的装饰器
类装饰器
django中的auth模块与admin后台管理
python的日期处理
字符串常用方法
基本数据类型概述
python-map()函数基本用法
python带你实现任意下载AcFun视频数据~
bbs项目之注册功能
变量的定义和使用
三大常用数据库事务详解之三:事务运行
三大常用关系型数据库事务详解之二:基
三大关系型数据库事务详解之一:基本概
MongoDB常用命令(2)
MongoDB基本介绍与安装(1)
SQLServer触发器调用JavaWeb接口
SQL Server索引的原理深入解析
SqlServer2016模糊匹配的三种方式及效率问题
SQL中Truncate的用法
sqlserver 多表关联时在where语句中慎用tri
VB.NET中如何快速访问注册表
ASP.NET中图象处理过程详解
Vue(1)Vue安装与使用
JavaScript 语言入门
js将一段字符串的首字母转成大写
纯原生html编写的h5视频播放器
H5仿原生app短信验证码vue2.0组件附源码地
TypeScript(4)接口
TypeScript(3)基础类型
TypeScript(2)WebStorm自动编译TypeScript配置