📄 toytest19.java
字号:
// typeinfo/toys/ToyTest19.java
// TIJ4 Chapter Typeinfo, Exercise 19, page 593
// In ToyTest.java, use reflection to create a Toy object using
// the non-default constructor.
package typeinfo.toys;
import static net.mindview.util.Print.*;
import java.lang.reflect.*;
interface HasBatteries {}
interface Waterproof {}
interface Shoots {}
class Toy {
Toy() {
print("Creating Toy() object");
}
Toy(int i) {
print("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) {
print("No such method: " + e);
} catch(InstantiationException e) {
print("Unable make Toy: " + e);
} catch(IllegalAccessException e) {
print("Unable access: " + e);
} catch(InvocationTargetException e) {
print("Target invocation problem: " + e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -