我有这个类,它应该有 3 个其他类的对象列表。
现在,如何使用以下方法从主类填充列表
来自类(class)?
import java.util.ArrayList;
import java.util.List;
public class FcomCarRentalSystem {
public List<Customer> customers = new ArrayList<Customer>();
public List<Car> cars = new ArrayList<Car>();
public List<Rental> rental = new ArrayList<Rental>();
// Add Car
public String addCar(Car tcar) {
cars.add(tcar);
String state = "“added car successfully”";
return state;
}
请注意,Car
、Customers
、Rental
都是类,它们没有问题。
这是主要类(class)。当我添加以下汽车时,它应该显示“添加成功”消息。但我的主课上没有显示任何内容。
我在这里做错了什么?
public class App {
public static void main(String[] args) {
FcomCarRentalSystem company = new FcomCarRentalSystem ();
Car nii1 = new Car("QA33145","Nissan",CarType.SEDAN,true);
company.addCar(nii1);
}
}
如有任何帮助,我们将不胜感激。
您可以在这些图像中看到类图所需的内容。
--
最佳答案
public static void main(String[] args) {
FcomCarRentalSystem company = new FcomCarRentalSystem ();
System.out.println(company.addCar(new Car()));
System.out.println(company.addCustomer(new Customer()));
System.out.println(company.addRental(new Rental()));
}
关于java - 如何将主类中的对象添加到其他Java类中的ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64497852/