某种服饰,包括名称和价格两个属性,要求设计服饰类,然后在主类中申明一个包含三个对象的数组,分别给三个对象赋值,最后输出数组中的所有值
class t{
public static void main(String[]args){
Clothes []cs=new Clothes[3];
cs[0]=new Clothes("服饰1",111.11);
cs[1]=new Clothes("服饰2",222.22);
cs[2]=new Clothes("服饰3",333.33);
cs[0].print();
cs[1].print();
cs[2].print();
}
}
class Clothes {
String name;
double price;
public Clothes(String name,double price){
this.name = name;
this.price=price;
}
public void setPrice(double price){
this.price = price;
}
public void setName(String name){
this.name = name;
}
public void print(){
System.out.println("名称:"+ name +"\t价格:" + price );
}
}
public class Product{
private String name;
private float price;
public Product(){}
public Porduct(String name,float price){
this.name = name;
this.price = price;
}
public String getName(){return name;}
public void setName(String name){this.name = name;}
public float getPrice(){ return price;}
public void setPrice(float price){this.price = price;}
public void display(){
System.out.println(name +","+price);
}
}
public class Test{
public static void main(String [] args){
Product[] prod = new Product()[3];
for(int i = 0 ;i<3;i++){
prod[i] = new Product("名称“+i,88+i);
prod[i].display();
}
}
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!