国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > WEB開發(fā) > 正文

java反射技術(shù)

2024-04-27 15:19:04
字體:
供稿:網(wǎng)友

一、概述

一個類有多個組成部分,例如:成員變量、成員方法、構(gòu)造方法等。 反射就是加載類,并解剖出類的各個組成部分。

二、加載類

java中有一個Class類代表某一個類的字節(jié)碼

方式一 Class clazz = Class.forName("cn.wzk.reflect.Person");

方式二 Class clazz = Person.Class;

方式三 Class clazz = new Person().getClass();

三、解剖類

1.反射類的字段

class Person{ public String name; PRivate int age; public void show(){ System.out.println(this.name); System.out.println(this.age); } //構(gòu)造方法 public Person(){ System.out.println("無參構(gòu)造方法。。。"); } public Person(String name){ System.out.println("name :一個參數(shù)的構(gòu)造函數(shù)。。。"); } public Person(String name,int age){ System.out.println("name,age : 兩個參數(shù)的構(gòu)造函數(shù)。。。。"); } private Person(List list){ System.out.println("List : 私有構(gòu)造函數(shù)。。。。。"); } //成員方法 public void method1(){ System.out.println("method1........"); } public void method1(String name,int age){ System.out.println(name+"------"+age); } public String method1(String name,int[] age){ return name; } public static void method1(int num){ System.out.println(num); } private void method1(InputStream is){ System.out.println(is); }}Person p = new Person();Class clazz= Class.forName("cn.wzk.reflect.Person");Field name= clazz.getField("name");name.set(p, "哈哈");Field age = clazz.getDeclaredField("age");age.setaccessible(true);age.set(p,20);p.show();

2.反射類的構(gòu)造方法

/** * 反射構(gòu)造函數(shù):public Person() */ @Test public void test2() throws Exception{ Class clazz = Class.forName("cn.wzk.reflect.Person"); Constructor c = clazz.getConstructor(); Person p = (Person)c.newInstance(); System.out.println(p.name); } /** * 反射構(gòu)造函數(shù):public Person(String name) * @throws Exception */ @Test public void test3() throws Exception{ Class clazz = Class.forName("cn.wzk.reflect.Person"); Constructor c = clazz.getConstructor(String.class); Person p = (Person)c.newInstance("小紅"); } /** * 反射構(gòu)造函數(shù):public Person(String name,int age) * @throws Exception */ @Test public void test4() throws Exception{ Class clazz = Class.forName("cn.wzk.reflect.Person"); Constructor c = clazz.getConstructor(String.class,int.class); Person p =(Person)c.newInstance("Mike",20); } /** * 反射構(gòu)造函數(shù):private Person(List list) * @throws Exception */ @Test public void test5() throws Exception{ Class clazz = Class.forName("cn.wzk.reflect.Person"); Constructor c = clazz.getDeclaredConstructor(List.class); c.setAccessible(true); Person p = (Person)c.newInstance(new ArrayList()); }

3.反射類的成員方法

/** * 反射成員方法:public void method1() * @throws Exception */ @Test public void test6() throws Exception{ Class clazz = Class.forName("cn.wzk.reflect.Person"); Person p = (Person)clazz.newInstance(); Method method = clazz.getMethod("method1"); method.invoke(p); } /** * 反射成員方法:public void method1(String name,int age) * @throws Exception */ @Test public void test7() throws Exception{ Class clazz = Class.forName("cn.wzk.reflect.Person"); Person p = (Person)clazz.newInstance(); Method method = clazz.getMethod("method1",String.class,int.class); method.invoke(p,"mike",20); } /** * 反射成員方法:public String method1(String name,int[] age) * @throws Exception */ @Test public void test8() throws Exception{ Class clazz = Class.forName("cn.wzk.reflect.Person"); Person p = (Person)clazz.newInstance(); Method method = clazz.getMethod("method1",String.class,int[].class); Object name =method.invoke(p,"jack",new int[]{1,2,3}); System.out.println(name); } /** * 反射成員方法:public static void method1(int num) * @throws Exception */ @Test public void test9() throws Exception{ Class clazz = Class.forName("cn.wzk.reflect.Person"); Person p = (Person)clazz.newInstance(); Method method = clazz.getMethod("method1",int.class); method.invoke(p,5); } /** * 反射成員方法:private void method1(InputStream is) * @throws Exception */ @Test public void test10() throws Exception{ Class clazz = Class.forName("cn.wzk.reflect.Person"); Person p = (Person)clazz.newInstance(); Method method = clazz.getDeclaredMethod("method1",InputStream.class); method.setAccessible(true); method.invoke(p,new FileOutputStream("a.txt")); }

4.反射類的main方法

public static void main(String[] args) { System.out.println("mian方法。。。。。");}/** * 反射類的main方法 */ @Test public void test11() throws Exception{ Class clazz = Class.forName("cn.wzk.reflect.Person"); Method method = clazz.getDeclaredMethod("main",String[].class); method.invoke(null, (Object)new String[]{"aa","bb"}); }

注意 對于反射的參數(shù)是數(shù)組的情況下,會將數(shù)組進行拆開(其實是為了兼容jdk1.4)jdk1.4沒有可變數(shù)組。

解決 方式一: method.invoke(null, (Object)new String[]{"aa","bb"}); 方式二: method.invoke(null,new Object[]{new String[]{"aa","bb"}});


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 富民县| 基隆市| 千阳县| 大城县| 汕尾市| 县级市| 新龙县| 台北市| 通城县| 正宁县| 屏东县| 睢宁县| 宕昌县| 富川| 长乐市| 文水县| 兴义市| 长岛县| 棋牌| 莱芜市| 扎囊县| 高邑县| 炉霍县| 邯郸县| 镇坪县| 高安市| 宝丰县| 门头沟区| 西吉县| 衡南县| 房产| 越西县| 阳信县| 稷山县| 含山县| 阿克陶县| 夏邑县| 宁阳县| 铁岭市| 伊吾县| 黑山县|