⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 carshop.java

📁 java 完全探索的随书源码
💻 JAVA
字号:
/* * CarShop */import java.lang.reflect.*;public class CarShop {  Car carList[];  public CarShop( String[] carTypes ) {    // create and start a car of each specified type    carList = new Car[carTypes.length];    for ( int i=0; i<carTypes.length; i++ ) {      carList[i] = createCar(carTypes[i], new TireSpecification());      startCar( carList[i] );    }  }  public Car createCar(String carName, TireSpecification tires) {    Car newCar = null;    try {      Object constructorParam[] = new TireSpecification[1];      constructorParam[0]= tires;      // get the class name for the car that you want      Class carClass = Class.forName(carName);      // create an array of Classes, and use this to      // array to find the constructor that you want      Class parameters[] = new Class[1];      parameters[0]= TireSpecification.class;      Constructor  con = carClass.getDeclaredConstructor(parameters);      // create a car instance for the carList      newCar = (Car)con.newInstance(constructorParam);    }    catch (Exception e) {      System.out.println("Error creating " + carName);    }    return newCar;  }  public void startCar(Car theCar) {    try {      // Define a zero-length Class array to represent      // the empty parameter list of the start method      Class parameters[] = new Class[0];      Class carType = theCar.getClass();      Method meth = carType.getDeclaredMethod("start", parameters);      // invoke the method on the specified car      meth.invoke(theCar,parameters);    }    catch (Exception e) {      System.out.println("Error starting car: " + e);    }  }  // supply names of car types as command line arguments  public static void main( String args[] ) {    // create a car shop that contains the specified car types    CarShop shop = new CarShop( args );    for ( int i=0; i<shop.carList.length; i++ ) {      System.out.println( shop.carList[i] );    }  }}

⌨️ 快捷键说明

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