如何将主类中的对象添加到另一个类中的ArrayList中,Java。

huangapple go评论95阅读模式
英文:

how to add the objects from main class to ArrayList in other class Java

问题

我有这个类,它应该有3个包含其他类对象的列表。
现在,我该如何使用以下方法从主类中填充列表?

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class FcomCarRentalSystem {
  4. public List<Customer> customers = new ArrayList<Customer>();
  5. public List<Car> cars = new ArrayList<Car>();
  6. public List<Rental> rental = new ArrayList<Rental>();
  7. // 添加车辆
  8. public String addCar(Car tcar) {
  9. cars.add(tcar);
  10. String state = "添加车辆成功";
  11. return state;
  12. }
  13. }

注意,CarCustomersRental 都是类,它们没有问题。
这是主类。当我添加以下车辆时,应该显示消息"添加成功"。但是在我的主类中没有显示任何内容。
我在这里做错了什么?

  1. public class App {
  2. public static void main(String[] args) {
  3. FcomCarRentalSystem company = new FcomCarRentalSystem();
  4. Car nii1 = new Car("QA33145", "Nissan", CarType.SEDAN, true);
  5. company.addCar(nii1);
  6. }
  7. }

任何帮助将不胜感激。
您可以在这些图片中看到类图中所需的内容。

--
如何将主类中的对象添加到另一个类中的ArrayList中,Java。

如何将主类中的对象添加到另一个类中的ArrayList中,Java。

英文:

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?

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class FcomCarRentalSystem {
  4. public List&lt;Customer&gt; customers = new ArrayList&lt;Customer&gt;();
  5. public List&lt;Car&gt; cars = new ArrayList&lt;Car&gt;();
  6. public List&lt;Rental&gt; rental = new ArrayList&lt;Rental&gt;();
  7. // Add Car
  8. public String addCar(Car tcar) {
  9. cars.add(tcar);
  10. String state = &quot;added car successfully&quot;;
  11. return state;
  12. }

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?

  1. public class App {
  2. public static void main(String[] args) {
  3. FcomCarRentalSystem company = new FcomCarRentalSystem ();
  4. Car nii1 = new Car(&quot;QA33145&quot;,&quot;Nissan&quot;,CarType.SEDAN,true);
  5. company.addCar(nii1);
  6. }
  7. }

Any help would be appreciated.
You can see in these images what is required with the class diagram.

--
如何将主类中的对象添加到另一个类中的ArrayList中,Java。

如何将主类中的对象添加到另一个类中的ArrayList中,Java。

答案1

得分: 2

  1. public static void main(String[] args) {
  2. FcomCarRentalSystem company = new FcomCarRentalSystem ();
  3. System.out.println(company.addCar(new Car()));
  4. System.out.println(company.addCustomer(new Customer()));
  5. System.out.println(company.addRental(new Rental()));
  6. }
英文:
  1. public static void main(String[] args) {
  2. FcomCarRentalSystem company = new FcomCarRentalSystem ();
  3. System.out.println(company.addCar(new Car()));
  4. System.out.println(company.addCustomer(new Customer()));
  5. System.out.println(company.addRental(new Rental()));
  6. }

huangapple
  • 本文由 发表于 2020年10月23日 17:58:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64497852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定