92 面向对象 商品(多个属性)放入3个数组中
对象
package com.fqs.goods; public class Goods { private int id; private String name; private double price; private int geShu; public Goods() { } public Goods(int id, String name, double price, int geShu) { this.id = id; this.name = name; this.price = price; this.geShu = geShu; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getGeShu() { return geShu; } public void setGeShu(int geShu) { this.geShu = geShu; } }
调用方法
package com.fqs.goods; import sun.font.FontRunIterator; public class GoodsTest { public static void main(String[] args) { Goods g1=new Goods(1,"apple",5.0,8); Goods g2=new Goods(2,"orage",9.0,3); Goods g3=new Goods(3,"rice",0.5,3); //存放3个商品信息的数组 类型是Goods Goods []arr=new Goods[3]; arr[0]=g1; arr[1]=g2; arr[2]=g3; //遍历数组 for (int i = 0; i < 3; i++) { System.out.println(arr[i].getId()+" "+arr[i].getName()+" "+arr[i].getPrice()+" "+arr[i].getGeShu()); } } }