需求一:
项目经理类
属性:姓名 工号 工资 奖金
行为:工作work(打印姓名、工号、工资、奖金)
程序员类
属性:姓名 工号 工资
行为:工作work(打印姓名、工号、工资)
需求二:
已知学生类和老师类如下:
属性:姓名,年龄
行为:吃饭
老师有特有的方法:讲课
学生有特有的方法:学习
需求三:
已知猫类和狗类:
属性:毛的颜色,腿的个数
行为:吃饭
猫特有行为:抓老鼠catchMouse
狗特有行为:看家lookHome
代码示例:
package cn.zxj.com; import java.math.BigDecimal; public class Staff { //姓名 工号 工资 private String name; private String staffNumber; private BigDecimal income; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStaffNumber() { return staffNumber; } public void setStaffNumber(String staffNumber) { this.staffNumber = staffNumber; } public BigDecimal getIncome() { return income; } public void setIncome(BigDecimal income) { this.income = income; } public void work(){ System.out.println("员工姓名:"+name+";员工编号:"+staffNumber+";收入:"+income); } }
package cn.zxj.com; import java.math.BigDecimal; public class ProjectManager extends Staff{ private BigDecimal bonus; public BigDecimal getBonus() { return bonus; } public void setBonus(BigDecimal bonus) { this.bonus = bonus; } public void work(){ System.out.println("员工姓名:"+this.getName()+";员工编号:"+this.getStaffNumber()+";收入:" +this.getIncome()+";奖金:"+this.bonus); } }
package cn.zxj.com; public class Programmer extends Staff{ }
package cn.zxj.com; import java.math.BigDecimal; public class Test { public static void main(String[] args){ ProjectManager projectManager = new ProjectManager(); projectManager.setName("wang"); projectManager.setStaffNumber("001"); projectManager.setIncome(new BigDecimal("20000")); projectManager.setBonus(new BigDecimal("5000")); projectManager.work(); Programmer programmer = new Programmer(); programmer.setName("xiaowang"); programmer.setStaffNumber("002"); programmer.setIncome(new BigDecimal("30000")); programmer.work(); } }