一、类对象
1.创建类对象
Class<?> class1 = Class.forName("d01_student.Student");
2.类对象方法(反射)
2.1构造方法
Class<?> class1 = forName("d01_student.Student"); //获取类对象
class1.getName() //d01_student.Student
class1.getSimpleName()); //Student
class1.getPackage()); //package d01_student
class1.getPackage().getName()); //d01_student
class1.getSuperclass()); //class java.lang.Object
Class<?>[] interfaces = class1.getInterfaces(); //继承的接口
//获取构造方法
Class<?> class1 = forName("d01_student.Student");
//获取所有构造方法
Constructor<?>[] constructors = class1.getConstructors();
//获取单个构造方法
Constructor<?> constructor = class1.getConstructor(String.class, int.class, String.class);
2.2所有方法
1.getMethods()获得公开的所有方法,没有私有的、保护的、默认的方法(包括继承的公开方法)
Method[] methods = class1.getMethods();
2.getDeclaredMethods()获得类中所有的方法,包括私有的、默认的、保护的,(不包括继承的)
Method[] declaredMethods = class1.getDeclaredMethods();
3.
Method methodShow1 = class1.getMethod("show");
二、类的对象(new)
1.创建类的对象
Class<?> class1 = Class.forName("d01_student.Student");
Student stu1 = (Student) class1.newInstance();
2.1构造方法
2.1.1获取单个构造方法
注意这里没有像下面获取无参构造方法一样的简单写法
// Student s1 = (Student) class3.newInstance("夏雨荷",18,"女"); ???????????///是错误的,没有这种方法
/*要想获得有参数的构造方法必须
1.先Class.forName()获取类的对象
2.再获取带某个参数的构造方法
3.再用这个constructor ,newInstance一个对象,如下:*/
System.out.println("获取单个构造方法");
Constructor<?> constructor = class1.getConstructor(String.class, int.class, String.class);
Student student = (Student) constructor.newInstance("大明", 12, "男");
System.out.println(student);
Student{name='大明', age=12, gender='男'}
2.1.2获取无参数构造方法
Constructor<?> constructor2 = class1.getConstructor();
Student student2 = (Student) constructor2.newInstance();
System.out.println(student2.toString());
无参构造执行了...
Student{name='null', age=0, gender='null'}
2.1.3简单写法(无参)
Student s3 = (Student) class1.newInstance();
System.out.println(s3.toString());
Student{name='null', age=0, gender='null'}
2.2获取单个方法
//Method methodShow2 = class1.getMethod("show", String.class);
Method methodShow1 = class1.getMethod("show");
Student stu1 = (Student) class1.newInstance();
methodShow1.invoke(stu1);
这里是show的无参数方法:姓名:null 年龄:0 性别:null
Process finished with exit code 0
2.3私有方法(设置访问权限失效)
Method method4 = class1.getDeclaredMethod("privateMethod");
//设置访问权限失效
method4.setAccessible(true);
method4.invoke(stu1);
2.4可以调用公开的(不能调用私有的)方法
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
invokeAny(properties,"setProperty",new Class[]{String.class,String.class},"username","zhangsan");
System.out.println(properties.toString());
}
public static Object invokeAny(Object obj, String methodName, Class<?>[] paramsType, Object... args)throws Exception{
Class<?> class1 = obj.getClass();
Method method = class1.getMethod(methodName, paramsType);
return method.invoke(obj, args);
}
2.5静态方法staticMethod.invoke( null);
Method staticMethod = class1.getMethod("staticMethod");
staticMethod.invoke( null);
2.5使用反射给属性赋值
public static void reflectOp6() throws Exception{
//1.获取对象
Class<?> class1 = forName("d01_student.Student");
//使用反射给属性赋值
Field name = class1.getDeclaredField("name");
//访问权限失效
name.setAccessible(true);
Student stu1 = (Student) class1.newInstance();
name.set(stu1,"大明");
System.out.println(name.get(stu1));
}