商品类 Item;具有成员变量 编号code String类型 商品编号 名称name String 类型 商品名称 类别type String 类型 商品的具体类别 价格price double 类型 商品的价格 总商品数量totalNum int 类型 商品的总商品数量1提供两个构造器 没有参数的构造器 创建一个以编号code名称name 作为形式参数的构造器2编写一个main方
public class Item {
private String code;
private String name;
private String type;
private double price;
private static int totalNum = 0;
public Item() {
totalNum++;
}
public Item(String code, String name) {
this.code = code;
this.name = name;
totalNum++;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public static int getTotalNum() {
return totalNum;
}
public static void setTotalNum(int totalNum) {
Item.totalNum = totalNum;
}
public static void main(String[] args) {
Item computer1 = new Item("666", "戴尔(DELL) 游戏笔记本");
computer1.setType("游戏");
computer1.setPrice(5499.00);
Item computer2 = new Item("007", "苹果(Macbook pro)笔记本");
computer2.setType("电脑,办公");
computer2.setPrice(18000.00);
System.out.println("商品1名称: " + computer1.getName());
System.out.println("商品1价格: " + computer1.getPrice());
System.out.println("商品2名称: " + computer2.getName());
System.out.println("商品2价格: " + computer2.getPrice());
System.out.println("总商品数量: " + Item.getTotalNum());
}
}
``
原文地址: https://www.cveoy.top/t/topic/iiOs 著作权归作者所有。请勿转载和采集!