概述
Java反射源码
package com.he.main;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws Exception {
Demo_01();//从文件读取class
Demo_02();//通过获取Constructor来创建有参实例
Demo_03();//暴力获取字段
Demo_04();//暴力获取方法
Demo_05();//实例:ArrayList<Integer>中添加"abc"字符串
}
private static void Demo_05() throws Exception{
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(123);
arrayList.add(456);
//arrayList.add("abc")
Class<?> clazz = Class.forName("java.util.ArrayList");
Method method = clazz.getDeclaredMethod("add", Object.class);
method.invoke(arrayList,"abc");
/*for(Object o : arrayList){
System.out.println(o);
}*/
System.out.println(arrayList);
}
private static void Demo_04() throws Exception {
Class<?> clazz = Class.forName("com.he.main.Person");
Constructor<?> constructor = clazz.getConstructor(String.class, int.class);
Person p
= (Person) constructor.newInstance("张三",18);
Method f = clazz.getMethod("setAge", int.class);
f.invoke(p,20);
System.out.println(p);
}
private static void Demo_03() throws Exception {
Class<?> clazz = Class.forName("com.he.main.Person");
Constructor<?> constructor = clazz.getConstructor(String.class, int.class);
Person p
= (Person) constructor.newInstance("张三",18);
Field f
= clazz.getDeclaredField("age");
f.setAccessible(true);
f.set(p, 20);
System.out.println(p);
Field[] f2 = clazz.getDeclaredFields();
for(Field f3 : f2) {
System.out.println(f3.getName());
}
}
private static void Demo_02() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> clazz = Class.forName("com.he.main.Person");
Constructor<?> constructor = clazz.getConstructor(String.class, int.class);
Person p
= (Person) constructor.newInstance("张三",18);
System.out.println(p);
}
private static void Demo_01() throws ClassNotFoundException {
Class<?> clazz1 = Class.forName("com.he.main.Person");
//System.out.println(clazz1);
Class<?> clazz2 = Person.class;
Class<?> clazz3 = new Person().getClass();
System.out.println((clazz1 == clazz2) ? ((clazz2 == clazz3) ? clazz1 == clazz3 : false) : false);
}
}
Person类
package com.he.main;
public class Person implements Comparable<Person>{
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person person = (Person) o;
if (getAge() != person.getAge()) return false;
return getName() != null ? getName().equals(person.getName()) : person.getName() == null;
}
@Override
public int hashCode() {
int result = getName() != null ? getName().hashCode() : 0;
result = 31 * result + getAge();
return result;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
@Override
public int compareTo(Person o) {
Person p = (Person) o;
if(name == p.name){
if(age > p.getAge())
return 1;
else if (age == p.age)
return 0;
else{
return -1;
}
}else if(name.getBytes()[0] > p.name.getBytes()[0]){
return 1;
}else if(name.getBytes()[0] == p.name.getBytes()[0]){
return 0;
}else{
return -1;
}
}
}
最后
以上就是自然机器猫为你收集整理的Java反射练习的全部内容,希望文章能够帮你解决Java反射练习所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复