今天学习了继承、多态

创建厂家实例用明确的子类 创建用户实例用父类
▼text复制代码public class Day37SmartHomeQuotationTest { public static void main(String[] args) { // 厂家 Day37Company vendor = new Day37Company("张经理","13829871635","视声智能","广州市黄埔区","KNX全屋智能/灯光/安防"); // 业主 Day37Person client1 = new Day37Customer("马思纯","15138473848","上海市静安区",533); // 商家 Day37Person client2 = new Day37Company("李经理","13150432864","杜亚窗帘","杭州市拱墅区","智能窗帘/智能门锁"); // 报价单:厂家给业主 Day37SmartHomeQuotation quote1 = new Day37SmartHomeQuotation( "Q-SM-20260001-000001", "2026-10-02 16:35:11", "马女士静安府全屋智能", client1, vendor, 35800.0, 5800.0, 6500.0, 4200.0, 2000.0 ); // 报价单:厂家给商家 Day37SmartHomeQuotation quote2 = new Day37SmartHomeQuotation( "Q-SM-20260001-000002", "2026-10-02 16:37:20", "杜亚展厅智能改造", client2, vendor, 28000.0, 3500.0, 4000.0, 2500.0, 1500.0 ); // 打印报价单 System.out.println(quote1); System.out.println(); System.out.println(quote2); // 打印完整客户信息 System.out.println(); System.out.println("======= 完整客户信息(点击查看)======="); System.out.println(quote1.getClientFullInfo()); System.out.println(quote2.getClientFullInfo()); } }
instanceof 是运行时类型判断 父类引用 Person 装子类对象后,只能调用父类定义的方法 想调子类特有方法 → instanceof 判断 → (子类) 引用 强转 → 调用 LocalDATeTime日期输出 DateTimeFormatter格式化输出 类字段引用了子类 文本块输出 String.format()格式化输出 StringBuilder动态长度格式化输出 toString()重写
▼text复制代码import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Day37SmartHomeQuotation { private String quotationNo = ""; private String quotationDate = ""; private String projectName = ""; private Day37Person client; private Day37Company vendor; private double deviceCost = 0.0; private double materialCost = 0.0; private double installCost = 0.0; private double debugCost = 0.0; private double otherCost = 0.0; private static final String QUOTATION_TITLE = "智能家居报价单"; private static final String QUOTATION_TITLE_LINE = "----------"; private static final String QUOTATION_PROJECT_TEXT = """ 报价编号:%s 报价日期:%s 报价单位:%s 项目名称:%s 用户信息:%s """; private static final DateTimeFormatter DATE_TIME_INPUT_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); private static final DateTimeFormatter DATE_TIME_OUTPUT_FORMAT = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss"); public Day37SmartHomeQuotation() { } public Day37SmartHomeQuotation(String quotationNo, String quotationDate, String projectName, Day37Person client, Day37Company vendor, double deviceCost, double materialCost, double installCost, double debugCost, double otherCost) { this.setQuotationNo(quotationNo); this.setQuotationDate(quotationDate); this.setProjectName(projectName); this.setClient(client); this.setVendor(vendor); this.setDeviceCost(deviceCost); this.setMaterialCost(materialCost); this.setInstallCost(installCost); this.setDebugCost(debugCost); this.setOtherCost(otherCost); } public void setQuotationNo(String quotationNo) { this.quotationNo = quotationNo; } public void setQuotationDate(String quotationDate) { this.quotationDate = quotationDate; } public void setProjectName(String projectName) { this.projectName = projectName; } public void setClient(Day37Person client) { this.client = client; } public void setVendor(Day37Company vendor) { this.vendor = vendor; } public void setDeviceCost(double deviceCost) { this.deviceCost = deviceCost; } public void setMaterialCost(double materialCost) { this.materialCost = materialCost; } public void setInstallCost(double installCost) { this.installCost = installCost; } public void setDebugCost(double debugCost) { this.debugCost = debugCost; } public void setOtherCost(double otherCost) { this.otherCost = otherCost; } public String getQuotationNo() { return this.quotationNo; } public String getQuotationDate() { LocalDateTime dateTime = LocalDateTime.parse(this.quotationDate, DATE_TIME_INPUT_FORMAT); return dateTime.format(DATE_TIME_OUTPUT_FORMAT); } public String getProjectName() { return this.projectName; } public Day37Person getClient() { return this.client; } public Day37Company getVendor() { return this.vendor; } public double getDeviceCost() { return this.deviceCost; } public double getMaterialCost() { return this.materialCost; } public double getInstallCost() { return this.installCost; } public double getDebugCost() { return this.debugCost; } public double getOtherCost() { return this.otherCost; } public String getClientDisplay() { if (client instanceof Day37Customer) { Day37Customer cu = (Day37Customer) client; return "(业主)" + cu.getName(); } else if (client instanceof Day37Company) { Day37Company co = (Day37Company) client; return "(公司)" + co.getCompanyName(); } return ""; } public String getClientFullInfo() { if (client instanceof Day37Customer) { Day37Customer cu = (Day37Customer) client; return "(业主)" + "电话:" + cu.getPhone() + ",地址:" + cu.getHouseAddress() + ",房屋面积:" + cu.getHouseArea() + "m²"; } else if (client instanceof Day37Company) { Day37Company co = (Day37Company) client; return "(公司)" + "联系人:" + co.getName() + ",电话:" + co.getPhone() + ",地址:" + co.getAddress(); } return ""; } @Override public String toString() { double totalAmount = deviceCost + materialCost + installCost + debugCost + otherCost; StringBuilder sb = new StringBuilder(); // 静态拼接报价单的头 sb.append(QUOTATION_TITLE_LINE.repeat(4)).append(QUOTATION_TITLE).append(QUOTATION_TITLE_LINE.repeat(4)).append("\n"); sb.append(String.format(QUOTATION_PROJECT_TEXT, quotationNo, getQuotationDate(), vendor.getCompanyName(), projectName, getClientDisplay())); sb.append(QUOTATION_TITLE_LINE.repeat(9)).append("\n"); // 动态拼接报价单金额(按照总金额格式化后的长度右对齐) String totalStr = String.format("%,.2f", totalAmount); int width = totalStr.length(); sb.append(String.format("项目金额:%," + width + ".2f" + " 元%n", totalAmount)); sb.append("其中:\n"); sb.append(String.format("设备金额:%," + width + ".2f" + " 元%n", deviceCost)); sb.append(String.format("材料金额:%," + width + ".2f" + " 元%n", materialCost)); sb.append(String.format("安装金额:%," + width + ".2f" + " 元%n", installCost)); sb.append(String.format("调试金额:%," + width + ".2f" + " 元%n", debugCost)); sb.append(String.format("其他金额:%," + width + ".2f" + " 元%n", otherCost)); sb.append(QUOTATION_TITLE_LINE.repeat(9)).append("\n"); return sb.toString(); } }
父类 手机号正则 构造方法调用setter方法 toString()重写
▼text复制代码public class Day37Person { private static final String PHONE_REGEX = "^1[3-9]\\d{9}$"; private static final String ERROR_PHONE_FORMAT = "手机号码格式不正确,已置空!"; private String name = ""; private String phone = ""; public Day37Person() { } public Day37Person(String name, String phone) { setName(name); setPhone(phone); } public void setName(String name) { this.name = name; } public void setPhone(String phone) { if (phone != null && phone.matches(PHONE_REGEX) ){ this.phone = phone; }else{ System.out.println(ERROR_PHONE_FORMAT); this.phone = ""; } } public String getName() { return this.name; } public String getPhone() { return this.phone; } @Override public String toString() { return "姓名:" + this.getName() + ",电话:" + this.getPhone(); } public String introduce(){ return ""; } }
Company子类,继承Person类 构造方法调用父类构造方法 构造方法调用setter方法 toString()重写 StringBuilder()拼接
▼text复制代码public class Day37Company extends Day37Person { private String companyName = ""; private String address = ""; private String serviceScope = ""; public Day37Company() { } public Day37Company(String name, String phone, String companyName, String address, String serviceScope) { super(name, phone); setCompanyName(companyName); setAddress(address); setServiceScope(serviceScope); } public void setCompanyName(String companyName) { this.companyName = companyName; } public void setAddress(String address) { this.address = address; } public void setServiceScope(String serviceScope) { this.serviceScope = serviceScope; } public String getCompanyName() { return this.companyName; } public String getAddress() { return this.address; } public String getServiceScope() { return this.serviceScope; } @Override public String toString() { return "【公司】联系人:" + this.getName() + ",电话:" + this.getPhone() + ",品牌:" + this.getCompanyName() + ",地址:" + this.getAddress() + ",服务范围:" + this.getServiceScope(); } public String introduce() { StringBuilder sb = new StringBuilder(); sb.append("您好,我们是").append(this.getCompanyName()).append(",公司位于").append(this.getAddress()).append(",主营").append(this.getServiceScope()).append(",联系人").append(this.getName()); return sb.toString(); } }
Worker子类,继承Person类 构造方法调用父类构造方法 构造方法调用setter方法 setter方法判断边界 StringBuilder()拼接 toString()重写
▼text复制代码public class Day37Worker extends Day37Person { private static final String ERROR_WORK_YEAR = "工龄0-70,其他无效!已置0"; private static final String ERROR_SCORE = "评分只能0-5,整数或一位小数!已置0"; private String jobType = ""; private int workYear = 0; private double score = 0.0; public Day37Worker() { } public Day37Worker(String name, String phone, String jobType, int workYear, double score) { super(name, phone); setJobType(jobType); setWorkYear(workYear); setScore(score); } public void setJobType(String jobType) { this.jobType = jobType; } public void setWorkYear(int workYear) { if (workYear >= 0 && workYear <= 70) { this.workYear = workYear; } else { System.out.println(ERROR_WORK_YEAR); this.workYear = 0; } } public void setScore(double score) { if (score >= 0 && score <= 5) { this.score = score; } else { System.out.println(ERROR_SCORE); this.score = 0.0; } } public String getJobType() { return this.jobType; } public int getWorkYear() { return this.workYear; } public double getScore() { return this.score; } @Override public String toString() { return "【师傅】:" + this.getName() + "," + this.getPhone() + "," + this.getJobType() + "," + this.getWorkYear() + "," + this.getScore(); } public String introduce() { StringBuilder sb = new StringBuilder(); sb.append("我是").append(this.getJobType()).append(",从业").append(this.getWorkYear()).append("年,评分").append(this.getScore()).append(",电话").append(this.getPhone()); return sb.toString(); } }
Customer子类,继承Person类 构造方法调用父类构造方法 构造方法调用setter方法 toString()重写 StringBuilder()拼接
▼text复制代码public class Day37Customer extends Day37Person { private static final String ERROR_HOUSE_AREA = "面积大于0小于5000,整数或一位小数!"; private String houseAddress = ""; private double houseArea = 0.0; public Day37Customer() { } public Day37Customer(String name, String phone, String houseAddress, double houseArea) { super(name, phone); setHouseAddress(houseAddress); setHouseArea(houseArea); } public void setHouseAddress(String houseAddress) { this.houseAddress = houseAddress; } public void setHouseArea(double houseArea) { if (houseArea > 0.0 && houseArea < 5000.0) { this.houseArea = houseArea; } else { System.out.println(ERROR_HOUSE_AREA); this.houseArea = 0.0; } } public String getHouseAddress() { return this.houseAddress; } public double getHouseArea() { return this.houseArea; } @Override public String toString() { return "【业主】:" + this.getName() + "," + this.getPhone() + "," + this.getHouseAddress() + "," + this.getHouseArea() + "m²"; } public String introduce() { StringBuilder sb = new StringBuilder(); sb.append("业主").append(this.getName()).append(",房屋面积").append(this.getHouseArea()).append("m²,位于").append(this.getHouseAddress()).append(",需要智能家居报价。"); return sb.toString(); } }
评论
问答助学
相关内容
0个评论
全部评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
