知识基础
- 类
- 对象
- 构造方法
- 重载
- 单例模式
动态加载
JVM在执行的时候,并不是一次性把所有的class加载到内存中的,而是用到谁加载谁。
反射的概述
https://www.cnblogs.com/tech-bird/p/3525336.html
Reflection(反射)是Java被视为动态语言的关键,反射机制允许程序在执行期借助于Reflection API取得任何类的內部信息,并能直接操作任意对象的内部属性及方法
获取class对象的三种方式
- class.forName("全类名")
- 类名.class
- 对象.getClass(),如果上下文存在某个实例对象,可以通过getClass获取他的类
public class re { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { Class cls1 = Class.forName("demo.Person"); Class cls2 = Person.class; Person person = new Person(); Class cls3 = person.getClass(); System.out.println(cls1); System.out.println(cls2); System.out.println(cls3); System.out.println(cls1==cls2); System.out.println(cls3==cls2); }
类“初始化”执行顺序是什么
package demo; public class test { public static void main(String[] args) { Ref ref = new Ref(); } } class Ref{ static { System.out.println("最先执行\r\n"); } { System.out.println("第二执行\r\n"); } public Ref(){ System.out.println("最后执行\r\n"); } }
demo1
package demo; public class test { public static void main(String[] args) throws ClassNotFoundException { Class.forName("demo.CalcDemo"); } } class CalcDemo { static { try { Runtime rt = Runtime.getRuntime(); Process pc = rt.exec("calc"); pc.waitFor(); } catch (Exception e) { } } }
Class方法
获取变量
- Field getField(name):根据字段名获取某个public的field(包括父类)
- Field getDeclaredField(name):根据字段名获取当前类的某个field(不包括父类)
- Field[] getFields():获取所有public的field(包括父类)
- Field[] getDeclaredFields():获取当前类的所有field(不包括父类)
cls3.getField("aaa");//指定名称的public修饰的 cls3.getFields();//获取所有public修饰的成员变量 cls3.getDeclaredField("aaaa");//获取所有 cls3.getDeclaredFields();
获取构造方法
cls3.getConstructors(); cls3.getConstructor("aaa"); cls3.getDeclaredConstructor("bbb"); cls3.getDeclaredConstructors();
package demo; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class test { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { // Ref ref = new Ref(); Class cls = Class.forName("demo.Person"); Constructor constructor = cls.getConstructor(); Constructor constructor1 = cls.getConstructor(String.class); constructor.newInstance(); constructor1.newInstance("jl"); } }
demo2
package demo; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; public class test { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Class cls = Class.forName("java.lang.ProcessBuilder"); Method methodStart = cls.getMethod("start"); Constructor constructor = cls.getConstructor(List.class); Object obj = constructor.newInstance(Arrays.asList("calc.exe")); methodStart.invoke(obj); } }
demo3-可变长参数
package demo; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; public class test { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Class cls = Class.forName("java.lang.ProcessBuilder"); Method methodStart = cls.getMethod("start"); Constructor constructor = cls.getConstructor(String[].class); Object obj = constructor.newInstance(new String[][]{{"calc.exe"}}); methodStart.invoke(obj); } }
获取成员方法
cls3.getConstructors(); cls3.getConstructor("aaa"); cls3.getDeclaredConstructor("bbb"); cls3.getDeclaredConstructors();
demo4
package demo; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class test { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { // Ref ref = new Ref(); Class cls = Class.forName("demo.Person"); Constructor constructor = cls.getConstructor(); Constructor constructor1 = cls.getConstructor(String.class); constructor.newInstance(); constructor1.newInstance("jl"); } }
demo5
package demo; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class test { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { Class cls = Class.forName("java.lang.Runtime"); Method method = cls.getMethod("exec", String.class); Method method1 = cls.getMethod("getRuntime"); method.invoke(method1.invoke(cls),"calc.exe"); } }
Runtime类就是单例模式,我们只能通过 Runtime.getRuntime() 来获取到 Runtime 对象。