java 中自定义类的概述

时间:2022-10-06 16:05:48
作业:  描述商品类  Goods        4个属性  商品名字    大小   价格  库存
        把商品类放进集合中
        小米品牌  大小  价格  库存的数量  都存集合
    华为。。。。。
    魅族

public class Goods{
    String name;
    double size;
    double price;
    int count;
}





import java.util.ArrayList;
public class TextGoods{
    public static void main(String[] args){
        Goods g=new Goods();
        g.name="小米";
        g.size=5.1;
        g.price=3999.9;
        g.count=12;
        
        Goods go=new Goods();
        go.name="华为";
        go.size=5.6;
        go.price=5999.9;
        go.count=29;
        
        Goods goo=new Goods();
        goo.name="魅族";
        goo.size=6.1;
        goo.price=7999.9;
        goo.count=19;
        
        Goods good=new Goods();
        good.name="iPon";
        good.size=4.7;
        good.price=3999.9;
        good.count=82;
        /*
        System.out.println(g.name+"     "+g.size+"      "+g.price+"      "+g.count);
        System.out.println(go.name+"     "+go.size+"      "+go.price+"      "+go.count);
        System.out.println(goo.name+"     "+goo.size+"      "+goo.price+"      "+goo.count);
        System.out.println(good.name+"     "+good.size+"      "+good.price+"      "+good.count);
        */
        ArrayList<Goods> array=new ArrayList<Goods>();
        array.add(g);
        array.add(go);
        array.add(goo);
        array.add(good);
        for(int i=0;i<array.size();i++){
            System.out.println(array.get(i).name+"     "+array.get(i).size+"     "
            +array.get(i).price+"     "+array.get(i).count);
        }
    }
    
}