/** * 进货 * @param className 赢料名 * @param amount 数量 * @throws NoSuchMethodException * @throws InvocationTargetException * @throws InstantiationException * @throws IllegalAccessException */ public void order(String className , int amount) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ClassNotFoundException { //在此补充代码 }
时间: 2024-02-07 12:03:27 浏览: 61
mybatis-demo9-方法多参数@Param.zip
可以使用反射机制来实现进货方法,代码如下:
```
/**
* 进货
* @param className 饮料名
* @param amount 数量
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public void order(String className , int amount) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ClassNotFoundException {
Class<?> clazz = Class.forName(className);
Constructor<?> constructor = clazz.getConstructor();
for(int i = 0; i < amount; i++){
repository[quantity++] = (Beverage)constructor.newInstance();
}
}
```
阅读全文