//简单的例子
public class ReflextionMain {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException, NoSuchMethodException {
EqualsMain refOne = new EqualsMain();//类全称为: equals.EqualsMain
System.out.println(refOne.getClass().getName());//equals.EqualsMain
System.out.println(EqualsMain.class.getName());//equals.EqualsMain
System.out.println(EqualsMain.class.getPackage().getName());//equals
/**
* 对应的类
*/
Class<?> classOne = Class.forName("equals.EqualsMain");
System.out.println(classOne.getName());//equals.EqualsMain
EqualsMain equOne = (EqualsMain) classOne.newInstance();
equOne.setId("5");
System.out.println(equOne.getId());//5
/**
* 所有的构造函数
*/
Constructor<?>[] constructors = classOne.getConstructors();
EqualsMain equTwo = (EqualsMain) constructors[0].newInstance("123");
EqualsMain equThree = (EqualsMain) constructors[1].newInstance();
System.out.println(equTwo.getId() + "|||" + equThree.getId());//123|||null
System.out.println(classOne.getClassLoader().getClass().getName());//sun.misc.Launcher$AppClassLoader
/**
* 名称为id的字段
*/
Field fieldOne = classOne.getDeclaredField("id");
fieldOne.setAccessible(true);
fieldOne.set(equOne, "456");
System.out.println(fieldOne.get(equOne));//456
/**
* 父类
*/
Class<?> superClass = classOne.getSuperclass();
System.out.println(superClass.getName());//java.lang.Object
/**
* 包含的字段
*/
Field[] fields = classOne.getDeclaredFields();
for (Field field : fields){
System.out.println(field);//private java.lang.String equals.EqualsMain.id
}
/**
* 包含的方法
*/
Method[] methods = classOne.getDeclaredMethods();
/**
* 打印内容如下:
* public static void equals.EqualsMain.main(java.lang.String[])
* main
* void
* 9
* [java.lang.String[] arg0]
* public boolean equals.EqualsMain.equals(java.lang.Object)
* equals
* boolean
* 1
* [java.lang.Object arg0]
* public int equals.EqualsMain.hashCode()
* hashCode
* int
* 1
* []
* public java.lang.String equals.EqualsMain.getId()
* getId
* class java.lang.String
* 1
* []
* public void equals.EqualsMain.setId(java.lang.String)
* setId
* void
* 1
* [java.lang.String arg0]
*/
for (Method method : methods){
System.out.println(method);//函数代码写法
System.out.println(method.getName());//函数名
System.out.println(method.getReturnType());//函数返回类型
System.out.println(method.getModifiers());//函数访问修饰符
System.out.println(Arrays.toString(method.getParameters()));//函数参数
}
/**
* 实现的接口
*/
Class<?>[] interfaces = classOne.getInterfaces();
for (Class clazz : interfaces){
System.out.println(clazz);//interface java.io.Serializable
}
/**
* 打印内容如下:
* map中的数据
* ----------------------
*/
Method methodMain = classOne.getMethod("main", java.lang.String[].class);
methodMain.invoke(classOne.newInstance(), (Object) new String[]{"999"});
}
}
public class EqualsMain implements Serializable {
private String id;
public EqualsMain(String id){
this.id = id;
}
public EqualsMain(){};
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public boolean equals(Object obj) {
if (null == obj) return false;
if (this.id.equals(((EqualsMain)obj).getId())){
return true;
}else {
return false;
}
}
public static void main(String[] args){
EqualsMain equalsMain1 = new EqualsMain("10");
EqualsMain equalsMain2 = new EqualsMain("20");
EqualsMain equalsMain3 = new EqualsMain("10");
Map<Object , String> map = new HashMap<>();
map.put(equalsMain1, "map中的数据");
System.out.println(map.get(equalsMain3));
System.out.println("----------------------");
}
}