麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 網站 > WEB開發 > 正文

java反射技術

2024-04-27 15:19:04
字體:
來源:轉載
供稿:網友

一、概述

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

二、加載類

java中有一個Class類代表某一個類的字節碼

方式一 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); } //構造方法 public Person(){ System.out.println("無參構造方法。。。"); } public Person(String name){ System.out.println("name :一個參數的構造函數。。。"); } public Person(String name,int age){ System.out.println("name,age : 兩個參數的構造函數。。。。"); } private Person(List list){ System.out.println("List : 私有構造函數。。。。。"); } //成員方法 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.反射類的構造方法

/** * 反射構造函數: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); } /** * 反射構造函數: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("小紅"); } /** * 反射構造函數: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); } /** * 反射構造函數: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"}); }

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

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 色综合激情 | 逼片视频| 暖暖免费观看高清完整版电影 | 日韩一级免费毛片 | 久久综合一区 | 亚洲精品在线观看免费 | 久久经典国产视频 | 久久亚洲成人 | 黄视频免费在线观看 | 在线成人免费观看视频 | 免费在线观看国产精品 | 成人午夜一区 | 久久视频精品 | 99国内精品视频 | 亚州综合图片 | 亚州成人在线观看 | 一级黄色大片在线观看 | 96视频在线免费观看 | 91av久久| 色播久久 | 欧美一级免费在线观看 | 九草视频 | 91成人免费网站 | www.69色| 毛片视频大全 | chinese18 xxxx videos | 亚洲网站免费 | 欧美精品一区二区久久 | 海角在线观看91一区二区 | 一级黄片毛片免费看 | 97干色| 久久久大片 | 激情97| 成人午夜淫片a | 免费看毛片的网站 | 国产一区二区三区四区在线 | 精品无吗乱吗av国产爱色 | 高清在线国产 | 久久我不卡 | 日本在线一区二区 | 久久丝袜脚交足黄网站免费 |