H5W3
当前位置:H5W3 > 其他技术问题 > 正文

java反射机制-高级特性详解

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

.  
介绍


Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

    Java的反射机制是指在程序的运行状态中,可以动态的构造任意一个类的对象,可以了解任意一个对象所属的类,可以了解任意一个类的成员变量和方法,可以调用任意一个对象的属性和方法,这就是JAVA反射的特性。

Java反射机制提供了那些功能:

1.     在运行时判断任意一个对象所属的类

2.     
在运行时构造任意一个类的对象

3.     
在运行时判断任意一个类所具有的成员变量和方法;


Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

4.     
在运行时调用任意一个对象的方法

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

二.JAVA反射的使用

    1.获取class类对象的方式

              a.通过Object类的getClass()方法获取

                     例如:

                            Person
person=new Person();

                            Class<?>
cls=person.getClass();

                            Person
person2=( Person) cls.newInstance();

                b.通过静态属性Class方式获取

                      例如:

                             Class<?> cls=Person.Class();

                             Person person=( Person) cls.newInstance();

               c.通过Class类中静态方法forName()方法获取

                       例如:

                             Class<?> cls= Class.forName(“包名.类全名”)//如:com.cn. Person

                             Person person=( Person) cls.newInstance();

    2.反射获取构造器方法

           a. 获取所有构造器

                public Constructor[] getConstructors()

                public Constructor[] getDeclaredConstructors()

           b.获取单个指定构造器

                 public
Constructor getConstructor(Class… parameterTypes)

                 public
Constructor getDeclaredConstructor(Class… parameterTypes)

    3.反射获取方法

          a.获取所有方法

                  getDeclareMethods()

          b.获取方法的返回值类型

                  getReturnType()

          c.
获取方法的传入参数类型

                  getParameterTypes();

           d.获取指定的方法

                  getDeclareMethod方法名,参数类型.class,….”

1.  成员变量

a. 获取所有成员变量

getDeclaredFields 

b. 获取特定的成员变量

getDeclaredField(参数类型.class,….) 

2.  父类和父接口

a.获取某类的父类

getSuperclass()

b. 获取某类实现的接口

getInterfaces()

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

三.  
反射使用例子

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

1.  定义一个Car类,该类拥有两个构造函数、一个方法及3个属性,如下所示:

public class Car {    private String brand;    private String color;    private int maxSpeed;    public Car() {        System.out.println("init car!!");    }    public Car(String brand, String color, int maxSpeed) {        this.brand = brand;        this.color = color;        this.maxSpeed = maxSpeed;    }    public void introduce() {        System.out.println("brand:" + brand + ";color:" + color + ";maxSpeed:" +
         maxSpeed);    }    public String getBrand() {        return brand;    }    public void setBrand(String brand) {        this.brand = brand;    }    public String getColor() {        return color;    }    public void setColor(String color) {        this.color = color;    }    public int getMaxSpeed() {        return maxSpeed;    }    public void setMaxSpeed(int maxSpeed) {        this.maxSpeed = maxSpeed;    }}

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

2. 通过反射机制操作如下:

import java.lang.reflect.Constructor;import java.lang.reflect.Method;public class ReflectTest {    public static Car initByDefaultConst() throws Throwable {        //①通过类装载器获取Car类对象        ClassLoader loader = Thread.currentThread().getContextClassLoader();        Class clazz = loader.loadClass("com.smart.reflect.Car");        //②获取类的默认构造器对象并通过它实例化Car        Constructor cons = clazz.getDeclaredConstructor((Class[]) null);        Car car = (Car) cons.newInstance();        //③通过反射方法设置属性        Method setBrand = clazz.getMethod("setBrand", String.class);        setBrand.invoke(car, "红旗CA72");        Method setColor = clazz.getMethod("setColor", String.class);        setColor.invoke(car, "黑色");        Method setMaxSpeed = clazz.getMethod("setMaxSpeed", int.class);        setMaxSpeed.invoke(car, 200);        return car;    }    public static void main(String[] args) throws Throwable {        Car car = initByDefaultConst();        car.introduce();    }}

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

本文地址:H5W3 » java反射机制-高级特性详解

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址