基本反射了解
1 package cn.wh;
2 /**
3 * java.lang.Class
4 * @author 王恒
5 * @time 2016年11月2日 上午10:39:25
6 */
7 public class RedlectTest {
8 public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
9 Cat cat=new Cat();System.out.println("--------------------------");
10
11 Class c1=Class.forName("cn.wh.Cat");
12 Class c2=Cat.class;
13 Class c3=cat.getClass();
14
15 System.out.println(c1.getName());
16 System.out.println(c1.getSimpleName());
17 //返回一個(gè)包含某些 Class 對(duì)象的數(shù)組,這些對(duì)象表示屬于此 Class 對(duì)象所表示的類的成員的所有公共類和接口。
18 System.out.println(c1.getClasses());
19 System.out.println(c1.getPackage());
20 System.out.println(c1.getModifiers());
21 System.out.println(c1.getInterfaces());
22 System.out.println("接口數(shù)量 "+c1.getInterfaces().length);
23 System.out.println(c1.getSuperclass().getName());
24 System.out.println("實(shí)例化 "+c1.newInstance());
25 }
26 } RedlectTest 1 package cn.constructor;
2
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.InvocationTargetException;
5
6 public class TestConstructor {
7
8 public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
9 Class claxx=Class.forName("cn.wh.Cat");
10 //查看有幾個(gè)構(gòu)造方法
11 Constructor[] cs=claxx.getConstructors();
12 System.out.println("有public幾個(gè)構(gòu)造方法 "+cs.length);
13 Constructor[] cs2=claxx.getDeclaredConstructors();
14 System.out.println("總共有幾個(gè)構(gòu)造方法 "+cs2.length);
15 //暴力反射,即沒有public修飾的構(gòu)造方法,使其能夠創(chuàng)建構(gòu)造方法
16 //c.setAccessible(true);
17 //一個(gè)參數(shù)
18 Constructor c=claxx.getDeclaredConstructor(String.class);
19 System.out.println(c.getName()+"---"+c.getModifiers());
20 //兩個(gè)參數(shù)
21 Constructor c2=claxx.getDeclaredConstructor(String.class,String.class);
22 System.out.println(c.getName()+"---"+c.getModifiers());
23 //實(shí)例化
24 Object obj=c.newInstance("");
25 System.out.println(obj+"---"+obj.getClass().getSimpleName());
26 //Class<?>... parameterTypes 表示任意類型的可變參數(shù)
27 }
28 } TestConstructor 1 package cn.Field;
2
3 import java.lang.reflect.Field;
4
5 import cn.wh.Cat;
6
7 public class TestField {
8
9 public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException {
10 Class claxx=Class.forName("cn.wh.Cat");
11 Cat cat=(Cat) claxx.newInstance();
12 System.out.println("---------------------");
13 //獲取聲明字段數(shù)組
14 Field[] fs=claxx.getDeclaredFields();
15 System.out.println("數(shù)組長(zhǎng)度為 "+fs.length);
16
17 for(Field f:fs){
18 f.setAccessible(true);
19 System.out.println(f.getName()+"--"+f.getModifiers()+"---"+f.get(cat));
20 }
21
22 //獲取單個(gè)聲明字段
23 System.out.println("---------------------獲取單個(gè)聲明字段");
24 Field f2=claxx.getDeclaredField("master");
25 f2.setAccessible(true);
26 System.out.println(f2.getName()+"--"+f2.getModifiers()+"---"+f2.get(cat));
27 System.out.println(f2.getName()+"--"+f2.getModifiers()+"---"+f2.get(null));
28 //若聲明的字段是static 類型,則可以為f2.get(null),否則一律加對(duì)象
29 }
30 } TestField 1 package cn.wh;
2
3 public class Cat extends Animal{//,Maoke{
4 //protected int age=50;
5 protected String age="我的年齡50";
6 public static String master="周老師";
7
8 public Cat(){
9 System.out.println("Cat.Cat(111)");
10 }
11 public Cat(String age,String sname){
12
13 }
14
15 Cat(String master,String sname,String age1){
16
17 };
18
19 public Cat(String sname){
20 //super(); 寫與不寫 都存在! 如果寫super()必須放在第一行
21 super(sname);
22 this.sname=sname;
23 //super.eat();
24 //System.out.println("Cat.Cat(222)"+super.age);
25 //System.out.println("Cat.Cat(222)"+age);
26 }
27 // public float walk(String road){
28 // System.out.println("Cat.walk()");
29 // return 60f;
30 // }
31 //
32 float walk(){
33 System.out.println("Cat.walk()");
34 m1();
35 return 60f;
36 }
37
38
39 public void eat(){
40 System.out.println("Cat.eat(111)");
41 }
42 void m1(){
43
44 }
45
46 } Cat 1 package cn.wh;
2
3
4
5 public class Animal {
6
7 protected String sname="";
8 protected int age=80;
9 public static String master="楊老師";
10 public Animal(){
11 //System.out.println("Animal.Animal(111)");
12 sname="";
13 }
14
15 public Animal(String sname){
16 //System.out.println("Animal.Animal(222)");
17 }
18
19 public void eat(){
20 System.out.println("Animal.eat(111)");
21 }
22
23 public void eat(String foodName){
24 System.out.println("Animal.eat(222)"+foodName);
25 }
26
27 String eat(String foodName,int num){
28 System.out.println("Animal.eat(333)"+foodName);
29 return "好";
30 }
31
32
33 public void sleep(){
34
35 }
36 float walk(){
37 System.out.println("Animal.walk()");
38 return 30.5f;
39 }
40
41
42
43 } Animal 1 package cn.method;
2
3 import java.lang.reflect.InvocationTargetException;
4
5 import java.lang.reflect.Method;
6
7 import cn.wh.Cat;
8
9 public class TestMethod {
10
11 public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
12 Cat cat=new Cat();
13
14 Class claxx=Class.forName("cn.wh.Cat");
15 //獲取所有方法 包括實(shí)例方法,即對(duì)象方法 類名.getInstanll();
16 Method[] ms=claxx.getDeclaredMethods();
17 for(Method m:ms){
18 System.out.println(m.getModifiers()+"--"+m.getName()+"--"+m.getParameterCount()+"--"+m.getReturnType());
19 }
20
21 Method me=claxx.getDeclaredMethod("walk",String.class);
22 me.setAccessible(true);
23 me.invoke(cat,"貓");
24
25 Method me2=claxx.getDeclaredMethod("eat");
26 me.setAccessible(true);
27 me2.invoke(cat);
28
29 claxx.getDeclaredMethod("m1").invoke(null);
30 }
31 } TestMethod
?
轉(zhuǎn)載于:https://www.cnblogs.com/1020182600HENG/p/6022589.html
總結(jié)
- 上一篇: cc2530学习笔记
- 下一篇: python前端和后端_python是用