注解与反射09:类加载内存分析
package com.reflection;
public class Test05 {
public static void main(String[] args) {
A a = new A();
System.out.println(A.m);
}
}
class A{
static int m = 100;
static {
System.out.println("A类静态代码块初始化");
m = 300;
}
// static int m = 100;
public A()
{
System.out.println();
}
}
注解与反射10:分析类初始化
package com.reflection;
public class Test06 {
static {
System.out.println("main类被加载");
}
public static void main(String[] args) throws ClassNotFoundException {
// 主动引用
// Son son = new Son();
// 反射也会产生主动引用
// Class.forName("com.reflection.Son");
// 不会产生类的引用的方法
System.out.println(Son.b);
Son[] array = new Son[5];
System.out.println(Son.M);
}
}
class Father{
static int b = 2;
static {
System.out.println("父类被加载");
}
}
class Son extends Father {
static {
System.out.println("子类被加载");
m = 300;
}
static int m = 100;
static final int M = 1;
}
注解与反射11:类加载器的作用
package com.reflection;
public class Test07 {
public static void main(String[] args) throws ClassNotFoundException {
// 获得系统类加载器
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
System.out.println(systemClassLoader);
// 获得系统类加载器的父类加载器-->扩展类加载器
ClassLoader parent = systemClassLoader.getParent();
System.out.println(parent);
// 获得扩展类加载器的父类加载器-->?根加载器
ClassLoader parent1 = parent.getParent();
System.out.println(parent1);
// 测试当前类是哪个加载器加载的
ClassLoader classLoader = Class.forName("com.reflection.Test07").getClassLoader();
System.out.println(classLoader);
// 测试jdk类加载器
classLoader = Class.forName("java.lang.Object").getClassLoader();
System.out.println(classLoader);
// 如何获得系统类加载器可以加载的路径
System.out.println(System.getProperty("java.class.path"));
// 双亲委派机制
}
}
注解与反射12:获取类的运行时结构
package com.reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test08 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class c1 = Class.forName("com.reflection.User");
// User user = new User();
// c1 = user.getClass();
// 获得类的名字
System.out.println(c1.getName());
System.out.println(c1.getSimpleName());
// 获得类的属性(只能找到public属性)
Field[] field = c1.getFields();
for (Field field1 : field) {
System.out.println(field1);
}
Field[] fields = c1.getDeclaredFields();
for (Field field1 : fields) {
System.out.println(field1);
}
Field name = c1.getDeclaredField("name");
System.out.println(name);
// 获得类的方法(包括继承的)
Method[] methods = c1.getMethods();
for (Method method : methods) {
System.out.println("正常的" + method);
}
// 获得本类的所有方法(包括私有的)
Method[] declaredMethods = c1.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
System.out.println(declaredMethod);
}
// 获得指定方法
Method getName = c1.getMethod("getName", null);
Method setName = c1.getMethod("setName", String.class);
System.out.println(getName);
System.out.println(setName);
// 获得构造器
Constructor[] constructors = c1.getConstructors();
Constructor[] declaredConstructors = c1.getDeclaredConstructors();
// 获得指定构造器
Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
System.out.println(declaredConstructor);
}
}