定义商品类Goods 并进行封装
如何使用封装的方法
//原创:马志勇
/*
假如 一个商品的属性为:
名称:白菜 单价:7.23 库存量:500公斤
*/
public class GoodsDemo {
public static void main(String[] args) {
String name="白菜";
double price = 7.23;
int inventories=500;
Goods s=new Goods();
s.setName(name);
s.setPrice(price);
s.setInventories(inventories);
s.show();
}
}
class Goods{
public Goods(){};
String name=null;
double price = 0.00;
int inventories=0;
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 getInventories() {
return inventories;
}
public void setInventories(int inventories) {
this.inventories = inventories;
}
public void show() {
System.out.println("名字:"+getName()+"\t价格:"+getPrice()+"\t库存量:"+getInventories());
}
}