英文:
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<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()));
}
}
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 "Shirt";
}
}
class Skirt extends Product {
String getType() {
return "Skirt";
}
}
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<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 instancef Jeans) {
result.add("Jeans");
}
}
return result;
}
You can then print this list like this:
System.out.println("Contents of cart: " + Strings.join(result, ","));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论