Java Reflection

package Reflection;
    
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
    
 class Demo{
        
 }
    
 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{
        
     /*must have a none parameter constructor*/
     public Person(){
            
     }
        
     public Person(String name, int age){
         this.name = name;
         this.age = age;
     }
    
     public String getName() {
         return name;
     }
    
     public void setName(String name) {
         this.name = name;
     }
    
     public int getAge() {
         return age;
     }
    
     public void setAge(int age) {
         this.age = age;
     }
    
     @Override
     public String toString() {
         return "[" + this.name + "  " + this.age + "]";
     }
    
     private String name;
     private int age;
     @Override
     public void sayChina() {
         // TODO Auto-generated method stub
            
     }
    
     @Override
     public void sayHello(String name, int age) {
         // TODO Auto-generated method stub
            
     }
 }
    
    
 public class Reflection {
        
     public static void main(String[] args) {
         /**
          * for class Demo
          */
         Demo demo = new Demo();
         System.out.println(demo.getClass().getName());
         Class<?> demo1 = null;
         Class<?> demo2 = null;
         Class<?> demo3 = null;
         try {
             demo1 = Class.forName("Reflection.Demo");
             demo2 = new Demo().getClass();
             demo3 = Demo.class;
         } catch (ClassNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         System.out.println(demo1.getSimpleName()+"  "+demo2.getName()+"  "+demo3.getName());
            
            
         /**
          * For class Person
          */
         Class<?> person = Person.class;
         Person p = null;
         try {
             p = (Person)person.newInstance();
         } catch (InstantiationException | IllegalAccessException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }       
         p.setName("Kelvin");
         p.setAge(24);
         System.out.println(p.toString());
            
         Class<?> ps = new Person().getClass();
         Constructor<?>[] cons = ps.getConstructors();
         for(int i = 0;i< cons.length;i++){
             int modifier = cons[i].getModifiers();
                
             System.out.println("constructor:"+ cons[i] );
             System.out.println("constructor:"+Modifier.toString(modifier)+" "+ cons[i] );
         }
         try {
             Person p1 = (Person) cons[0].newInstance();
             System.out.println(p1.toString());
             Person p2 = (Person) cons[1].newInstance("summer",22);
             System.out.println(p2.toString());
             Class<?>[] ifs = p2.getClass().getInterfaces();
             System.out.println(ifs[0].getSimpleName());
                
             Person p3 = (Person) cons[2].newInstance();
             Person p4 = (Person) cons[3].newInstance();
         } catch (InstantiationException | IllegalAccessException
                 | IllegalArgumentException | InvocationTargetException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
            
         /**
          * To get all methods in Person
          */
         Method[] methods = new Person().getClass().getMethods();
         for(int i =0;i < methods.length;i++){
             System.out.println(methods[i].getName());
         }
            
         try {
             Method method = new Person().getClass().getMethod("setName", String.class);
             Person pm = new Person();
             try {
                 method.invoke(pm, "jean");
                 System.out.println(pm.toString());
             } catch (IllegalAccessException | IllegalArgumentException
                     | InvocationTargetException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
         } catch (NoSuchMethodException | SecurityException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
            
     }
        
 }


知识共享许可协议
《Java Reflection》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     160.86ms