通过数组列表设置对象属性

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

Setting object attributes via a array list

问题

基本上,我正在处理一个项目,其中某些类属性和类的名称存储在一个文本文件中。这里的目标是在文本文件中创建一个特定数据类型(在这种情况下是Car)的对象列表(已经完成),然后将这些对象分配给文本文件中的数据类型。以下是我要处理的文本文件示例:


Car: 2

4 1 1 Red 3 80.5 20 60 2 aadawd

1 3 2 Blue 3 80 30 20 1 aaxzd

Bike: 3

2 1

2 2

2 3


基本上,'Car: 2' 表示我们有2辆汽车。其下面的两行表示各自汽车的属性。为了不浪费时间在不必要的信息上,我只会列出 Car 下面的行的数据类型:<int, int, int, String, int, double, double, double, int, char[]>

已经为 Car 类建立了获取器/设置器。

到目前为止,我已将文件转换为名为 list 的 ArrayList,文件的每一行表示数组的一个元素。这使我能够轻松地创建一个 Car 类型的 ArrayList。我现在所需的就是设置 Car 的属性。有什么想法吗?

            for (int i = 0; i < list.size(); i++) {


                if (list.get(i).contains("Red")) {

                    

                    carList.add(new RedCar());
                    
                    

                } else if (list.get(i).contains("Blue")) {

                    

                    carList.add(new BlueCar());
                    

                }  else if (list.get(i).contains("Yellow")) {


                    carList.add(new YellowCar());
                    

                }

            }





英文:

Basically I am working on a project where certain class attributes and names of classes are stored in a text file. The goal here is to create a list of the objects of a certain data type (in this case Car) listed in the text file (of which I have already done), and then assign these to the data types within the text file. Below is an example of a text file that I would work with:


Car: 2

4 1 1 Red 3 80.5 20 60 2 aadawd

1 3 2 Blue 3 80 30 20 1 aaxzd

Bike: 3

2 1

2 2

2 3


Basically, 'Car: 2' indicates that we have 2 cars. The two lines below it indicate the attributes of the respective cars. To not waste time on unnecessary information I'll just list the data types of the lines below Car: <int, int, int, String, int, double, double, double, int, char[]>

Getters/setters are already established for the Car classes.

So far, I converted the file into an ArrayList called list, where each line of the file represents is an element of the array. This allowed me to create an ArrayList of type Car quite easily. All I need now is to set the attributes of the Car. Any thoughts?

            for (int i = 0; i &lt; list.size(); i++) {


                if (list.get(i).contains(&quot;Red&quot;)) {

                    

                    carList.add(new RedCar());
                    
                    

                } else if (list.get(i).contains(&quot;Blue&quot;)) {

                    

                    carList.add(new BlueCar());
                    

                }  else if (list.get(i).contains(&quot;Yellow&quot;)) {


                    carList.add(new YellowCar());
                    

                }

            }





答案1

得分: 0

就我所见,你拥有一切所需。要想将你的carlist填充满属性,你需要创建一个对象并将其放入一个变量中,然后再将其推送到一个列表中。

...
if (list.get(i).contains("Red")) {
  //希望所有的汽车都继承自Car
  Car c = new RedCar();
  final String[] attributes = list.get(i).split(" ");
  c.setFirstAttr(attributes[0]);
  c.setSecondAttr(attributes[1]);
  //以此类推
  carList.add(c);
}

用汽车的颜色作为属性可能是一个更好的方法。从这个角度来看,汽车看起来都是一样的,除了它们的颜色。

//你不必决定创建哪种类型的汽车
for (int i = 0; i < list.size(); i++) {
  //希望所有的汽车都继承自Car
  Car c = new Car();
  final String[] attributes = list.get(i).split(" ");
  c.setFirstAttr(attributes[0]);
  //以此类推
  c.setColor(attributes[3]);
  carList.add(c);
}
英文:

As far as I can see, you have everything you need. To fill your carlist with properties you have to create an object and put it on a variable before pushing it to a list.

...
if (list.get(i).contains(&quot;Red&quot;)) {
  //hopefully all cars inherts from Car
  Car c = new RedCar();
  final String[] attributes = list.get(i).split(&quot; &quot;);
  c.setFirstAttr(attributes[0]);
  c.setSecondAttr(attributes[1]);
  //and so on
  carList.add(c);
}

It could be a better way to use the color of the car as an attribute. Because from this perspective it looks like that the cars are all the same, except their color.

//you dont have to decide which type if car you create
for (int i = 0; i &lt; list.size(); i++) {
  //hopefully all cars inherts from Car
  Car c = new Car();
  final String[] attributes = list.get(i).split(&quot; &quot;);
  c.setFirstAttr(attributes[0]);
  //and so on
  c.setColor(attributes[3]);
  carList.add(c);
}

huangapple
  • 本文由 发表于 2020年9月9日 15:04:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63806379.html
匿名

发表评论

匿名网友

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

确定