Java基础学习day06-作业
案例一
Card类
package com.itheima.homework_day06.card; public class Card { //车牌号码 private String id; // 车主姓名 private String name; // 电话号码 private String phone; // 余额 private double balance; // 花销 private double cost; // 预存 private double deposit; public Card() { } public Card(String id, String name, String phone, double balance, double cost, double deposit) { this.id = id; this.name = name; this.phone = phone; this.balance = balance; this.cost = cost; this.deposit = deposit; } /** * 获取 * * @return id */ public String getId() { return id; } /** * 设置 * * @param id */ public void setId(String id) { this.id = id; } /** * 获取 * * @return name */ public String getName() { return name; } /** * 设置 * * @param name */ public void setName(String name) { this.name = name; } /** * 获取 * * @return phone */ public String getPhone() { return phone; } /** * 设置 * * @param phone */ public void setPhone(String phone) { this.phone = phone; } /** * 获取 * * @return balance */ public double getBalance() { return balance; } /** * 设置 * * @param balance */ public void setBalance(double balance) { this.balance = balance; } /** * 获取 * * @return cost */ public double getCost() { return cost; } /** * 设置 * * @param cost */ public void setCost(double cost) { this.cost = cost; } /** * 获取 * * @return deposit */ public double getDeposit() { return deposit; } /** * 设置 * * @param deposit */ public void setDeposit(double deposit) { this.deposit = deposit; } public void pay() { setBalance(getBalance() - getCost()); System.out.println("您本次消费" + getCost() + "元,当前的余额为" + getBalance() + "元"); } public void predeposit() { setBalance(getBalance() + getDeposit()); System.out.println("您本次预存" + getDeposit() + "元,当前余额为" + getBalance() + "元"); } public String toString() { return "Card{id = " + id + ", name = " + name + ", phone = " + phone + ", balance = " + balance + ", cost = " + cost + ", deposit = " + deposit + "}"; } }
GoldenCard类
package com.itheima.homework_day06.card; public class GoldenCard extends Card { //金卡办理时入存金额必须>=5000元,金卡支付时享受8折优惠,金卡消费满200元可以提供打印免费洗车票的服务。 public void predeposit() { if (getDeposit() >= 5000) { setBalance(getBalance() + getDeposit()); System.out.println("尊敬的车牌号为" + getId() + "的金卡用户,您本次预存" + getDeposit() + "元,当前余额为" + getBalance() + "元"); } else { System.out.println("尊敬的车牌号为" + getId() + "的金卡用户,您本次预存" + getDeposit() + "元,低于最低预存金额5000元,请重新预存"); } } public void pay() { double discountCost = getCost() * 0.8; setBalance(getBalance() - discountCost); if (discountCost >= 200) { System.out.println("尊敬的车牌号为" + getId() + "的金卡用户,您本次消费" + getCost() + "元,打8折后扣款" + discountCost + "元,当前余额为:" + getBalance() + "元,因本次消费满200元,可免费打印免费洗车票,欢迎下次光临"); } else { System.out.println("尊敬的车牌号为" + getId() + "的金卡用户,您本次消费" + getCost() + "元,打8折后扣款" + discountCost + "元,当前余额为:" + getBalance() + "元,欢迎下次光临"); } } }
SilverCard类
package com.itheima.homework_day06.card; public class SilverCard extends Card { //银卡办理时预存金额必须>=2000元,银卡支付时享受9折优惠。 public void predeposit() { if (getDeposit() >= 2000) { setBalance(getBalance() + getDeposit()); System.out.println("尊敬的车牌号为" + getId() + "的银卡用户,您本次预存" + getDeposit() + "元,当前余额为" + getBalance() + "元"); } else { System.out.println("尊敬的车牌号为" + getId() + "的银卡用户,您本次预存" + getDeposit() + "元,低于最低预存金额2000元,请重新预存"); } } public void pay() { double discountCost = getCost() * 0.9; setBalance(getBalance() - discountCost); System.out.println("尊敬的车牌号为" + getId() + "的银卡用户,您本次消费" + getCost() + "元,打9折后扣款" + discountCost + "元,当前余额为:" + getBalance() + "元,欢迎下次光临"); } }
Test类
package com.itheima.homework_day06.card; public class Test { public static void main(String[] args) { //某加油站为了吸引更多的车主,推出了如下活动,车主可以办理金卡和银卡。 //卡片信息包括:车牌号码、车主姓名、电话号码、卡片余额。 //金卡办理时入存金额必须>=5000元,金卡支付时享受8折优惠,金卡消费满200元可以提供打印免费洗车票的服务。 //银卡办理时预存金额必须>=2000元,银卡支付时享受9折优惠。 GoldenCard g1 = new GoldenCard(); g1.setId("粤A123456"); g1.setName("张三"); g1.setPhone("12345678910"); g1.setBalance(0); g1.setDeposit(4000); g1.predeposit(); g1.setDeposit(5000); g1.predeposit(); g1.setCost(300); g1.pay(); g1.setCost(100); g1.pay(); System.out.println("===============================================================================贫富分割线=================================================================="); SilverCard s1 = new SilverCard(); s1.setId("粤B123456"); s1.setName("李四"); s1.setPhone("10987654321"); s1.setBalance(0); s1.setDeposit(1000); s1.predeposit(); s1.setDeposit(2000); s1.predeposit(); s1.setCost(300); s1.pay(); } }
案例二
Shape类
package com.itheima.homework_day06.shape; public class Shape { // 案例: 继承 // 基类: // Shape类有一个属性color(字符串)和一个计算面积的方法area()(返回0.0),以及一个构造方法用于设置颜色。 // 属性 private String color; // 无参构造 public Shape() { } // 全参构造 public Shape(String color) { this.color = color; } // set/get方法 public void setColor(String color) { this.color = color; } public String getColor() { return color; } // area方法 public double area() { return 0.0; } }
Circle类
package com.itheima.homework_day06.shape; public class Circle extends Shape { // Circle类继承自Shape,增加一个属性radius(double),重写area方法计算圆的面积(使用Math.PI)。 // 属性 private double radius; public Circle() { } public Circle(double radius) { this.radius = radius; } // 重写area方法 public double area() { return Math.PI * radius * radius; } // set/get方法 public void setRadius(double radius) { this.radius = radius; } public double getRadius() { return radius; } }
Rectangle类
package com.itheima.homework_day06.shape; public class Rectangle extends Shape { // Rectangle类继承自Shape,增加两个属性width和height(double),重写area方法计算矩形面积。 // 属性 private double width; private double height; public Rectangle() { } public Rectangle(double width, double height) { this.width = width; this.height = height; } // 重写area方法 public double area() { return width * height; } public void setWidth(double width) { this.width = width; } public double getWidth() { return width; } public void setHeight(double height) { this.height = height; } public double getHeight() { return height; } }
Test类
package com.itheima.homework_day06.shape; public class Test { public static void main(String[] args) { // 编写一个测试类,创建一个Circle对象和一个Rectangle对象, // 设置各自的属性,然后调用它们的area方法并打印结果。 Circle c1 = new Circle(); c1.setColor("黄色"); c1.setRadius(2.0); System.out.println("半径为:" + c1.getRadius() + "的圆形面积为:" + c1.area()); Rectangle r1 = new Rectangle(); r1.setColor("蓝色"); r1.setWidth(3.3); r1.setHeight(4.4); System.out.println("长为:" + r1.getHeight() + ",宽为:" + r1.getWidth() + "的矩形面积为:" + r1.area()); System.out.println("--------案列二-----------"); // 案例二: //1. 在测试类中,创建一个Shape类型的数组,数组元素包括一个Circle对象和一个Rectangle对象。 //2. 遍历数组,对每个元素调用area方法,并打印结果和该图形的颜色。 //3. 解释多态在此处的体现:同一个方法调用(area)在不同对象上表现出不同的行为(分别计算圆和矩形的面积)。 Circle c = new Circle(); c.setRadius(2.0); c.setColor("红色"); Rectangle r = new Rectangle(); r.setWidth(1.0); r.setHeight(2.0); r.setColor("绿色"); Shape[] sh = new Shape[]{c, r}; for (int i = 0; i < sh.length; i++) { System.out.println(sh[i].getColor()); System.out.println(sh[i].area()); } } }