继承和多态对应的几类问题
继承和多态对应的几类问题
- 员工薪资问题:
例:
某公司的雇员分为以下若干类: Employee:这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:void getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励1000元。 SalariedEmployee:Employee的子类,拿固定工资的员工。属性:月薪 HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数 SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率 BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。属性:底薪。
1 //定义一个父类,包含name和birthday属性 2 public class Employee {办法 3 public String name; 4 public int birthday; 5 public void getSalary(int month){ 6 } 7 } 8 //定义一个子类,第一类员工的结算工资办法 9 class SalariedEmployee extends Employee{ 10 public double yx; 11 @Override 12 public void getSalary(int month){ 13 if(month==birthday){ 14 System.out.println(name+month+"月份工资为"+(yx+1000)+"元"); 15 }else{ 16 System.out.println(name+month+"月份工资为"+yx+"元"); 17 } 18 } 19 } 20 //定义一个子类,第二类员工的结算工资办法 21 class HourlyEmployee extends Employee { 22 public double hgz; 23 public double hour; 24 @Override 25 public void getSalary(int month) { 26 double gz = (hour < 160) ? (hour * hgz) : (hour - 160) * 1.5 + (160 * hgz); 27 if (month == birthday) { 28 System.out.println(name + month + "月份工资为" + (gz + 1000) + "元"); 29 } else { 30 System.out.println(name + month + "月份工资为" + gz + "元"); 31 } 32 } 33 } 34 //定义一个子类,第三类员工的结算工资办法 35 class SalesEmployee extends Employee{ 36 public double xse; 37 public double tcl; 38 @Override 39 public void getSalary(int month){ 40 double gz=(month==birthday)?(xse*tcl+1000):(xse*tcl); 41 System.out.println(name+month+"月份工资为"+gz+"元"); 42 } 43 } 44 //定义一个子类,第四类员工的结算工资办法 45 class BasePlusSalesEmployee extends SalesEmployee{ 46 public double dx; 47 @Override 48 public void getSalary(int month){ 49 double gz=(month==birthday)?(xse+tcl+dx+1000):(xse+tcl+dx); 50 System.out.println(name+month+"月份工资为"+gz+"元"); 51 } 52 } 53 //定义一个测试类,并创建四个对象进行实例化操作 54 class Test2 { 55 public static void main(String[] args) { 56 SalariedEmployee e1 = new SalariedEmployee(); 57 e1.name = "张三"; 58 e1.birthday = 1; 59 e1.yx = 3000; 60 61 HourlyEmployee e2 = new HourlyEmployee(); 62 e2.name = "李四"; 63 e2.birthday = 2; 64 e2.hgz = 10; 65 e2.hour = 160; 66 SalesEmployee e3 = new SalesEmployee(); 67 e3.name = "王五"; 68 e3.birthday = 3; 69 e3.xse = 100000; 70 e3.tcl = 0.1; 71 BasePlusSalesEmployee e4 = new BasePlusSalesEmployee(); 72 e4.name = "赵六"; 73 e4.birthday = 4; 74 e4.xse = 100000; 75 e4.tcl = 0.1; 76 e4.dx = 2000; 77 //打印四个人的名字 78 Employee[] e = {e1, e2, e3, e4}; 79 for (Employee em : e) { 80 System.out.println(em.name); 81 } 82 //打印这四人12个月来每个月的薪资情况 83 for (int i = 1; i < 13; i++) { 84 System.out.println("------------第" + i + "月工资表------------"); 85 e1.getSalary(i); 86 e2.getSalary(i); 87 e3.getSalary(i); 88 e4.getSalary(i); 89 } 90 } 91 }
在多态中,涉及到将子类对象当做父类类型使用的情况,例如Animal animal=new dog();将子类对象当做父类使用时不需要任何显式声明,需要注意的是,此时不能通过父 类变量去调用子类中的某些方法,这时要用到对象类型转换。
instanceof关键字可以判断一个对象是否为某个类(或接口)的实例或者子类实例。
例:
1 public interface Animal { 2 void shout(); 3 } 4 class Dog implements Animal{ 5 public void shout(){ 6 System.out.println("汪汪汪"); 7 } 8 } 9 class Cat implements Animal { 10 public void shout() { 11 System.out.println("喵喵喵"); 12 } 13 14 public static void main(String[] args) { 15 16 Cat cat = new Cat(); 17 Dog dog = new Dog(); 18 19 animalShout(cat); 20 animalShout(dog); 21 // animal1.shout(); 22 // animal2.shout(); 23 /* animalShout(animal1); 24 animalShout(animal2); 25 } 26 public static void animalShout(Animal animal){ 27 animal.shout(); 28 }*/ 29 } 30 31 public static void animalShout(Animal animal) { 32 if (animal instanceof Dog) { /*instanceof关键字判断animalShout方法中传入的对象是否为Dog类型*/ 33 //Cat cat = (Cat) animal; 34 //animal.shout(); 35 Dog dog = (Dog) animal; 36 animal.shout(); 37 } else if(animal instanceof Cat){ /*instanceof关键字判断animalShout方法中传入的对象是否为Cat类型*/ 38 Cat cat=(Cat) animal; 39 animal.shout(); 40 } 41 } 42 }
- 匿名内部类
匿名内部类是实现接口的一种简便写法,在程序中不一定非要使用匿名内部类。
格式:new 父类(参数列表)或父接口(){/*匿名内部类实现部分*/}
1 //匿名内部类 2 public interface Jiaju { 3 public void name(); 4 5 public class Test { 6 public static void main(String[] args) { 7 zhidaoName(new Jiaju() { 8 @Override 9 public void name() { 10 System.out.println("我是凳子"); 11 } 12 }); 13 } 14 public static void zhidaoName(Jiaju a){ 15 a.name(); 16 } 17 } 18 }