testnewcar.java

来自「java 完全探索的随书源码」· Java 代码 · 共 25 行

JAVA
25
字号
import java.lang.reflect.Constructor;public class TestNewCar{  public static void main(String args[]) {    try {      Class car = Class.forName("NewCar");      // create the array of parameter types and find the constructor      Class param[] = {int.class};      Constructor con = car.getDeclaredConstructor(param);      // use a wrapper class to hold the int argument 4      Object values[] = {new Integer(4)};      // create an instance of the class      NewCar carObj = (NewCar)con.newInstance(values);      System.out.println("The car has " + carObj.getTireCount() + " tires");    }    catch (Exception e){      System.out.println("Something went wrong while instantiating NewCar");      e.printStackTrace(System.err);    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?