Product.java
public class Product {
private final String name;
private final int weight;
public Product(String name, int weight) {
super();
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public int getWeight() {
return weight;
}
@Override
public String toString() {
return "Product [name=" + name + ", weight=" + weight + "]";
}
}
TheArrayProlem.java
public class TheArrayProlem {
public static void main(String[] args) {
// TODO Auto-generated method stub
Product door =new Product("Wooden Door", 35);
Product floorPanel=new Product("Floor Panel", 25);
//Creat
//Print
//Add
//Dupicate
}
}
TheArryProblem1.java
import java.util.Arrays;
public class TheArrayProblem {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Product door =new Product("Wooden Door", 35);
Product floorPanel=new Product("Floor Panel", 25);
//Creat
//Print
//Add
//Dupicate
}
private static Product[] add(Product product,Product[] array){
int length=array.length;
Product[] newArray=Arrays.copyOf(array, length+1);
newArray[length]=product;
return newArray;
}
}
TheArrayProblem2.java
import java.util.Arrays;
public class TheArrayProblem {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Product door =new Product("Wooden Door", 35);
Product floorPanel=new Product("Floor Panel", 25);
//Creat
Product[] products={door,floorPanel};
System.out.println(Arrays.toString(products));
//Print
//Add
//Dupicate
}
private static Product[] add(Product product,Product[] array)
{
int length=array.length;
Product[] newArray=Arrays.copyOf(array, length+1);
newArray[length]=product;
return newArray;
}
}
本文探讨了Java中产品类的设计,包括属性定义、构造方法及toString()方法的重写。同时,深入讲解了如何使用数组进行产品实例的创建、打印、添加及复制,并提供了具体的代码实现。
1100

被折叠的 条评论
为什么被折叠?



