标签:data- tco getname wait keyword public csdn 代理 blog
一、java反射
1、反射:动态获取类的信息,以及动态调用对象的方法的功能。可以理解为动态看透类的能力。
2、主要功能:在运行时判断任意一个对象所属的类;在运行时构造任意一个类的对象;在运行时判断任意一个类所具有的成员变量和方法;在运行时调用任意一个对象的方法;生成动态代理。
通过Java反射机制,可以在程序中访问已经装载到JVM中的java对象的描述,实现访问、检测和修改描述java对象本身信息的功能。java反射机制的功能十分强大,java.lang.reflect包中提供了对该功能的支持。
二、通过反射获取类的三种方法
-
- Class c1=Class.forName("csdn.Student");
-
-
- Student s=new Student();
- Class c2 = s.getClass();
-
- Class c3=Student.class;
三、通过反射可以获取到的主要描述信息
- package csdn;
-
- public class Person {
- public String name;
- public int age;
-
- public Person() {
- super();
- }
-
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
-
- public String showInfo() {
- return "name=" + name + ", age=" + age;
- }
- }
子类:Student类
- package csdn;
-
- public class Student extends Person implements Study {
- public String className;
- private String address;
-
- public Student() {
- super();
- }
-
- public Student(String name, int age, String className, String address) {
- super(name, age);
- this.className = className;
- this.address = address;
- }
-
- public Student(String className) {
- this.className = className;
- }
-
- public String toString() {
- return "姓名:" + name + ",年龄:" + age + ",班级:" + className + ",住址:"
- + address;
- }
-
- public String getAddress() {
- return address;
- }
-
- public void setAddress(String address) {
- this.address = address;
- }
-
- }
接口:study
- package csdn;
-
- public interface Study {
-
- }
1、获得属性:
- Class stu = Class.forName("csdn.Student");
-
- Field[] fields = stu.getFields();
- for (Field f : fields) {
- System.out.println(f);
- }
- System.out.println("---------------------");
-
- Field[] declaredFields = stu.getDeclaredFields();
- for (Field ff : declaredFields) {
- System.out.println(ff);
- }
运行结果:
public java.lang.String csdn.Student.className
public java.lang.String csdn.Person.name
public int csdn.Person.age
---------------------
public java.lang.String csdn.Student.className
private java.lang.String csdn.Student.address
2、获得方法:
- Class stu = Class.forName("csdn.Student");
-
- Method[] methods = stu.getMethods();
- for (Method m : methods) {
- System.out.println(m);
- }
- System.out.println("---------------------");
-
- Method[] declaredMethods = stu.getDeclaredMethods();
- for (Method ms : declaredMethods) {
- System.out.println(ms);
- }
运行结果:
public java.lang.String csdn.Student.toString()
public java.lang.String csdn.Student.getAddress()
public void csdn.Student.setAddress(java.lang.String)
public java.lang.String csdn.Person.showInfo()
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
---------------------
public java.lang.String csdn.Student.toString()
public java.lang.String csdn.Student.getAddress()
public void csdn.Student.setAddress(java.lang.String)
3、获得构造方法:
为了演示区别,将Student类中参数为className的构造方法的public修饰符改为protected,查看效果。
- Class stu = Class.forName("csdn.Student");
-
- Constructor[] constructors = stu.getConstructors();
- for (Constructor c : constructors) {
- System.out.println(c);
- }
- System.out.println("---------------------");
-
- Constructor[] declaredConstructors = stu.getDeclaredConstructors();
- for (Constructor con : declaredConstructors) {
- System.out.println(con);
- }
运行结果为:
public csdn.Student(java.lang.String,int,java.lang.String,java.lang.String)
public csdn.Student()
---------------------
protected csdn.Student(java.lang.String)
public csdn.Student(java.lang.String,int,java.lang.String,java.lang.String)
public csdn.Student()
4、获得其他:
- Class stu = Class.forName("csdn.Student");
- System.out.println(stu.getName());
- System.out.println(stu.getPackage());
- Class[] interfaces = stu.getInterfaces();
- for(Class in:interfaces){
- System.out.println(in);
- }
运行结果为:
csdn.Student
package csdn
interface csdn.Study
5、通过反射实例化对象,调用对象方法
- package csdn;
-
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Method;
-
- public class TestReflect {
-
- @SuppressWarnings("unchecked")
- public static void main(String[] args) {
- try {
- Class c = Class.forName("csdn.Student");
- Student stu = (Student) c.newInstance();
-
- stu.setAddress("深圳南山");
- System.out.println(stu);
-
- Constructor<Student> constructor = c.getConstructor(String.class,
- int.class, String.class, String.class);
- Student s = (Student) constructor.newInstance("李四", 18, "七班", "深圳");
- System.out.println(s);
-
- Method show = c.getMethod("showInfo");
- System.out.println(show.invoke(s));
-
- } catch (Exception e) {
-
- e.printStackTrace();
- }
- }
- }<span style="color:#3333ff;"><strong>
- </strong></span>
运行结果为:
姓名:null,年龄:0,班级:null,住址:深圳南山
姓名:李四,年龄:18,班级:七班,住址:深圳
name=李四, age=18
Java编程基础-反射
标签:data- tco getname wait keyword public csdn 代理 blog
原文地址:http://www.cnblogs.com/stsinghua/p/6413848.html