当前位置:网站首页>注解与反射
注解与反射
2022-07-27 09:25:00 【看小虫子】
性能分析
普通方式>反射关闭检测>反射
package TestReflected;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//分析性能问题
public class Test08 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
test01();
test02();
test03();
}
//普通方式调用
public static void test01() {
User user = new User();
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
user.getName();
}
long endTime = System.currentTimeMillis();
System.out.println("普通方法执行1000000次需要的时间:" + (endTime - startTime) + "ms");
}
//反射方式调用
public static void test02() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
User01 user01 = new User01();
Class C1 = Class.forName("User01");
Method getName = C1.getDeclaredMethod("getName", null);
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
getName.invoke(user01, null);
}
long endTime = System.currentTimeMillis();
System.out.println("反射方法执行1000000次需要的时间:" + (endTime - startTime) + "ms");
}
//反射方式调用,关闭检测
public static void test03() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
User01 user01 = new User01();
Class C1 = Class.forName("User01");
Method getName = C1.getDeclaredMethod("getName", null);
getName.setAccessible(true);
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
getName.invoke(user01, null);
}
long endTime = System.currentTimeMillis();
System.out.println("反射方法,关闭检测执行1000000次需要的时间:" + (endTime - startTime) + "ms");
}
}
通过反射操作对象
package TestReflected;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//动态创建对象,通过反射
public class Test07 {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//获得Class对象
Class c1 =User.class;
//构造一个对象
// User user = (User) c1.getDeclaredConstructor().newInstance();
// System.out.println(user);
//通过构造器创建对象
Constructor constructor = c1.getDeclaredConstructor(int.class, String.class);
User user2 = (User) constructor.newInstance(18, "yan");
System.out.println(user2);
//通过反射调用普通方法
User user3=(User)c1.newInstance();
//通过反射获取一个方法
Method setName =c1.getDeclaredMethod("setName", String.class);
//invoke:激活的意思
//(对象,""方法的值)
setName.invoke(user3,"yan");
System.out.println(user3.getName());
//通过反射操作属性
User user4=(User)c1.newInstance();
Field name = c1.getDeclaredField("name");
//不能直接操作私有属性,我们需要关闭程序的安全检测,属性/方法的setAccessible(true)
name.setAccessible(true); //
name.set(user4,"yan2");
System.out.println(user4.getName());
}
}
用反射操作注解
package TestReflected;
//练习反射操作注解
import java.lang.annotation.*;
import java.lang.reflect.Field;
public class Test09 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class C1 = Class.forName("TestReflected.student2");
//通过反射获得注解
//System.out.println(C1.getDeclaredAnnotations());
Annotation[] annotations = C1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//获得注解value的值
TableYan tableYan = (TableYan)C1.getAnnotation(TableYan.class);
String value = tableYan.value();
System.out.println(value);
//获得类指定的注解
Field f = C1.getDeclaredField("name");
FieldYan annotation = f.getAnnotation(FieldYan.class);
System.out.println(annotation.columnName());
System.out.println(annotation.length());
System.out.println(annotation.type());
}
}
@TableYan("db_Student")
class student2{
@FieldYan(columnName = "db.id",type = "int",length = 10)
private int id;
@FieldYan(columnName = "db.age",type = "int",length = 10)
private int age;
@FieldYan(columnName = "db.name",type = "String",length = 10)
private String name;
public student2() {
}
public student2(int id, int age, String name) {
this.id = id;
this.age = age;
this.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;
}
@Override
public String toString() {
return "student2{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableYan{
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldYan{
String columnName();
String type();
int length();
}
边栏推荐
- 七月集训(第06天) —— 滑动窗口
- 【CTF】ciscn_ 2019_ es_ two
- July training (day 10) - bit operation
- BGP联邦实验
- Quick apply custom progress bar
- < script> detailed explanation of label content
- 好久不送书,浑身不舒服
- July training (day 15) - depth first search
- 2022 software test interview questions 200 big factory interview true questions brushed to get 10K positions
- 七月集训(第20天) —— 二叉搜索树
猜你喜欢

九种方式,教你读取 resources 目录下的文件路径

ArcGIS pro2.8 deep learning environment configuration based on rtx30 graphics card

抢了个票,还以为发现了12306的系统BUG

Esp8266 Arduino programming example ADC

NCCL (NVIDIA Collective Communications Library)

Eureka delayed registration of a pit

Read the paper snunet CD: a densely connected Siamese network for change detection of VHR images

XML概述

ES6 new - deconstruction assignment of array / object

Nccl collective communication --collective operations
随机推荐
七月集訓(第07天) —— 哈希表
July training (day 12) - linked list
快应用自定义进度条
1640. Can you connect to form an array -c language implementation
[C language - zero foundation lesson 10] adventure of array Kingdom
【无标题】
NCCL (NVIDIA Collective Communications Library)
七月集训(第16天) —— 队列
[C language - zero basis _ study _ review _ lesson 5] operational properties of basic operators
好久不送书,浑身不舒服
C language exercises
七月集训(第03天) —— 排序
1344. Included angle of clock pointer
深度剖析分库分表最强辅助Sharding Sphere
抢了个票,还以为发现了12306的系统BUG
七月集训(第10天) —— 位运算
[wechat applet] lunar calendar and Gregorian calendar are mutually converted
七月集训(第21天) —— 堆(优先队列)
[C language - zero foundation lesson 7] sequential structure and selection structure
监控神器:Prometheus 轻松入门,真香!