public class CustomerDemo { class Customer { private String cardID; private String name; private double cost = 0; private String address; private String email; private double discount = 1; public Customer(String id,String name,String add,String email) { cardID = id; this.name = name; address = add; this.email=email; } public void buy(double cost) { this.cost +=cost; } public void setDiscount() { if (cost>2000.00) discount -= 0.2; else if (cost>1000.00) discount -=0.1; } public String getAddress() { return address; } public void setAddress(String address) { this.address=address; } public String getCardID() { return cardID; } public void setCardID(String cardID) { this.cardID=cardID; } public double getCost() { return cost; } public double getDiscount() { return discount; } public String getEmail() { return email; } public void setEmail(String email) { this.email=email; } public String getName() { return name; } public void setName(String name) { this.name=name; } } Customer customer[] = new Customer[3]; public CustomerDemo() { customer[0] = new Customer("001","wang ziyu","BeiJing","wangziyu@qq.com"); customer[1] = new Customer("002","Zhang zhiyi","ShangHai","Zhangzhiyi@qq.com"); customer[2] = new Customer("003","Li yanxi","BeiJing","Liyanxi@qq.com"); customer[0].buy(2600.00); customer[0].setDiscount(); customer[1].buy(1579.00); customer[1].setDiscount(); customer[2].buy(880.00); customer[2].setDiscount(); for(int i=0; i < customer.length; i++) { System.out.println("customer["+ i +"]"); System.out.println("cardID:" + customer[i].getCardID()); System.out.println("name:" + customer[i].getName()); System.out.println("cost:" + customer[i].getCost()); System.out.println("dico其中重要变量的定义和作用
时间: 2024-03-14 11:49:20 浏览: 22
这段代码定义了一个名为CustomerDemo的Java类,其中包含了一个名为Customer的内部类。Customer类有五个私有属性:卡号(cardID)、姓名(name)、花费(cost)、地址(address)和电子邮件(email),以及一个折扣(discount)属性,初始值为1。Customer类有一个构造方法,用于初始化卡号、姓名、地址和电子邮件属性。它还有一个buy方法,用于记录花费,并有一个setDiscount方法,根据花费来更新折扣。除此之外,它还有一些getter和setter方法,用于获取和设置属性值。
在CustomerDemo类中,定义了一个包含三个元素的Customer数组,分别用于存储三个客户的信息。随后,分别在每个客户对象上调用buy和setDiscount方法,并输出每个客户的卡号、姓名、花费和折扣。
相关问题
private ushort _CardID = 0;
这段代码定义了一个名为_CardID的私有字段(private field),它的类型是ushort,即无符号的16位整数类型。这个字段的初始值为0。
private关键字表示这个字段只能在当前类的内部访问,其他类无法访问它。这种封装的方式可以保证字段的访问安全性和可靠性,同时还可以隐藏实现细节,避免对外部造成影响。通常情况下,我们会将字段尽可能地封装起来,并提供公共的属性(public property)来访问它们。
在这个例子中,_CardID字段可能是一个与运动控制卡相关的标识符,用于唯一地标识一个运动控制卡。在程序中,可能需要通过访问_CardID字段来对运动控制卡进行初始化、配置和控制等操作。
public class Card implements Serializable { /** * 与数据库的cardid映射 */ private String cardId; /** * =与数据库的memberid映射 */ private String memberId; private Integer balance; private Integer integral; private Integer lose; private static final long serialVersionUID = 1L; public String getcardId() { return cardId; } public void setcardId(String cardId) { this.cardId = cardId == null ? null : cardId.trim(); } public String getmemberId() { return memberId; } public void setmemberId(String memberId) { this.memberId = memberId == null ? null : memberId.trim(); } public Integer getBalance() { return balance; } public void setBalance(Integer balance) { this.balance = balance; } public Integer getIntegral() { return integral; } public void setIntegral(Integer integral) { this.integral = integral; } public Integer getLose() { return lose; } public void setLose(Integer lose) { this.lose = lose; }
这是一个 Java 类,名为 Card,实现了 Serializable 接口。该类映射了一个数据库表,包含了该表的各个字段以及对应的 getter 和 setter 方法。具体来说,该类包含以下字段:
- cardId:与数据库表中的 cardid 字段映射,为 String 类型;
- memberId:与数据库表中的 memberid 字段映射,为 String 类型;
- balance:映射了余额字段,为 Integer 类型;
- integral:映射了积分字段,为 Integer 类型;
- lose:映射了挂失字段,为 Integer 类型。
其中,cardId 和 memberId 的 getter 和 setter 方法分别为 getcardId/setcardId 和 getmemberId/setmemberId,名称有点特殊,需要注意一下。该类的 serialVersionUID 字段用于序列化和反序列化对象时的版本控制。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)