反射
1.获取Class对象的方法
public class Test {
public static void main ( String[ ] args) {
Class clazz1 = Student. class ;
Student s = new Student ( 18 , 0 ) ;
Class clazz2 = s. getClass ( ) ;
try {
Class clazz3 = Class. forName ( "cn.itlaobing.ref.Student" ) ;
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
}
}
}
2.获取构造器
方法 描述 Connstructor getConstructor(Class<?>…paramterTypes) 获取Class对象对应类的带指定参数列表的public构造器 Connstructor<?> getContructors() 获取Class对象对应类的所有public构造器 Connstructor getDeclaredConstructor (Class<?>…paramterTypes) 获取Class对象对应类的带指定参数列表的构造器,与访问修饰符无关 Connstructor<?> getDeclaredConstructors() 获取Class对象对应类的所有构造器,与访问修饰符无关
2.1示例
public class Test2 {
public static void main ( String[ ] args) throws NoSuchMethodException, SecurityException {
Class< Student> clazz = Student. class ;
Constructor[ ] c1 = clazz. getConstructors ( ) ;
Constructor[ ] c2 = clazz. getDeclaredConstructors ( ) ;
Constructor c = clazz. getConstructor ( int . class , double . class ) ;
System. out. println ( c) ;
Constructor construct1 = clazz. getDeclaredConstructor ( double . class ) ;
System. out. println ( construct1) ;
Constructor construct2 = clazz. getDeclaredConstructor ( ) ;
System. out. println ( construct2) ;
}
}
3.获取成员方法
方法 描述 Method getMethod(String name,Class<?>…paramterTypes) 获取指定名称和参数类型class对象的public修饰方法 Method[] getMethods() 获取所有public修饰的成员方法以及继承的方法,包括静态方法 Method getDeclaredMethod(String name,Class<?>…paramterTypes) 获取指定名称和参数类型class对象的方法,与访问修饰符无关 Method[] getDeclaredMethods() 获取自身声明的所有成员方法,与访问修饰符无关
3.1示例
public class MethodTest {
public static void main ( String[ ] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Student s = new Student ( 0 ) ;
Class clazz = s. getClass ( ) ;
Method[ ] methods = clazz. getMethods ( ) ;
Method[ ] methods2 = clazz. getDeclaredMethods ( ) ;
Method method2 = clazz. getDeclaredMethod ( "newObject" , int . class ) ;
Method method3 = clazz. getDeclaredMethod ( "makeMoney" ) ;
method3. invoke ( s) ;
Method method4 = clazz. getDeclaredMethod ( "sleep" , String. class ) ;
method4. invoke ( null, "蔡老师" ) ;
}
}
4.获取成员变量
方法 描述 Field getField(String name) 获取指定名称的public修饰的成员变量 Field[] getFields() 获取所有的public修饰的成员变量,包括从父类继承的成员变量 Field getDeclaredField(String name) 获取指定名称的成员变量,与访问权限无关 Field[] getDeclaredFields() 获取所有的成员变量,与访问修饰符无关
public static void main ( String[ ] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Son s = new Son ( ) ;
Class clazz = s. getClass ( ) ;
Field[ ] fields = clazz. getFields ( ) ;
Field[ ] fields2 = clazz. getDeclaredFields ( ) ;
Field f1 = clazz. getField ( "age" ) ;
f1. setInt ( s, 10 ) ;
System. out. println ( s. age) ;
Field f3 = clazz. getField ( "name" ) ;
f3. set ( s, "张三" ) ;
System. out. println ( s. name) ;
Field f2 = clazz. getDeclaredField ( "width" ) ;
System. out. println ( f2) ;
f2. setAccessible ( true ) ;
f2. setInt ( s, 100 ) ;
System. out. println ( s. getWidth ( ) ) ;
}
5.获取注解
6.Method对象方法
方法 描述 invoke(Object obj,Object…obj) 调用对象方法 setAccessible(boolean flag) 设置私有方法可访问
7.实例化对象
方法 描述 clazz.newInstance() 只能调用默认无参构造方法 Constructor.newInstance(Object…obj) 调用对应的构造方法
7.1示例
public class NewInstance {
public static void main ( String[ ] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class clazz = Class. forName ( "cn.itlaobing.ref2.Student" ) ;
Constructor c = clazz. getDeclaredConstructor ( int . class ) ;
c. setAccessible ( true ) ;
Student s = ( Student) c. newInstance ( 18 ) ;
System. out. println ( s. getAge ( ) ) ;
}
}
8.作业实例
1.使用反射创建FileInputStream以及BufferedInputStream读取某个文件,并利用反射创建 FileOutputStream和BufferedOutputStream对象,将文件写入到E://File(如果使用File对象) 也必须使用反射创建对象
package com. itlaobing. z0830;
import java. io. BufferedInputStream;
import java. io. BufferedOutputStream;
import java. io. FileInputStream;
import java. io. FileOutputStream;
import java. io. InputStream;
import java. io. OutputStream;
import java. lang. reflect. Constructor;
import java. lang. reflect. InvocationTargetException;
import java. lang. reflect. Method;
@SuppressWarnings ( "all" )
public class ReflectFileTest {
public static void main ( String[ ] args) {
Class clazz1 = FileInputStream. class ;
Class clazz2 = BufferedInputStream. class ;
Class clazz3 = FileOutputStream. class ;
Class clazz4 = BufferedOutputStream. class ;
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
Method bisClose = null;
Method bosClose = null;
try {
Constructor constructor = clazz1. getDeclaredConstructor ( String. class ) ;
fis = ( FileInputStream) constructor. newInstance ( "D:/1/Menu.java" ) ;
Constructor constructor2 = clazz2. getDeclaredConstructor ( InputStream. class ) ;
bis = ( BufferedInputStream) constructor2. newInstance ( fis) ;
Method read = clazz2. getMethod ( "read" , byte [ ] . class ) ;
bisClose = clazz2. getDeclaredMethod ( "close" ) ;
Constructor constructor3 = clazz3. getDeclaredConstructor ( String. class ) ;
fos = ( FileOutputStream) constructor3. newInstance ( "D:/1/File/Menu.java" ) ;
Constructor constructor4 = clazz4. getDeclaredConstructor ( OutputStream. class ) ;
bos = ( BufferedOutputStream) constructor4. newInstance ( fos) ;
Method write = clazz4. getMethod ( "write" , byte [ ] . class ) ;
bosClose = clazz4. getMethod ( "close" ) ;
int i = 0 ;
byte [ ] b = new byte [ 1024 ] ;
while ( ( i = ( int ) read. invoke ( bis, b) ) > - 1 ) {
write. invoke ( bos, b) ;
}
} catch ( NoSuchMethodException e) {
e. printStackTrace ( ) ;
} catch ( SecurityException e) {
e. printStackTrace ( ) ;
} catch ( InstantiationException e) {
e. printStackTrace ( ) ;
} catch ( IllegalAccessException e) {
e. printStackTrace ( ) ;
} catch ( IllegalArgumentException e) {
e. printStackTrace ( ) ;
} catch ( InvocationTargetException e) {
e. printStackTrace ( ) ;
} finally {
try {
bisClose. invoke ( bis) ;
bosClose. invoke ( bos) ;
} catch ( IllegalAccessException e) {
e. printStackTrace ( ) ;
} catch ( IllegalArgumentException e) {
e. printStackTrace ( ) ;
} catch ( InvocationTargetException e) {
e. printStackTrace ( ) ;
}
}
}
}