在一个ArrayList中为每个对象打印不同的单词。

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

Printing a different word for each object in an ArrayList

问题

我有多个Client对象。每个client对象都有一个名为shoppingCart的ArrayList。这些ArrayList由我创建的Product类的对象填充。这些产品可以是Shirt、Jeans或Skirt类(都继承自Product类)。
我想将每个Client购物车中的物品以字符串形式打印出来。例如,如果一个Client的shoppingCart中有一个Shirt对象和一个Skirt对象,控制台将打印:“Contents of cart: Shirt, Skirt”。
我该如何实现这一目标?

英文:

I have multiple Client objects. Each client object has an ArrayList called shoppingCart. These ArrayLists are populated by objects of the class Product, made by me. These products can be of class Shirt, Jeans or Skirt (all inherit Product).
I want to print what each Client has on his shoppingCart as Strings. For example, if a Client has a Shirt and a skirt object in his shoppingCart, the console would print: "Contents of cart: Shirt, Skirt"
How can i accomplish this?

答案1

得分: 1

public enum ProductType {
    PANT,
    SHIRT,
    SKIRT,
    TSHIRT,
}

public class Product {
    private ProductType productType;

    public Product(ProductType productType) {
        this.productType = productType;
    }

    public ProductType getProductType() {
        return productType;
    }
}

public class Pant extends Product {
    private int size;

    public Pant(ProductType productType, int size) {
        super(productType);
        this.size = size;
    }
}

public class Shirt extends Product {
    private int size;

    public Shirt(ProductType productType, int size) {
        super(productType);
        this.size = size;
    }
}

public class App {
    public static void main(String[] args) {
        List<Product> cart = List.of(new Pant(ProductType.PANT, 100),
                new Pant(ProductType.PANT, 101),
                new Shirt(ProductType.SHIRT, 42));

        System.out.println("Contents of cart: " +
                cart.stream()
                .map(Product::getProductType)
                .collect(Collectors.toList()));

    }
}

输出:

Contents of cart: [PANT, PANT, SHIRT]
英文:

Sample Code:

public enum ProductType {
PANT,
SHIRT,
SKIRT,
TSHIRT,
}
public class Product {
private ProductType productType;
public Product( ProductType productType) {
this.productType = productType;
}
public ProductType getProductType() {
return productType;
}
}
public class Pant extends Product {
private int size;
public Pant(ProductType productType, int size) {
super(productType);
this.size = size;
}
}
public class Shirt extends Product {
private int size;
public Shirt(ProductType productType, int size) {
super(productType);
this.size = size;
}
}
public class App {
public static void main(String[] args) {
List&lt;Product&gt; cart = List.of(new Pant(ProductType.PANT, 100),
new Pant(ProductType.PANT, 101),
new Shirt(ProductType.SHIRT, 42));
System.out.println(&quot;Contents of cart:  &quot; +
cart.stream()
.map(Product::getProductType)
.collect(Collectors.toList()));
}
}

Output:

Contents of cart:  [PANT, PANT, SHIRT]
</details>
# 答案2
**得分**: 0
你可以像这样做。一个***多态性***的示例:
```java
abstract class Product{
abstract String getType();
}
class Shirt extends Product {
String getType() {
return "Shirt";
}
}
class Skirt extends Product {
String getType() {
return "Skirt";
}
}

当你遍历你的shoppingCart并打印类型时,你会得到相应的类型。

for(Product p : shoppingCart) {
  System.out.println(p.getType);
}
英文:

You could do something like this. An example of Polymorphism:

abstract class Product{
abstract String getType();
}
class Shirt extends Product {
String getType() {
return &quot;Shirt&quot;;
}
}
class Skirt extends Product {
String getType() {
return &quot;Skirt&quot;;
}
}

When you iterate over your shoppingCart and print the type you get the corresponding type.

for(Product p : shoppingCart) {
System.out.println(p.getType);
}

答案3

得分: 0

我认为可以通过使用 instanceof 运算符来实现。你可以尝试像这样操作:

public List<String> getContentsOfCart(List<Product> products) {
  List<String> result = new ArrayList<>();
  for (Product p : products) {
    if (p instanceof Skirt) {
      result.add("Skirt");
    } else if (p instanceof Shirt) {
      result.add("Shirt");
    } else if (p instanceof Jeans) {
      result.add("Jeans");
    }
  }
  return result;
}

然后你可以像这样打印这个列表:

System.out.println("购物车内容:" + Strings.join(result, ","));
英文:

I think this can achieved by using instanceof operator. You can try doing something like this:

public List&lt;String&gt; getContentsOfCart(List&lt;Product&gt; products) {
  List&lt;String&gt; result = new ArrayList&lt;&gt;();
  for (Product p : products) {
    if (p instanceof Skirt) {
      result.add(&quot;Skirt&quot;);
    } else if (p instanceof Shirt) {
      result.add(&quot;Shirt&quot;);
    } else if (p instancef Jeans) {
      result.add(&quot;Jeans&quot;);
    }
  }
  return result;
}

You can then print this list like this:

System.out.println(&quot;Contents of cart: &quot; + Strings.join(result, &quot;,&quot;));

huangapple
  • 本文由 发表于 2020年10月1日 01:30:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64142842.html
匿名

发表评论

匿名网友

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

确定