toytest19.java

来自「JAVA编程思想第四版英文原版习题答案. pdf原版的」· Java 代码 · 共 43 行

JAVA
43
字号
// 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 + =
减小字号Ctrl + -
显示快捷键?