貌似又是多态的问题 不会了啊

猫儿爷爷 2010-03-28 04:46:13
/*2
.某公司的雇员分为以下若干类:
Employee:这是所有员工总的父类,
属性:员工的姓名,员工的生日月份。
方法:getSalary(int month)
根据参数月份来确定工资,
如果该月员工过生日,则公司会额外奖励100元。
SalariedEmployee:Employee的子类,拿固定工资的员工。属性:月薪
HourlyEmployee:Employee的子类,按小时拿工资的员工
,每月工作超出160小时的部分按照1.5倍工资发放。
属性:每小时的工资、每月工作的小时数
SalesEmployee:Employee的子类,销售人员,
工资由月销售额和提成率决定。属性:月销售额、提成率
BasedPlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,
工资由底薪加上销售提成部分。属性:底薪。
写一个程序,把若干各种类型的员工放在一个Employee数组里,
写一个函数,打印出某月每个员工的工资数额。
注意:要求把每个类都做成完全封装,不允许非私有化属性
*/
class Employee //所有员工总的父类
{
private String name ;//员工的姓名
private int yue ; //员工的生日

public Employee()
{
}
public Employee( String name , int yue ) //构造方法
{
this.name = name ;
this.yue = yue ;
}
void setName( String name )
{
this.name = name ;
}
void setYue (int yue)
{
this.yue = yue ;
}
String getName()
{
return name ;
}
int getYue()
{
return yue ;
}
void getSalary(int month) //方法:getSalary(int month)
{
if ( yue == month )
{
System.out.println("生日快乐,公司将额外奖励你100");
}
}
void gongzi ()
{
System.out.println("父类的成员方法");
}
}
//拿固定工资的员工
class SalariedEmployee extends Employee
{
private double yuexin ; //月薪

public SalariedEmployee ( double yuexin ) //构造方法
{
this.yuexin = yuexin ;
}
void setYuexin( double yuexin )
{
this.yuexin = yuexin ;
}
double getYuexin()
{
return (double)yuexin;
}
void gongzi ()
{
System.out.println("拿固定工资得是" + yuexin );
}

}

//按小时拿工资的员工
class HourlyEmployee extends Employee
{
private double gongzi ; //每小时的工资、
private int xiaoshi ; //每月工作的小时数

public HourlyEmployee()
{

}
public HourlyEmployee ( double gongzi , int xiaoshi )//构造方法
{
this.gongzi = gongzi ;
this.xiaoshi = xiaoshi ;
}

void setGongzi ( double gongzi )
{
this.gongzi = gongzi ;
}
void setXiaoshi ( int xiaoshi )
{
this.xiaoshi = xiaoshi ;
}

double getGongzi()
{
return (double)gongzi ;
}
int getXiaoshi()
{
return xiaoshi ;
}
//每月工作超出160小时的部分按照1.5倍工资发放
void gongzi () //工作时间
{
if ( xiaoshi > 160 )
{
gongzi =(double) (gongzi * xiaoshi * 1.5) ;
System.out.println("超出时间得工资是 " + gongzi );
}
else
{
gongzi = (double)gongzi * xiaoshi ;
System.out.println("没有超出时间得工资是" + gongzi );
}
}
}

//销售人员,
class SalesEmployee extends Employee
{
private double xiaoshou ; //月销售额
private double ticheng ; // 提成率

public SalesEmployee()
{

}
public SalesEmployee ( double xiaoshou , double ticheng ) //构造方法
{
this.xiaoshou = xiaoshou ;
this.ticheng = ticheng ;
}

void setXiaoshou( double xiaoshou )
{
this.xiaoshou = xiaoshou ;
}
void setTicheng( double ticheng )
{
this.ticheng = ticheng ;
}

double getXiaoshou()
{
return (double)xiaoshou ;
}
double getTicheng()
{
return (double)ticheng ;
}
//工资由月销售额和提成率决定
void gongzi()
{
System.out.println("销售得工资是" +( xiaoshou * ticheng )) ;
}

}
//有固定底薪的销售人员
class BasedPlusSalesEmployee extends SalesEmployee
{
private double dixin ;
private double ticheng;

public BasedPlusSalesEmployee ( double dixin )
{
this.dixin = dixin ;
}
public void setDixin( double dixin )
{
this.dixin = dixin ;
}
public double getDixin()
{
return (float)dixin ;
}

void gongzi ()
{
System.out.println("拿底薪得销售工资是 " + (dixin * ticheng)) ;
}
}

class EmployeeTest
{
public static void main ( String [] args )
{
Employee [] a = new Employee [4];
a[0] = new Employee ("张三" , 1);
a[1] = new HourlyEmployee(0.0 , 0 );
a[2] = new SalesEmployee(0.0 , 0.0 );
a[3] = new BasedPlusSalesEmployee(0.0);


}
}


各位 这个我前面写出来了 可是写到 写一个程序,把若干各种类型的员工放在一个Employee数组里,
写一个函数,打印出某月每个员工的工资数额。 这里就不知道怎么做了
还有我写的这些是封装嘛?????? 那里不好的 麻烦个人牛人说出来 详细点啊
...全文
327 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
仙茅 2010-03-31
  • 打赏
  • 举报
回复
代码太长,看得头晕...
kyousuke 2010-03-30
  • 打赏
  • 举报
回复
好吧 我承认我是来拿分的
最近手头有点紧,想提个问题都不行
maxtomb 2010-03-30
  • 打赏
  • 举报
回复
因为都是员工,都有员工父类的特点,所以函数定义中的参数也应该是员工的父类,在JAVA中用父类或者接口来NEW子类是很常见的事。因为数组中的元素类型必须是一致的,如果把对象都变成员工的父类那么就符合要求了。至于把他们添加到数组中 加个for循环赋值就可以了。
打印工资,你可以覆盖每个子类中的toString方法,从数组中取出来,然后转换类型,这样直接打印数组中的对象就可以了。
无聊司马 2010-03-29
  • 打赏
  • 举报
回复
当你定义了一个类时 类里面一定有它的若干个成员变量而且还有它的方法 而这些类的成员变量和方法就被封装在这个类里面 这也是封装的一个书面上的定义
另外你写的这个函数还是觉得有点模糊 我帮你改了一下 大体上的还是原样的
代码如下:
class Employee //所有员工总的父类
{
private String name ;//员工的姓名
private int yue ; //员工的生日
public Employee()
{}
public Employee(String name ,int yue) //构造方法
{
this.name = name ;
this.yue = yue ;
}
void setName(String name)
{
this.name = name;
}
void setYue (int yue)
{
this.yue = yue ;
}
public String getName()
{
return name ;
}
public int getYue()
{
return yue ;
}
void getSalary(int month) //方法:
{
if (yue==month )
{
System.out.println("生日快乐,公司将额外奖励你100");
}
}
void gongzi()
{
System.out.println("父类的成员方法");
}
}
//拿固定工资的员工
class SalariedEmployee extends Employee
{
private double yuexin ; //月薪
public SalariedEmployee(double yuexin) //构造方法
{
this.yuexin = yuexin ;
}
void setYuexin(double yuexin)
{
this.yuexin = yuexin ;
}
double getYuexin()
{
return (double)yuexin;
}
void gongzi ()
{
System.out.println("拿固定工资得是" +yuexin );
}
}
//按小时拿工资的员工
class HourlyEmployee extends Employee {
private double gongzi ; //每小时的工资、
private int xiaoshi ;
double ewai=0;
public HourlyEmployee()
{}
public HourlyEmployee(double gongzi ,int xiaoshi)//构造方法
{

this.gongzi = gongzi ;
this.xiaoshi = xiaoshi ;
}
void setGongzi(double gongzi)
{
this.gongzi =gongzi ;
}
void setXiaoshi(int xiaoshi)
{
this.xiaoshi = xiaoshi ;
}
double getGongzi()
{
return (double)gongzi ;
}
int getXiaoshi()
{
return xiaoshi ;
} //每月工作超出160小时的部分按照1.5倍工资发放

void gongzi () //工作时间
{
if ( xiaoshi < 160 ) {
ewai=0;
}
else{
ewai =(double) ((xiaoshi-160 )* 1.5) ;
System.out.println("额外时间得工资是 " +ewai );
}
gongzi=gongzi+ewai;
System.out.println("所得到的工资是" +gongzi );
}
}
//销售人员,
class SalesEmployee extends Employee {
private double xiaoshou ;
private double ticheng ;
double i=50.0; //销售一件可以得到的钱
double sum=0;

public SalesEmployee()
{}
public SalesEmployee(double xiaoshou ,double ticheng) //构造方法
{
this.xiaoshou = xiaoshou ;
this.ticheng = ticheng ;
}
void setXiaoshou(double xiaoshou)
{
this.xiaoshou = xiaoshou ;
}
void setTicheng(double ticheng)
{
this.ticheng = ticheng;
}
double getXiaoshou()
{
return (double)xiaoshou ;
}
double getTicheng()
{
return (double)ticheng;
}
void gongzi()
{
sum=(xiaoshou)*i+ticheng;
System.out.println("销售得工资是:" +sum) ;
}
}
//有固定底薪的销售人员
class BasedPlusSalesEmployee extends SalesEmployee {
private double dixin ;
private double ticheng;
BasedPlusSalesEmployee(double dixin,double ticheng){
this.dixin=dixin;
this.ticheng=ticheng;
}
/*public BasedPlusSalesEmployee(double dixin)
{
this.dixin = dixin ;
}*/
public void setDixin(double dixin)
{
this.dixin = dixin ;
}
public double getDixin()
{
return (float)dixin ;
}
void gongzi()
{
System.out.println("拿底薪得销售工资是 " + (dixin + ticheng)) ;
}
}
public class EmployeeTest {
public static void main ( String [] args ){
SalariedEmployee s=new SalariedEmployee(2000);
HourlyEmployee h=new HourlyEmployee(2500,240);
SalesEmployee d=new SalesEmployee(100,1000);
BasedPlusSalesEmployee b=new BasedPlusSalesEmployee(2500,1000);
System.out.println("第一个员工的工资情况为:");
s.gongzi();
System.out.println("");
System.out.println("第二个员工的工资情况为:");
h.gongzi();
System.out.println("");
System.out.println("第三个员工的工资情况为:");
d.gongzi();
System.out.println("");
System.out.println("第四个员工的工资情况为:");
b.gongzi();
}
}
猫儿爷爷 2010-03-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 amos1989 的回复:]
....又是你. 老来问作业0.0
上次就你说一个JAVA文件只写一个类。你看你... 一个public类都有,能编译通过???
还这样写,不听话。懒得回答你
[/Quote] 0.0想偷懒 下次开始打包 闷闷 主要闲打包麻烦 不过现在知道了 那样看得明白 这样一排写下来 我看的发晕 也算作业吧 主要是不会了 不知道该怎么做了
amos1989 2010-03-28
  • 打赏
  • 举报
回复
....又是你. 老来问作业0.0
上次就你说一个JAVA文件只写一个类。你看你... 一个public类都有,能编译通过???
还这样写,不听话。懒得回答你
canoe982 2010-03-28
  • 打赏
  • 举报
回复
JAVA的动态多态性本就可以很好的完成你需要的工作。直接用数组中的元素调用gongzi 这个函数就好了。
xiaohuanjie 2010-03-28
  • 打赏
  • 举报
回复
这个题很像我曾经做过的一个实验


LZ参考这里:http://blog.csdn.net/XiaoHuanJie/archive/2009/09/21/4575587.aspx

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧