当前位置:文档之家› 继承封装多态实例

继承封装多态实例

1.设计一个形状类Shape,方法:求周长和求面积
形状类的子类:Rect(矩形),Circle(圆形)
Rect类的子类:Square(正方形)
不同的子类会有不同的计算周长和面积的方法
创建三个不同的形状对象,放在Shape类型的数组里,分别打印出每个对象的周长和面积

package https://www.doczj.com/doc/0119004430.html,;

public class Good{
public static void main(String[] args){
Shape[] s=new Shape[3];

s[0]=new Rect(6,3);
s[1]=new Square(6);
s[2]=new Circle(5);

for(int i=0;iSystem.out.println("周长:"+s[i].zhouChang());
System.out.println("面积:"+s[i].mianJi());
}

}
}

// 设计一个Shape类,并生成两个方法
class Shape{
double zhouChang(){
return 0.0;
}
double mianJi(){
return 0.0;
}
}

// 矩形类继承Shape类
class Rect extends Shape{
private double chang;
private double kuan;

public Rect(){

}

public Rect(double chang,double kuan){
this.chang=chang;
this.kuan=kuan;
}
public double zhouChang(){
return (chang+kuan)*2;
}
public double mianJi(){
return chang*kuan;
}
}

// 圆形类继承Shape类
class Circle extends Shape{
private double r;

public Circle(){

}

public Circle(double r){
this.r=r;
}

public double zhouChang(){
return 2*3.1415926*r;
}

public double mianJi(){
return 3.1415926*r*r;
}
}

// 正方形类继承Rect类
class Square extends Rect{
private double bianChang;

public Square(){

}

public Square(double bianChang){
super();
this.bianChang=bianChang;
}

public double zhouChang(){
return bianChang*4;
}

public double mianJi(){
return bianChang*bianChang;
}
}

2.某公司的雇员分为以下若干类:
Employee:这是所有员工总的父类,属性:员工的姓名,员工的生日月份。
方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。

SalariedEmployee:Employee的子类,拿固定工资的员工。属性:月薪

HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。
属性:每小时的工资、每月工作的小时数

SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定。
属性:月销售额、提成率

BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。
属性:底薪。

写一个程序,把若干各种类型的员工放在一个Employee数组里,写一个函数,
打印出某月每个员工的工资数额。
注意:要求把每个类都做成完全封装,不允许非私有化属性。

package https://www.doczj.com/doc/0119004430.html,;

public class Who{
public static void main(String[] args){
Employee[] a=new Employee[9];
a[0]=new SalariedEmployee(2500,"Tom",10);

a[1]=new HourlyEmployee(170,20,"Jay",2);
a[2]=new HourlyEmployee(158,19,"Lily",5);
a[3]=new SalesEmployee(1240,0.5,"Lucy",7);
a[4]=new SalesEmployee(2730,0.6,"MeiYe",11);
a[5]=new BasePlusSalesEmployee(2450,0.4,2500,"Sungw",8);
a[6]=new BasePlusSalesEmployee(2250,0.4,2300,"Zhangw",3);
a[7]=new SalariedEmployee(2700,"Ham",9);
a[8]=new SalesEmployee(1210,0.5,"Jams",1);
for(int i=0;idouble money=a[i].getSalary(9);
System.out.println("员工"+i+"这个月的薪水为:"+money);
}
}
}
class Employee{
private String name;
private int month;
private double salary;
public Employee(){
}
public Employee(String name,int month){
https://www.doczj.com/doc/0119004430.html,=name;
this.month=month;
}
public double getSalary(int month){
if(this.month==month){//如果此月恰逢某员工的生日月
salary+=100;//在薪水的基础上再加100块
}
return salary;
}
}
class SalariedEmployee extends Employee{//SalariedEmployee
private double monthsalary;
public SalariedEmployee(double monthsalary,String name,int month){//在构造方法里对实例变量赋值
super(name,month);//调用父类有参的构造方法
this.monthsalary=monthsalary;
}
public double getSalary(int month){
double salary=super.getSalary(month);//调用父类getSalary方法
salary+=this.monthsalary;//再加上月工资
return salary;
}
}
class HourlyEmployee extends Employee{
private int hours;
private double hoursalary;
public HourlyEmployee(int hours,double hoursalary,String name,int month){//在构造方法里对实例变量赋值
super(name,month);//调用父类有参的构造方法
this.hours=hours;
this.hoursalary=hoursalary;
}
public double getSalary(int month){
double salary=super.getSalary(month);//调用父类getSalary方法,再根据具体情况加上必要的小时数与小时工资所得的
if(hours<=160){//如果每月工作的小时数小于160小时
salary+=this.hours*this.hoursalary;
}
else if(hours>160){//大于160小时时
salary+=160*this.hoursalary+(this.hours-160)*this.hoursalary*1.5;
}
return salary;
}
}
class SalesEmployee extends Employee{
private double monthsales;
private double rate;
public SalesEmployee(){
}
public SalesEmployee(double monthsales,double rate,String name,int month){//在构造方法里对实例变量赋值
super(name,month);//调用父类的有参的构造方法
this.monthsales=monthsales;
this.rate=rate;
}
public double getSalary(int month){
double salary=super.getSalary(month);//调用父类getSalary方法
salary+=this.monthsales*this.rate;//再加上月销售额乘以提成率
return salary;
}
}
class BasePlusSalesEmployee extends SalesEmployee{

private double basesales;
public BasePlusSalesEmployee(double monthsales,double rate,double basesales,String name,int month){//在构造方法里对实例变量赋值
super(monthsales,rate,name,month);//调用父类的有参的构造方法
this.basesales=basesales;
}
public double getSalary(int month){
double salary=super.getSalary(month);//调用父类getSalary方法
salary+=this.basesales;//再加上基本工资
return salary;
}
}


相关主题
文本预览
相关文档 最新文档