一、类是怎么加载到内存中的
1.类的加载:
就是将class文件读入内存,并创建一个class对象任何类在使用时系统都会创建一个class对象
2.类的加载时机:
(1).创建类的实例
(2).访问类的静态成员
(3).通过反射创建对象
(4).初始化某个子类
(5).运行main方法
3.加载器:负责将.class加载到内存,并生成Class对象
(1).BootStrap ClassLoader(根类加载器):负责java核心类的加载(jdk\jre\lib\rt.jar,API)
(2).Extension ClassLoader(扩展类加载器): 负责JRE的扩展目录中的jar包的加载(jdk\jre\lib\ext目录中)
(3).System ClassLoader(系统类加载器):负责字JVM启动时加载来自java命令的class文件,以及classpath环境变量指定的jar包和类路径
二、反射(reflect)
定义:在运行过程中“动态的获取对象的信息”和“动态的调用对象的方法”的功能就是反射
实现:就是获取Class对象,操作Class对象
三、获取Class对象
第一种:Object的getClass()方法返回Object的运行时类
第二种:通过任意对象的class静态属性
第三种:将类名作为字符串参数传递给Class类的forName()方法,参数使用类的带包路径
- package com.et.example;
- public class ReflectTest {
- public static void main(String[] args) throws ClassNotFoundException {
- //对象的getClass()方法返回对象的运行时类
- Animal a = new Animal();
- Class<? extends Animal> c1 = a.getClass();
- //任意对象的class静态属性
- Class<? extends Animal> c2 = Animal.class;
- //类名作为字符串参数传递给Class类的forName()方法,参数使用类的带包路径
- Class<? extends Object> c3 = Class.forName("com.et.example.Animal");
- System.out.println(c1==c2);
- System.out.println(c2==c3);
- }
- }
- class Animal {
- private int age;
- private String name;
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + age;
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Animal other = (Animal) obj;
- if (age != other.age)
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
- @Override
- public String toString() {
- return "Animal [age=" + age + ", name=" + name + "]";
- }
- }
四、通过反射获取类的构造方法
- package com.et.example;
- import java.lang.reflect.Constructor;
- public class ReflectTest {
- public static void main(String[] args) throws Exception{
- Class<? extends Object> c = Class.forName("com.et.example.Animal");
- //可获取访问权限内的构造方法
- Constructor<? extends Object> cst1 = c.getConstructor(int.class,String.class);
- //可获取类的所有构造方法
- Constructor<? extends Object> cst2 = c.getDeclaredConstructor();
- //取消对Java语言的访问检查
- cst2.setAccessible(true);
- Animal a1 = (Animal) cst1.newInstance(10,"小提莫");
- Animal a2 = (Animal) cst2.newInstance();
- System.out.println(a1);
- System.out.println(a2);
- }
- }
- class Animal {
- private int age;
- private String name;
- private Animal(){
- }
- public Animal(int age,String name) {
- this.age = age;
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + age;
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Animal other = (Animal) obj;
- if (age != other.age)
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
- @Override
- public String toString() {
- return "Animal [age=" + age + ", name=" + name + "]";
- }
- }
5.通过反射获取成员变量
- package com.et.example;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- public class ReflectTest {
- public static void main(String[] args) throws Exception{
- Class<? extends Object> c = Class.forName("com.et.example.Animal");
- Constructor<? extends Object> cst = c.getDeclaredConstructor();
- cst.setAccessible(true);
- Animal animal = (Animal) cst.newInstance();
- Field age = c.getDeclaredField("age");
- Field name = c.getDeclaredField("name");
- age.setAccessible(true);
- name.setAccessible(true);
- age.set(animal,20);
- name.set(animal,"大提莫");
- System.out.println(animal);
- }
- }
- class Animal {
- private int age;
- private String name;
- private Animal(){
- }
- public Animal(int age,String name) {
- this.age = age;
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + age;
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Animal other = (Animal) obj;
- if (age != other.age)
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
- @Override
- public String toString() {
- return "Animal [age=" + age + ", name=" + name + "]";
- }
- }
6.通过反射获取普通方法
- package com.et.example;
- import java.lang.reflect.Method;
- public class ReflectTest {
- public static void main(String[] args) throws Exception{
- Class<? extends Object> c = Class.forName("com.et.example.Animal");
- Method[] methods = c.getDeclaredMethods();
- for(Method method:methods) {
- //获取方法的权限类编号
- int modifiers = method.getModifiers();
- //获取方法的参数类型
- System.out.print(modifiers+"--");
- Class<?>[] parameterTypes=method.getParameterTypes();
- for(Class parameterType:parameterTypes){
- System.out.print(parameterType+"--");
- }
- System.out.println(method.getName());
- }
- }
- }
- class Animal {
- private int age;
- private String name;
- private Animal(){
- }
- public Animal(int age,String name) {
- this.age = age;
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + age;
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Animal other = (Animal) obj;
- if (age != other.age)
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
- @Override
- public String toString() {
- return "Animal [age=" + age + ", name=" + name + "]";
- }
- }