Java反射机制三

1.返回一个类实现的接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package  Reflect;
 
interface  China{
     public  static  final  String name= "Rollen" ;
     public  static   int  age= 20 ;
     public  void  sayChina();
     public  void  sayHello(String name,  int  age);
}
 
class  Person  implements  China{
     public  Person() {
         
     }
     public  Person(String sex){
         this .sex=sex;
     }
     public  String getSex() {
         return  sex;
     }
     public  void  setSex(String sex) {
         this .sex = sex;
     }
     @Override
     public  void  sayChina(){
         System.out.println( "hello ,china" );
     }
     @Override
     public  void  sayHello(String name,  int  age){
         System.out.println(name+ "  " +age);
     }
     private  String sex;
}
 
class  hello{
     public  static  void  main(String[] args) {
         Class<?> demo= null ;
         try {
             demo=Class.forName( "Reflect.Person" );
         } catch  (Exception e) {
             e.printStackTrace();
         }
         //保存所有的接口
         Class<?> intes[]=demo.getInterfaces();
         for  ( int  i =  0 ; i < intes.length; i++) {
             System.out.println( "实现的接口   " +intes[i].getName());
         }
     }
}

2.实现的接口   Reflect.China

(注意,以下几个例子,都会用到这个例子的Person类,所以为节省篇幅,此处不再粘贴Person的代码部分,只粘贴主类hello的代码)

【案例】:取得其他类中的父类

1
2
3
4
5
6
7
8
9
10
11
12
13
class  hello{
     public  static  void  main(String[] args) {
         Class<?> demo= null ;
         try {
             demo=Class.forName( "Reflect.Person" );
         } catch  (Exception e) {
             e.printStackTrace();
         }
         //取得父类
         Class<?> temp=demo.getSuperclass();
         System.out.println( "继承的父类为:   " +temp.getName());
     }
}

3.获得其他类中的全部构造函数

这个例子需要在程序开头添加import java.lang.reflect.*;

然后将主类编写为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class  hello{
     public  static  void  main(String[] args) {
         Class<?> demo= null ;
         try {
             demo=Class.forName( "Reflect.Person" );
         } catch  (Exception e) {
             e.printStackTrace();
         }
         Constructor<?>cons[]=demo.getConstructors();
         for  ( int  i =  0 ; i < cons.length; i++) {
             System.out.println( "构造方法:  " +cons[i]);
         }
     }
}


构造方法:  public Reflect.Person()

构造方法:  public Reflect.Person(java.lang.String)

但是细心的读者会发现,上面的构造函数没有public 或者private这一类的修饰符

下面这个例子我们就来获取修饰符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class  hello{
     public  static  void  main(String[] args) {
         Class<?> demo= null ;
         try {
             demo=Class.forName( "Reflect.Person" );
         } catch  (Exception e) {
             e.printStackTrace();
         }
         Constructor<?>cons[]=demo.getConstructors();
         for  ( int  i =  0 ; i < cons.length; i++) {
             Class<?> p[]=cons[i].getParameterTypes();
             System.out.print( "构造方法:  " );
             int  mo=cons[i].getModifiers();
             System.out.print(Modifier.toString(mo)+ " " );
             System.out.print(cons[i].getName());
             System.out.print( "(" );
             for ( int  j= 0 ;j<p.length;++j){
                 System.out.print(p[j].getName()+ " arg" +i);
                 if (j<p.length- 1 ){
                     System.out.print( "," );
                 }
             }
             System.out.println( "){}" );
         }
     }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值