本文主要是介绍java_反射概述,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
反射概述
获得反射对象
package com.test5;
//什么叫反射
public class test1 {
public static void main(String[] args) throws ClassNotFoundException {
//通过反射获取类的class对象
Class c1=Class.forName("com.test5.User");
System.out.println(c1); //class com.test5.User
Class c2=Class.forName("com.test5.User");
Class c3=Class.forName("com.test5.User");
//一个类在内存中只有一个class对象
//一个类被加载后,类的整个结构都会被封装在class对象中
System.out.println(c2.hashCode()); //925858445
System.out.println(c3.hashCode()); //925858445
}
}
//实体类: pojo ,entity
class User{
private String name;
private int id;
private int age;
public User() {}
public User(String name, int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
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;
}
@Override
public String toString() {
return "user [name=" + name + ", id=" + id + ", age=" + age + "]";
}
}
Class类
这篇关于java_反射概述的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!