您当前的位置: 首页 > 

梁云亮

暂无认证

  • 3浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

第十四章 注解与反射

梁云亮 发布时间:2022-03-08 17:54:57 ,浏览量:3

14.1 注解
User.java

enum User{
    LISI,JACK,MARY,ANDY,WANGWU
}

javac User.java 
User.class

class java
interface java
enum java
annotation java
module-info.java

class User{
    public void save(T t){
	
    }
}

User u = new User();
u.save(new Book());

User u = new User();
u.save(new Teacher());


List

泛型

枚举

注解  
comment 
annotation 
@Data

public enum RetentionPolicy {
    SOURCE,CLASS,RUNTIME
}

package cn.org;
import static java.lang.annotation.ElementType.*;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.*;
import java.lang.annotation.Target;
/**
 * 

Project: part13 - Webrx *

Powered By webrx *

Created By IntelliJ IDEA On 2021-02-03 10:12:22 *

Description : * * @author webrx [webrx@126.com] * @version 1.0 * @since 15 */ @Documented @Target({TYPE,FIELD,METHOD}) @Retention(RUNTIME) //RUNTIME > CLASS > SOURCE public @interface Webrx { public String font() default ""; public int age() default 25; public String[] values() default {"java","html","mysql"}; public int pos() default 5; } /* * Copyright (c) 2006, 2021, webrx.cn All rights reserved. */ package cn.org; /** *

Project: part13 - MyUtil *

Powered By webrx *

Created By IntelliJ IDEA On 2021-02-03 11:01:27 *

Description : * * @author webrx [webrx@126.com] * @version 1.0 * @since 15 */ @Webrx(age=35,pos=9) public class MyUtil { public void showAge(){ //反射 int a = this.getClass().getAnnotation(Webrx.class).age(); System.out.println(a); } public static void main(String[] args) { MyUtil mu = new MyUtil(); mu.showAge(); } }

14.2 反射

Java的反射(reflection)机制是指在程序的运行状态中,可以构造任意一个类的对象,可以了解任意一个对象所属的类,可以了解任意一个类的成员变量和方法,可以调用任意一个对象的属性和方法。这种动态获取程序信息以及动态调用对象的功能称为Java语言的反射机制。反射被视为动态语言的关键。

image-20210203115906905

image-20210203115629441

反射
Reflection
	User user = new User("b");
  2010 java java反射机制,打印出java.lang.String 的所有方法
        Class s = String.class;
        Class a = new String().getClass();
        Class c = Class.forName("java.lang.String");
        //Class
        //Method
        //Field
        System.out.println(a==c);
        //Arrays.stream(s.getDeclaredMethods()).forEach(e->System.out.println(e.getName()));
        Arrays.stream(s.getMethods()).forEach(e->System.out.println(e.getName()));

image-20210203115850531

image-20210203115935143

/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.reflect;

import cn.org.Webrx;

/**
 * 

Project: part13 - Book *

Powered By webrx *

Created By IntelliJ IDEA On 2021-02-03 15:20:52 *

Description : * * @author webrx [webrx@126.com] * @version 1.0 * @since 15 */ @Webrx public class Book { private int id; private String name; private double price; int i = 50; protected String user = "lisi"; public String addr = "郑州市"; public void info(){ System.out.println("hello world info()..."); } public Book() { } public Book(int id, String name, double price) { this.id = id; this.name = name; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Book{" + "id=" + id + ", name='" + name + '\'' + ", price=" + price + '}'; } } /* * Copyright (c) 2006, 2021, webrx.cn All rights reserved. * */ package cn.webrx.reflect; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; /** *

Project: part13 - ReflectTest *

Powered By webrx *

Created By IntelliJ IDEA On 2021-02-03 15:20:46 *

Description : * * @author webrx [webrx@126.com] * @version 1.0 * @since 15 */ public class ReflectTest { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException { System.out.println(Class.forName("java.sql.Connection").isInterface()); System.out.println(Integer.class.isPrimitive()); //正常方式 //var book = new Book(); //反射方式 Class Class var c1 = Book.class; String c = "cn.webrx.reflect.Book"; var c2 = Class.forName(c); //System.out.println(c2.getName());//cn.webrx.reflect.Book //System.out.println(c2.getSimpleName());//Book //Method info = c2.getMethod("info"); //System.out.println(info.invoke(c2.getConstructor().newInstance())); Arrays.stream(c2.getDeclaredFields()).forEach(e->{ System.out.println(e.getName()); }); Field fi = c2.getDeclaredField("i"); System.out.println(fi.getType()); System.out.println(fi.get(c2.getConstructor().newInstance())); //0 1 2 3 4 System.out.println(c2.getDeclaredField("id").getModifiers()); System.out.println(c2.getDeclaredField("i").getModifiers()); System.out.println(c2.getDeclaredField("user").getModifiers()); System.out.println(c2.getDeclaredField("addr").getModifiers()); //var c3 = book.getClass(); //System.out.println(c1.hashCode()); //System.out.println(c2.hashCode()); //System.out.println(c3.hashCode()); //通过反射来实现对象 //Book bk = (Book)c2.getDeclaredConstructors()[0].newInstance(3,"java",40d); //bk.info(); //System.out.println(bk); var c4 = String.class; var c5 = int[][].class; var c6 = Class.class; var c7 = void.class; var c8 = Object.class; var c9 = String[][][].class; } }

关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0415s