反射和动态实例化

反射

第一种方式、Class类

package com.gaoji.reflex;

import org.apache.tomcat.jni.User;

/** * 反射机制 * 获取正在运行的类的方法 * 我的理解:反射机制就是以类为对象, * 将这个类的方法行为作为对象的参数去使用它, * 可以理解为对封装好的类进行再封装或调用的一种思想。 * * 反射是为了动态代理做准备的 * @author Administrator * */
public class reflexdDome_01 {
   
   public static void main(String[] args) {
   
       Class c1 = null;
       Class<?> c2 = null;
       Class<?> c3 = null;
       Class<?> c4 = null;
       
       //如何获取正在运行中的类的对象
       //第一个方法:使用对象的getClass方法
       //不常用
       User u=new User();
       c1=u.getClass();
       
       
       //第二种:使用Class的静态方法forName,将类加载到内存中,并且获取class对象
       //常用,jdbc链接数据时,注册驱动使用的就是该方法
       try {
   
               c2=Class.forName("com.gaoji.reflex.User");//需要该类的全路径
               System.out.println(c2);
       } catch (ClassNotFoundException e) {
   
               // TODO Auto-generated catch block
               e.printStackTrace();
       }
       
       //第三种使用类.class语法
       
       c3=User.class;
       System.out.println(c3);
       
       //第四种:基本数据类型的封装类型。type语法
       c4=Integer.TYPE;
       System.out.println(c4);
       
       
   }

}

第二种方式

  • p.java 父类
package com.gaoji.reflex;

public class p {
   
    public int dome;

    public int getDome() {
   
        return dome;
    }

    public void setDome(int dome) {
   
        this.dome = dome;
    }
}
  • User.java 子类
package com.gaoji.reflex;

import java.io.Serializable;

public class User extends p{
   
    private int id;
    
    private String name;

    public char sex;

    private transient String phone;

    public User(int id, String name, char sex, String phone) {
   
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.phone = phone;
    }

    User(int id, String name) {
   
        super();
        this.id = id;
        this.name = name;
    }

    public User(int id) {
   
        super();
        this.id = id;
    }

    public User() {
   
        super();
    }

    public int getId() {
   
        return id;
    }

    public void setId(int id) {
   

        this.id = id;
    }

    public String getName() {
   
        return name;
    }

    public void introduce() {
   
        System.out.println("this is uesr");
    }

    public void setName(String name) {
   
        this.name = name;
    }

    public char getSex() {
   
        return sex;
    }

    public void setSex(char sex) {
   
        this.sex = sex;
    }

    public String getPhone() {
   
        return phone;
    }

    public void setPhone(String phone) {
   
        this.phone = phone;
    }

    @Override
    public String toString() {
   
        return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", phone=" + phone + "]";
    }

}
  • 运行类
package com.gaoji.reflex;

//获取正在运行中的类的属性
import java.lang.reflect.Field;

public class reflexdDoem_03 {
   
public static void main(String[] args) throws NoSuchFieldException, SecurityException {
   
    Class<?> c = null;
    User u = new User();
    c=u.getClass();
    //获取指定的属性
    /*Field f = c.getDeclaredField("name"); System.out.println(f.toGenericString());*/
    //获取指定共有的属性
    /*Field f = c.getField("sex"); System.out.println(f.toGenericString());*/
    //获取类的所有属性
    /*Field[] f = c.getDeclaredFields(); for (Field field : f) { System.out.println(field.getName()); }*/
    //获取类的所有共有的属性
    /*Field[] f = c.getFields(); for (Field field : f) { System.out.println(field.getName()); }*/
    //getFields可以显示父类继承过来的属性
    //getDeclaredFields不可以显示父类继承过来的属性
    Field[] f = c.getDeclaredFields();
    for (Field field : f) {
   
        System.out.println(field.getName());
    }
}
}

动态实例化

是一种不通过new的方式来实例化对象

  • User.java 用户实体类
package com.gaoji.reflex;

import java.io.Serializable;

public class User extends p{
   
    private int id;
    
    private String name;

    public char sex;

    private transient String phone;

    public User(int id, String name, char sex, String phone) {
   
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.phone = phone;
    }

    User(int id, String name) {
   
        super();
        this.id = id;
        this.name = name;
    }

    public User(int id) {
   
        super();
        this.id = id;
    }

    public User() {
   
        super();
    }

    public int getId() {
   
        return id;
    }

    public void setId(int id) {
   

        this.id = id;
    }

    public String getName() {
   
        return name;
    }

    public void introduce() {
   
        System.out.println("this is uesr");
    }

    public void setName(String name) {
   
        this.name = name;
    }

    public char getSex() {
   
        return sex;
    }

    public void setSex(char sex) {
   
        this.sex = sex;
    }

    public String getPhone() {
   
        return phone;
    }

    public void setPhone(String phone) {
   
        this.phone = phone;
    }

    @Override
    public String toString() {
   
        return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", phone=" + phone + "]";
    }

}
  • Run.java 运行类
package com.gaoji.reflex;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//动态实例化:是一种不通过new的方式来实例化对象
public class reflexdDome_02 {
   
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
   
            Class<?> c1 = Class.forName("com.gaoji.reflex.User");
            // getConstructors();是用来获取所以public类型的构造方法
            /* * Constructor<?>[] cons=c1.getConstructors(); * for (int i = 0; i < cons.length; i++) { * System.out.println(cons[i].toGenericString()); * } */
            // .getDeclaredConstructors();获取所有的构造方法
            /* * Constructor<?>[] cons=c1.getDeclaredConstructors(); * for (int i = 0; i < cons.length;i++) { * System.out.println(cons[i].toGenericString()); * * } */

            // 获取指定的公有的构造方法
            Constructor<?> c = c1.getConstructor(int.class);
            System.out.println(c);

            // 获得指定的构造方法
            Constructor<?> c3 = c1.getDeclaredConstructor(int.class, String.class);
            System.out.println(c3);
            // 动态实例化
            User u = (User) c3.newInstance(1, "张三");
            System.out.println(u.toString());
            //
        

    }
}
  • 运行结果
			public com.gaoji.reflex.User(int)
			com.gaoji.reflex.User(int,java.lang.String)
			User [id=1, name=张三, sex=
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是空空呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值