java反射创建实体类_JAVA反射--看完原来这么简单(1)

大家好,我是JAVA智慧君,这篇文章给大家详细说一说JAVA中反射机制是咋玩的,

反射。。。第一次学习反射机制的时候有点蒙,感觉很难得样子,这个东西其实非常的重要,在很多java框架中都在使用反射,话不多说,智慧君开始进入正题。

1、什么叫反射机制

下面是官方的定义:

Reflection enables Java code to discover information about the fields, methods and constructors of loaded classes, and to use reflected fields, methods, and constructors to operate on their underlying counterparts, within security restrictions.

The API accommodates applications that need access to either the public members of a target object (based on its runtime class) or the members declared by a given class. It also allows programs to suppress default reflective access control.

我的解释就是:反射能够在运行时类中获取类的方法,属性,构造器等;

现在大家知道反射的强大之处了吧!现在跟大家一起研究研究JAVA反射机制神奇之处,错误之处希望大家能多多指出。

2、获取运行时类的三种方式:

我首先创建了一个为student的实体类

public class Student {

public Integer id;

public String name;

private Integer age;

public Boolean sex;

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

@Override

public String toString() {

return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";

}

}

然后main方法中去执行

public class testMain {

public static void main(String[] args) throws Exception {

//获取运行时类的三种方式

System.out.println("-----------------------获取运行时类的第一种方式----------------------");

Student st = new Student();

Class clazz1 = st.getClass();

System.out.println(clazz1.getName());

System.out.println("-----------------------获取运行时类的第二种方式----------------------");

Class clazz2 = Student.class;

System.out.println(clazz2.getName());

System.out.println("-----------------------获取运行时类的第三种方式----------------------");

Class clazz3 = Class.forName("zhihuijun.com.Student");

System.out.println(clazz3.getName());

}

}

大家可以看出,所说的运行时类就是这个Class类了,具体他有什么作用呢,请大家继续往下看。

3、给实体类中的属性赋值

我习惯用第二种方法,获取到运行时类之后还要创建运行时类的对象

运行时类创建对象用的是newInstance;

获取student里面的属性用的是field.

首先要通过运行时类创建对象,然后再通过set方法给公共属性进行赋值,我在student类中name用了public,大家来看看我的代码吧

public static void main(String[] args) throws Exception {

Class clazz2 = Student.class;

Student student = clazz2.newInstance();//创建运行时类的Student类的对象

System.out.println(student);

Field field = clazz2.getField("name"); //获取公共属性name

field.set(student, "张三");//给属性赋值

System.out.println(student);

}

打印结果

Student [id=null, name=null, age=null, sex=null]

Student [id=null, name=张三, age=null, sex=null]

赋值成功,我们再试一试私有的属性看看

public static void main(String[] args) throws Exception {

Class clazz2 = Student.class;

Student student = clazz2.newInstance();//创建运行时类的Student类的对象

System.out.println(student);

Field field = clazz2.getField("sex"); //获取公共属性name

field.set(student, 2);//给属性赋值

System.out.println(student);

}

打印结果

Student [id=null, name=null, age=null, sex=null]

Exception in thread "main" java.lang.IllegalArgumentException: Can not set java.lang.Boolean field zhihuijun.com.Student.sex to java.lang.Integer

at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)

at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)

at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)

at java.lang.reflect.Field.set(Unknown Source)

at zhihuijun.com.testMain.main(testMain.java:11)

没有这个属性,这个是封装导致的,难道反射就不能获取到私有的属性吗?答案是

可以的。

好的,上代码给大家看看怎么玩的。

私有属性要用getDeclaredField来获取Field,然后再field.setAccessible(true)进行解锁就可以了。

Class clazz2 = Student.class;

Student student = clazz2.newInstance();//创建运行时类的Student类的对象

System.out.println(student);

Field field = clazz2.getDeclaredField("age");

field.setAccessible(true);

field.set(student, 2);//给属性赋值

System.out.println(student);

打印结果

Student [id=null, name=null, age=null, sex=null]

Student [id=null, name=null, age=2, sex=null]

欢迎大家指出错误之处,谢谢大家继续关注智慧君,在后面几篇文章中也会继续为大家讲解反射的知识。886


版权声明:本文为weixin_30227789原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。