Java核心技术卷
对象分析者 理解还不够深 后续再回顾
package objectAnalyzer;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Objects;
public class ObjectAnalyzer {
private ArrayList<Object> visited = new ArrayList<>();
public String toString(Object obj) {
if (obj == null)
return "null";
if (visited.contains(obj))
return "...";
visited.add(obj);
Class c1 = obj.getClass();
if (c1 == String.class)
return (String) obj;
if (c1.isArray()) {
String r = c1.getComponentType() + "[]{";
for (int i = 0; i < Array.getLength(obj); i++) {
if (i > 0)
r += ",";
Object val = Array.get(obj, i);
if (c1.getComponentType().isPrimitive()) r += val;
else
r += toString(val);
}
return r + "}";
}
String r =c1.getName();
do{
r += "[";
Field[] fields = c1.getDeclaredFields();
AccessibleObject.setAccessible(fields,true);
for(Field f : fields){
if(!Modifier.isStatic(f.getModifiers())){
if(!r.endsWith("["))
r += ",";
r += f.getName() + "=";
try {
Class t = f.getType();
Object val = f.get(obj);
if(t.isPrimitive()) r += val;
else r += toString(val);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
r+= "]";
c1 = c1.getSuperclass();
}
while (c1 != null);
return r;
}
}
package objectAnalyzer;
import java.util.ArrayList;
public class ObjectAnalyzerT {
public static void main(String[] args){
ArrayList<Integer> squares = new ArrayList<>();
for(int i = 1 ; i <= 5 ; i++)
squares.add(i*i);
System.out.println(new ObjectAnalyzer().toString(squares));
}
}
需要学习下 idea的使用 查看方法声明等 快捷键使用 待更新
————————————————6.15
查阅了别人的博客后的自我总结
首先是API查询
反射获取Class对象的三种方式
- Class c = Class.forName("");
- Employee e = new Employee(); Class c = e.getClass();
- Class c = Employee.class;
类的实例化 (对象)
Class c = Class.forName(“Employee”);
Object o = c.newInstance();
通过反射获取构造方法并使用
package reflection;
public class Student {
public Student(){
}
private String name;
private int age;
private char sex;
@Override
public String toString(){
return "Student[name=" + name + ", age=" + age + ",sex=" + sex + "]";
}
}
package reflection;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class Fields{
public static void main(String[] args) throws Exception {
Class stuClass = Class.forName("reflection.Student");
System.out.println("_________获取所有共有的实例域_____________");
Field[] fields = stuClass.getDeclaredFields();
for (Field f : fields){
Class type = f.getType();
String name = f.getName();
String modifiers = Modifier.toString(f.getModifiers());
System.out.println(modifiers +":" + type.getName()+ ":" +name);
}
for (Field f : fields){
System.out.println(f);
}
}
}
————————————6.16 更
System.out.println("___________获取公有字段 并调用________________");
Field f = stuClass.getField("name");
System.out.println(f);
Object o = stuClass.getConstructor().newInstance();
f.set(o , "小明");
Student stu = (Student) o;
System.out.println("验证姓名:" +stu.name);
System.out.println("___________获取私有字段 并调用________________");
f = stuClass.getDeclaredField("age");
System.out.println(f);
f.setAccessible(true);
f.set(o , 15);
System.out.println("验证年龄:" + stu.age);
setAccessible(true) 方法 用此方法可以访问私有实例域