英文:
how to add the objects from main class to ArrayList in other class Java
问题
我有这个类,它应该有3个包含其他类对象的列表。
现在,我该如何使用以下方法从主类中填充列表?
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>();
// 添加车辆
public String addCar(Car tcar) {
cars.add(tcar);
String state = "添加车辆成功";
return state;
}
}
注意,Car
、Customers
、Rental
都是类,它们没有问题。
这是主类。当我添加以下车辆时,应该显示消息"添加成功"。但是在我的主类中没有显示任何内容。
我在这里做错了什么?
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);
}
}
任何帮助将不胜感激。
您可以在这些图片中看到类图中所需的内容。
--
英文:
I have this class which is supposed to have 3 lists of objects of other classes.
Now, how can I fill the list from the main class by using the following method
from the 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;
}
Note that Car
, Customers
, Rental
are classes and they have no problem.
Here is the main class. When I add the following car it is supposed to show the message "added successfully". But nothing shows in my main class.
What did I do wrong here?
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);
}
}
Any help would be appreciated.
You can see in these images what is required with the class diagram.
--
答案1
得分: 2
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()));
}
英文:
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()));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论