使用构造函数 创建3个商品 放到数组中
类
package com.fqs.demo1; public class Shop { private String id; private String name; private double price; private int kucun; //空参 public Shop() { } //全部参数 public Shop(String id,String name,Double price,int Kucun) { this.id=id; this.name=name; this.price=price; this.kucun=kucun; } //set get id public void setId(String id) { this.id=id; } public String getId() { return id; } //set get name public void setName(String name) { this.name=name; } public String name() { return name; } //set get price public void setPrice(Double price) { this.price=price; } public double getPrice() { return price; } //set get kucun public void setKucun(int kucun) { this.kucun=kucun; } public int getKucun() { return kucun; } }
对象
package com.fqs.demo1; public class ShopTest { public static void main(String[]args) { Shop [] arr= new Shop[3];//类类型的数组,允许接收不同类型的数组 Shop s1=new Shop("shop001","app",3.5,1); Shop s2=new Shop("shop002","orange",3.5,1); Shop s3=new Shop("shop003","milk",3.5,1); arr[0]=s1; arr[1]=s2; arr[2]=s3; //遍历 for(int i=0;i<arr.length;i++) { System.out.println(arr[i].getId()+"," +arr[i].name()+","+arr[i].getPrice()+","+arr[i].getKucun()); } /* System.out.println(s1.getId()); */ } }