获取Class对象的三种方式
1.通过类名获取 类名.class
class c1=Person.class;
2.通过对象获取 对象名.getClass()
创建一个对象,通过getClass方法获取Class对象:
People p = new People();
Class people = p.getClass();//返回此 Object 的运行时类对象。
3.通过全类名获取 Class.forName(全类名)
通过Class类的静态方法forName(),根据全类名获取Class对象
Class people = Class.forName("com.oracle.demo01.Person");
反射相当于解剖,而解剖使用的就是Class类中的方法.所以先要获取到每一个字节码文件对应的Class类型的对象
Class类:常用类
成员变量:Field
构造方法: Constructor
成员方法:Method
反射:通过Class类的方法获取以上的三个对应的对象,然后通过Filed/Method/Contructor对象调用其方法去真正的使用类的成员、方法、构造方法
package com.oracle.demo01;
public class Person {
private String name;
public int age;
public Person() {
System.out.println("public Person()");
}
public Person(String name) {
this.name = name;
System.out.println(" public Person(String name)");
}
private Person(int age) {
this.age = age;
System.out.println("private Person(int age)");
}
static {
System.out.println("静态代码块");
}
public void eat() {
System.out.println("public void eat()");
}
public void sleep(String name,int age) {
System.out.println(name+"......"+age);
System.out.println("public void sleep(String name,int age)");
}
public void sleep(String name) {
System.out.println("public void sleep(String name)");
}
private void smoke(int age){
System.out.println("private void smoke(int age)");
}
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
package com.oracle.demo01;
public class Demo02 {
public static void main(String[] args) throws ClassNotFoundException {
//获取字节码文件对象
//1.通过对象获取
Class c1=new Person().getClass();
System.out.println(c1);
//2.通过类名获取
Class c2=Person.class;
System.out.println(c2);
//System.out.println(c1==c2);//true 比的地址 只有一个所以是true
//3.通过Class类的静态方法获取
Class c3=Class.forName("com.oracle.demo01.Person");
System.out.println(c3);
}
}
package com.oracle.demo02;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import com.oracle.demo01.Person;
public class Demo01 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//method02();
//method03();
method04();
}
public static void method01() throws ClassNotFoundException {
// 获取字节码文件对象
Class c = Class.forName("com.oracle.demo01.Person");
// 获取所有公共的构造方法对象数组
// Constructor[] cons=c.getConstructors();
// 获取所有的构造方法
Constructor[] cons = c.getDeclaredConstructors();
for (Constructor con : cons) {
System.out.println(con);
}
}
public static void method02() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//获取字节码文件对象
Class c=Class.forName("com.oracle.demo01.Person");
//获取空参构造方法对象
Constructor con=c.getConstructor();
//调用构造方法创建对象
Object obj=con.newInstance();
//向下转型
Person p=(Person)obj;
p.eat();
}
public static void method03() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//获取字节码文件对象
Class c=Class.forName("com.oracle.demo01.Person");
//获取有参构造方法对象
Constructor con=c.getConstructor(String.class);
//调用构造方法创建对象
Object obj=con.newInstance("张三丰");
//向下转型
Person p=(Person)obj;
p.eat();
}
//暴力反射:抢银行(违反了面向对象的封装思想,破坏了程序 的安全性,不推荐使用!)
public static void method04() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//获取字节码文件对象
Class c=Class.forName("com.oracle.demo01.Person");
//获取有参构造方法对象
Constructor con=c.getDeclaredConstructor(int.class);
//暴力反射,取消java语言检测访问
con.setAccessible(true);
//调用构造方法创建对象
Object obj=con.newInstance(666);
//向下转型
Person p=(Person)obj;
p.eat();
}
}
package com.oracle.demo02;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.print.attribute.standard.Media;
public class Demo03 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//获取字节码文件对象
Class c=Class.forName("com.oracle.demo01.Person");
//获取公共有参成员方法
Method method=c.getMethod("sleep",String.class,int.class);
//快速创建对象
Object obj=c.newInstance();
//调用方法
method.invoke(obj,"张三丰",666);
}
}