//小明买东西购物单
package javaday01;
/**
* @author joy
*
*/
public class BuyDemo
{
/**
* @param args
*/
public static void main(String[] args)
{
Personss p= new Personss(20,"小明");
Foods[] fs= null;
/*
Foods f1 = new Foods("牛奶",3.5);
Foods f2 = new Foods("薯片",5.5);
Foods f3 = new Foods("瓜子",8.5);
Foods f4 = new Foods("花生",4.5);
*/
fs=p.add(new Foods("牛奶",3.5),new Foods("薯片",5.5),new Foods("瓜子",8.5),new Foods("花生",4.5));
p.buy(fs);
}
}
class Personss
{
private int age;
private String name;
Personss(int age,String name)
{
this.age=age;
this.name=name;
}
public void buy(Foods[] f)//买的商品加入购物车
{
System.out.println("\t商品名称"+"\t商品价格");
for(int x=0;x<f.length;x++)
{
System.out.println("\t"+f[x].getName()+"\t"+f[x].getPrice());
}
System.out.println("\t合计"+"\t"+pay(f));
}
public Foods[] add(Foods... f)
{
return f;
}
public int pay(Foods[] payfor)
{
int sum=0;
for(int x=0;x<payfor.length;x++)
{
if(payfor[x].getPrice()!=0)
{
sum+=payfor[x].getPrice();
}
}
return sum;
}
}
class Foods
{
private String name;
private double price;
Foods(String name,double price)
{
this.name=name;
this.price=price;
}
public String getName()
{
return name;
}
public double getPrice()
{
return price;
}
}
public Foods[] add(Foods... f)知道这样写,但是不知道括号内的...的由来?为什么要这样写?
|
|