public class ClassLoaderDemo1 {
public static void main(String[] args) {
//static ClassLoader getSystemClassLoader():返回用于委派的系统类加载器
ClassLoader c=ClassLoader.getSystemClassLoader();
System.out.println(c);//AppClassLoader
//ClassLoader getParent():返回父类加载器进行委派
ClassLoader c2=c.getParent();
System.out.println(c2);//ExtClassLoader
ClassLoader parent = c2.getParent();
System.out.println(parent);//null
}
}
反射
获取Class类的对象
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException {
//使用类的class属性来获取该类对应的Class对象
Class studentClass=Student.class;
System.out.println(studentClass);//class demo11.Student
Class studentClass1=Student.class;
System.out.println(studentClass==studentClass1);//true
System.out.println("--------------------");
//调用对象的getClass()方法 返回该对象所属类对应的Class对象
Student s=new Student();
Class extends Student> studentClass2=s.getClass();
System.out.println(studentClass2==studentClass1);//true
System.out.println("--------------------");
//调用Class类中的静态方法forName(String className)
Class> studentClass3 = Class.forName("demo11.Student");
System.out.println(studentClass3==studentClass1);//true
}
}
反射获取构造方法并使用
public class ReflectDemo2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
//获取Class对象
Class> aClass = Class.forName("demo11.Student");
Constructor>[] constructors=aClass.getConstructors();
for (Constructor con: constructors) {
System.out.println(con);
}
//getDeclaredConstructor():获取当前所有运行时类中声明的所有构造器
Constructor> declaredConstructor = aClass.getDeclaredConstructor();
System.out.println(declaredConstructor);
}
}
我的Student类是个空类,感兴趣的可以自己加构造器玩玩
反射获取成员变量并使用现在在Student类中添加属性(private的name属性,public的age属性)
public class ReflectDemo3 {
public static void main(String[] args) throws NoSuchFieldException {
Class studentClass = Student.class;
//Field[] getFields():获取所有的"公有字段"
//Field[] getDeclaredFields():获取所有字段,包括:私有、受保护、默认、公有
// Field field1 = studentClass.getField();
Field[] declaredFields = studentClass.getDeclaredFields();
for (Field field : declaredFields){
System.out.println(field);
}
System.out.println("-----------");
Field age = studentClass.getField("age");
System.out.println(age);
System.out.println("-----------");
Field name = studentClass.getDeclaredField("name");
System.out.println(name);
}
}
获取成员方法并调用
在Student类中添加两个方法(public的walk方法 private的eat方法)
public class ReflectDemo4 {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
Class studentClass = Student.class;
Method[] method = studentClass.getMethods();
for (Method me : method) {
System.out.println(me);
}
System.out.println("----------------");
Method[] declaredMethods = studentClass.getDeclaredMethods();
for (Method d: declaredMethods) {
System.out.println(d);
}
System.out.println("----------------");
Method walk = studentClass.getMethod("walk");
System.out.println(walk);
System.out.println("----------------");
//获取无参构造方法创建对象
Constructor constructor = studentClass.getConstructor();
Object obj = constructor.newInstance();
walk.invoke(obj);
}
}
PS:newInstance()方法简单讲解
- newInstance实例化对象只能调用无参构造方法
- newInstance创建类是这个类必须已经加载过且已经连接(Class.forName(“package”)这个过程),new创建类是则不需要这个类加载过
- 创建对象方式,是实用类的加载机制
- new是直接创造一个类



