- int[] a = new int[10];
- int[] b = new int[100];
- Class c10 = a.getClass();
- Class c11 = b.getClass();
- // 只要数组的元素类型与维度一样,就是同一个Class
- System.out.println(c10 == c11);
- Person p = new Person();
- Class extends Person> calzz = p.getClass();
- System.out.println(calzz);
- Class> clazz1 = Class.forName("day10.Person");
- System.out.println(clazz1);
- Class
clazz2 = Person.class; - System.out.println(clazz2);
- ClassLoader classLoader = ClassModelTest.class.getClassLoader();
- Class> calzz3 = classLoader.loadClass("day10.Person");
- System.out.println(calzz3);
- Class c1 = Object.class;//类
- Class c2 = Comparable.class;//接口
- Class c3 = String[].class;
- Class c4 = int[][].class;//数组
- Class c5 = ElementType.class;//枚举
- Class c6 = Override.class;//异常
- Class c7 = int.class;//基本数据类型
- Class c8 = void.class;//空参
- Class c9 = Class.class;
- public class ClassLoadingTest {
- public static void main(String[] args) {
- System.out.println(A.m);
- }
- }
- class A {
- static {
- m = 1;
- }
- static int m = 2;
- }
- //第二步:链接结束后m=0
- //第三步:初始化后,m的值由
()方法执行决定 - // 这个A的类构造器
()方法由类变量的赋值和静态代码块中的语句按照顺序合并 - 产生,类似于
- //
(){ - // m = 1;
- // m = 2;
- // }
类的主动引用(一定会发生类的初始化)
类的被动引用(不会发生类的初始化)
- public static void main(String[] args) throws ClassNotFoundException {
- //获取一个系统类加载器
- ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
- System.out.println(classLoader);
-
- //获取系统类加载器的父类加载器,即扩展类加载器
- ClassLoader classLoader1 = classLoader.getParent();
- System.out.println(classLoader1);
-
- //获取扩展类加载器的父类加载器,即引导类加载器
- ClassLoader classLoader2 = classLoader1.getParent();
- System.out.println(classLoader2);//null
-
- //获取类由哪个类加载器进行加载
- ClassLoader classLoader4 = Class.forName("java.lang.Object").getClassLoader();
- System.out.println(classLoader4);
-
- }
应用场景:Properties:用来读取配置文件。
- @Test
- public void test2() throws IOException {
- Properties pros = new Properties();
-
- //读取配置文件的方式1;
- //此时的文件默认在当前的module的src下下。
- ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
- InputStream is = classLoader.getResourceAsStream("jdbc1.properties");
- pros.load(is);
-
- //读取配置文件的方式2:
- //此时的文件默认在当前的module下。
- // FileInputStream fis = new FileInputStream("jdbc.properties");
- // pros.load(fis);
-
- String user = pros.getProperty("user");
- String possWord = pros.getProperty("passWord");
- System.out.println("user=" + user + "\t" + "passWord=" + possWord);
- }
小知识点:在javabean中为什么要求提供一个public的空参构造器?
- @Test
- public void test4() throws Exception {
- Class
clazz = (Class) Class.forName("day10.Person"); - Person person = clazz.newInstance();
- System.out.println(person);//Person{name='null', age=0}
- }
- @Test
- public void test5() throws Exception {
- Person p = new Person();
- Class extends Person> clazz = p.getClass();
- //获取指定的构造器,声明构造器的参数列表
- Constructor extends Person> constructor = clazz.getDeclaredConstructor(String.class);
- //保证次构造器可以访问(构造器的权限为public时,该步可省略)
- constructor.setAccessible(true);
- //使用获取的该构造器创造对象
- Person person = constructor.newInstance("嘻戏");
- System.out.println(person);
- }
- @Test
- public void test6() throws ClassNotFoundException {
- ClassLoader classLoader = ClassModelTest.class.getClassLoader();
- Class> clazz = classLoader.loadClass("day10.Person");
- Field[] fields = clazz.getDeclaredFields();
- for (int i = 0; i < fields.length; i++) {
- System.out.println(fields[i]);
- //获取权限修饰符
- int modifiers = fields[i].getModifiers();
- System.out.print("权限修饰符:" + Modifier.toString(modifiers) + "\t");
- //获取属性数据类型
- Class> type = fields[i].getType();
- System.out.print("数据类型:" + type.getName() + "\t");
- //获取变量名称
- String fName = fields[i].getName();
- System.out.print("变量名" + fName);
-
- System.out.println();
- }
- }
- @Test
- public void test6() throws Exception {
- //实例化运行实例对象
- Class> clazz1 = Class.forName("day10.Person");
- //创建运行时类的对象
- Person person = (Person) clazz1.newInstance();
- //获取运行时类中指定变量名的属性
- Field name = clazz1.getDeclaredField("name");
- //保证当前属性是可访问的
- name.setAccessible(true);
- //设置属性的值
- name.set(person, "Jane");
- //获取属性的值
- System.out.println(name.get(person));
- }
- @Test
- public void test7(){
- Person p = new Person();
- Class extends Person> clazz = p.getClass();
- Method[] methods = clazz.getDeclaredMethods();
- for (Method m : methods) {
- //获取当前运行时类中声明的所有方法。(不包含父类中声明的方法)
- System.out.println("运行时类方法:" + m + "\t");
- //获取方法声明的注解
- Annotation[] annotations = m.getAnnotations();
- for (Annotation a : annotations) {
- System.out.print("注解:" + a + "\t");
- }
- //获取方法权限修饰符
- System.out.print("方法权限修饰符:" + Modifier.toString(m.getModifiers()) + "\t");
- //获取方法返回值类型
- System.out.print("返回值类型:" + m.getReturnType().getName() + "\t");
- //获取方法名
- System.out.print("方法名:" + m.getName() + "\t");
- //获取方法形参列表
- Class>[] parameterTypes = m.getParameterTypes();
- for (int i = 0; i < parameterTypes.length; i++) {
- if (i == parameterTypes.length -1) {
- System.out.print("args_" + i + "=" + parameterTypes[i].getName() + "\t");
- break;
- }
- if (i == 0)
- System.out.print("形参列表:" + "args_" + i + "=" + parameterTypes[i].getName() + ",");
- System.out.print("args_" + i + "=" + parameterTypes[i].getName() + ",");
-
- }
- //获取方法抛出的异常
- Class>[] exceptionTypes = m.getExceptionTypes();
- if (exceptionTypes.length > 0) {
- System.out.print("异常:");
- for (int i = 0; i < exceptionTypes.length; i++) {
- if (i == exceptionTypes.length - 1) {
- System.out.print(exceptionTypes[i].getName());
- break;
- }
- System.out.print(exceptionTypes[i].getName() + ",");
- }
- }
-
- System.out.println();
- }
-
- }
方法有返回值则返回对应的返回值,如果调用的运行时类中的方法没有返回值,则此invoke()返回null
- @Test
- public void test7() throws Exception {
- //实例化运行实例对象
- Class
clazz1 = Person.class; - //创建运行时类的对象
- Person person = clazz1.newInstance();
- //获取指定的某个方法
- Method show = clazz1.getDeclaredMethod("show", String.class, int.class);
- //保证当前方法是可访问的
- show.setAccessible(true);
- //设置方法的形参
- Object returnValue = show.invoke(person, "Jane", 23);
- System.out.println(returnValue);
-
- Method voidModel = clazz1.getDeclaredMethod("voidModel", Person.class);
- voidModel.setAccessible(true);
- }
调用静态方法
- Method model = clazz1.getDeclaredMethod("model");
- model.setAccessible(true);
- // Object o = model.invoke(null);
- Object o = model.invoke(Person.class);//该处null/Person.class没有意义,因为invoke方法需要传入参数
- System.out.println(o);
- @Test
- public void test8() throws ClassNotFoundException {
- Class> clazz = Class.forName("day10.Person");
- Constructor>[] constructors = clazz.getDeclaredConstructors();
- for (Constructor c : constructors) {
- System.out.println(c);
- }
- }
- @Test
- public void test9() throws Exception {
- Person p = new Person();
- Class extends Person> clazz = p.getClass();
- //获取指定的构造器
- Constructor extends Person> constructor = clazz.getDeclaredConstructor(String.class);
- //保证此构造器是可访问的
- constructor.setAccessible(true);
- //调用此构造器创建运行时类的对象
- Person person = constructor.newInstance("Jane");
- System.out.println(person);
- }
- @Test
- public void test10() throws Exception {
- ClassLoader classLoader = ClassModelTest.class.getClassLoader();
- Class> clazz = classLoader.loadClass("day10.Person");
- //获取运行时类的父类
- Class> superclass = clazz.getSuperclass();
- System.out.println(superclass);
- //获取运行时类的带泛型的父类
- Type genericSuperclass = clazz.getGenericSuperclass();
- System.out.println(genericSuperclass);
- }
- @Test
- public void test11() throws ClassNotFoundException {
- Class> clazz = Class.forName("day10.Person");
- Type genericSuperclass = clazz.getGenericSuperclass();
- ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
- Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
- // System.out.println(actualTypeArguments[0].getTypeName());
- System.out.println(((Class)actualTypeArguments[0]).getName());
- }
- @Test
- public void test12(){
- Person p = new Person();
- Class extends Person> clazz = p.getClass();
- //获取运行时类实现的接口
- Class>[] interfaces = clazz.getInterfaces();
- for (Class c: interfaces) {
- System.out.println(c);
- }
- System.out.println();
- //获取运行时类父类实现的接口
- Class>[] interfaces1 = clazz.getSuperclass().getInterfaces();
- for (Class c1 : interfaces1) {
- System.out.println(c1);
- }
- }
- @Test
- public void test13() throws Exception {
- ClassLoader classLoader = ClassModelTest.class.getClassLoader();
- Class> clazz = classLoader.loadClass("day10.Person");
- Package aPackage = clazz.getPackage();
- System.out.println(aPackage);
- }
- @Test
- public void test14(){
- Class
clazz = Person.class; - Annotation[] annotations = clazz.getAnnotations();
- for (Annotation a : annotations) {
- System.out.println(a);
- }
- }