首页 > 代码库 > Java反射学习笔记
Java反射学习笔记
示例类
@SuppressWarnings("unused")public class Person { public String Name; private int Age; public Gender Gender; private static String Species = "人类"; public Person(){ Name="佚名"; Age=-1; } public Person(String name){ Name=name; } private Person(String name,int age){ Name=name; Age=age; } private Person(Gender g){ Gender=g; } public void Run(){ System.out.println(Name+" 跑!"); } public void Attack(){ System.out.println(Name+" 打!"); } public void Attack(String name){ System.out.println(Name+" 打 "+name+"!"); } private void Eat(String food){ System.out.println(Name+" 吃 "+food); } public void Introduce() { System.out.println("我叫"+Name+",我今年"+Age+"岁了。"); } public static void PlayGame(String gameName){ System.out.println("玩 "+gameName+" 游戏"); } public static void main(String[] args){ System.out.println("main"); for(String s:args) System.out.println(s); }}enum Gender{ Male,Female}
反射类的无参构造函数:
@Test//反射类的无参构造函数public void constructor1() throws Exception{ Class clazz = Class.forName("pro.shaowei.reflect.Person"); Constructor c=clazz.getConstructor(); Person p = (Person) c.newInstance(); Person p1 = (Person) clazz.newInstance(); p.Introduce(); p.Run(); p1.Introduce(); p1.Run();}
反射类的有参构造函数:
@Test//反射类的有参构造函数public void constructor2() throws Exception{ Class clazz=Class.forName("pro.shaowei.reflect.Person"); Constructor c=clazz.getConstructor(String.class); Person p=((Person) c.newInstance("张三")); p.Introduce(); p.Run();}
反射类的私有构造函数:
@Test//反射类的私有构造函数public void constructor3() throws Exception{ Class clazz=Class.forName("pro.shaowei.reflect.Person"); //反射私有构造函数时必须从使用 getDeclaredConstructor 方法 Constructor c=clazz.getDeclaredConstructor(String.class,int.class); c.setAccessible(true);//暴力反射 Person p=((Person) c.newInstance("张三",25)); p.Introduce(); p.Run();}
反射类的公有无参方法:
@Test//反射类的公有无参方法public void method1() throws Exception{ Class clazz=Class.forName("pro.shaowei.reflect.Person"); Person p=(Person) clazz.newInstance(); Method method=clazz.getMethod("Run"); method.invoke(p);}
反射类的公有有参方法:
@Test//反射类的公有有参方法public void method2() throws Exception{ Class clazz=Class.forName("pro.shaowei.reflect.Person"); Person p=(Person) clazz.newInstance(); Method method=clazz.getMethod("Attack",String.class); method.invoke(p,"李四");}
反射类的私有有参方法:
@Test//反射类的私有有参方法public void method3() throws Exception{ Class clazz=Class.forName("pro.shaowei.reflect.Person"); Person p=(Person) clazz.newInstance(); Method method=clazz.getDeclaredMethod("Eat",String.class); method.setAccessible(true); method.invoke(p,"香蕉");}
反射类的静态有参方法:
@Test//反射类的静态有参方法public void method4() throws Exception{ Class clazz=Class.forName("pro.shaowei.reflect.Person"); Person p=(Person) clazz.newInstance(); Method method=clazz.getDeclaredMethod("PlayGame",String.class); method.setAccessible(true); method.invoke(p,"扫雷");}
反射类的main方法:
@Test//反射类的main方法public void method5() throws Exception{ Class clazz=Class.forName("pro.shaowei.reflect.Person"); Person p=(Person) clazz.newInstance(); Method method=clazz.getDeclaredMethod("main",String[].class); method.setAccessible(true); method.invoke(p,(Object)new String[]{"1","2"});}
反射类公有的字段:
@Test//反射类公有的字段public void field1() throws Exception{ Class clazz=Class.forName("pro.shaowei.reflect.Person"); Person p=(Person) clazz.newInstance(); Field field=clazz.getField("Name"); System.out.println(field.get(p)); field.set(p, "王五"); p.Introduce();}
反射类私有的字段:
@Test//反射类私有的字段public void field2() throws Exception{ Class clazz=Class.forName("pro.shaowei.reflect.Person"); Person p=(Person) clazz.newInstance(); Field field=clazz.getDeclaredField("Age"); field.setAccessible(true); System.out.println(field.get(p)); field.set(p, 7); p.Introduce();}
反射类私有静态的字段:
@Test//反射类私有静态的字段public void field3() throws Exception{ Class clazz=Class.forName("pro.shaowei.reflect.Person"); Person p=(Person) clazz.newInstance(); Field field=clazz.getDeclaredField("Species"); field.setAccessible(true); System.out.println(field.get(p)); field.set(p, "不死族"); System.out.println(field.get(p));}
Java反射学习笔记
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。