Code Analysis(3)之反射机制

In ToyTest.java, use reflection to create a Toy object using the non-default constructor.

import java.lang.reflect.*;

interface HasBatteries {}
interface Waterproof {}
interface Shoots {}

class Toy {
    Toy() {
        System,out.println("Creating Toy() object");    
    }
    Toy(int i) {
         System,out.println("Creating Toy(" + i + ") object");
    }
}

class FancyToy extends Toy implements HasBatteries, Waterproof, Shoots {
    FancyToy() { super(1); }
}

public class ToyTest19 {
    public static void main(String[] args) {
    // get appropriate constructor and create new instance:
    try {
        Toy.class.getDeclaredConstructor(int.class).newInstance(1);
        // catch four exceptions:
    } catch(NoSuchMethodException e) {
         System,out.println("No such method: " + e);
    } catch(InstantiationException e) {
         System,out.println("Unable make Toy: " + e);
    } catch(IllegalAccessException e) {
         System,out.println("Unable access: " + e);
    } catch(InvocationTargetException e) {
         System,out.println("Target invocation problem: " + e);
    }
    }
}/* Output:
Creating Toy(1) object 
*///:~
posted @ 2010-08-07 19:41  philander  阅读(293)  评论(0编辑  收藏  举报