第十次作业
- 3、设计三个类,分别如下:(知识点:抽象类及抽象方法) [必做题]
- 3.1 设计Shape表示图形类,有面积属性area、周长属性per,颜色属性clr,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、shwAll输出所有信息,还有一个求颜色的方法getClr。
- 3.2 设计 2个子类:
- 3.2.1 Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPer、getArea和shwAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。
- 3.2.2 Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和shwAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。
- 3.3 在main方法中,声明创建每个子类的对象,并调用2个子类的shwAll方法
-
1234567891011121314
packagetest;publicclassShape {doublearea;doubleper;charcolor;publicShape() {super();}publicShape(charcolor) {super();this.color = color;}}12345678910111213141516171819202122232425262728293031packagetest;publicclassRectangleextendsShape {publicdoublegetPer() {// TODO Auto-generated method stubreturn(width + height) *2;}publicdoublegetArea() {// TODO Auto-generated method stubreturnwidth * height;}doublewidth;doubleheight;publicRectangle(doublewidth,doubleheight,charcolor) {super();this.width = width;this.height = height;this.color = color;}publicRectangle() {super();}publicvoidshowAll() {System.out.println("矩形的长是"+ width +",宽是"+ height +",颜色是"+ color +",周长是"+ getPer() +",面积是"+ getArea());}}1234567891011121314151617181920212223packagetest;publicclassCircleextendsShape{doubleradius;publicvoidshowAll(){System.out.println("圆形的半径是"+radius+",颜色是"+color+",周长是"+getPer()+",面积是"+getArea());}publicdoublegetPer() {// TODO Auto-generated method stubreturn2*3.14*radius;}publicdoublegetArea() {// TODO Auto-generated method stubreturn3.14*radius*radius;}publicCircle(doubleradius,charcolor) {super();this.radius = radius;this.color=color;}}1234567891011121314packagetest;publicclassTest {publicstaticvoidmain(String[] args) {// TODO Auto-generated method stubRectangle r =newRectangle(2,3,'黑');r.showAll();Circle c =newCircle(4,'红');c.showAll();}}![]()
4、 Cola 公司的雇员分为以下若干类:(知识点:多态)[必做题]
·4.1 ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法: getSalary ( int month )根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。
4.2 SalariedEmployee :ColaEmployee 的子类,拿固定工
4.3 HourlyEmployee : ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数
·4.4 SalesEmployee : ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率.4.5定义一个类 Company ,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类 TestCompany ,在 main法,把若干各种类型的员工在一个 ColaEmployee 数组里,并单元出数组中每个员工当月的工资。资的员工。属性:月薪
//ColaEmployee类
-
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
packagetest;publicclassColaEmployee {protectedString name;protectedintmonth;publicColaEmployee() {super();}publicColaEmployee(String name,intmonth) {super();this.name = name;this.month = month;}publicintgetMonth() {returnmonth;}publicdoublegetSalary(intmonth) {return0;}}packagetest;publicclassColaEmployee {protectedString name;protectedintmonth;publicColaEmployee() {super();}publicColaEmployee(String name,intmonth) {super();this.name = name;this.month = month;}publicintgetMonth() {returnmonth;}publicdoublegetSalary(intmonth) {return0;}}12345678910111213141516171819202122232425262728293031323334packagetest;publicclassHourlyEmployeeextendsColaEmployee {protectedinthourmoney;protectedintdaymonth;publicHourlyEmployee() {super();}publicHourlyEmployee(String name,intmonth,inthourmoney,intdaymonth) {super(name, month);this.hourmoney = hourmoney;this.daymonth = daymonth;}publicdoublegetSalary(intmonth) {if(super.month == month) {if(daymonth >160) {returnhourmoney *160+ hourmoney * (daymonth -160) *1.5+100;}else{returnhourmoney * daymonth +100;}}else{if(daymonth >160) {returnhourmoney *160+ hourmoney * (daymonth -160) *1.5;}else{returnhourmoney * daymonth;}}}}1234567891011121314151617181920212223packagetest;publicclassSalariedEmployeeextendsColaEmployee {protecteddoublemoney;publicSalariedEmployee() {super();}publicSalariedEmployee(String name,intmonth,doublemoney) {super(name, month);this.money = money;}publicdoublegetSalary(intmonth) {if(super.month == month) {returnmoney +100;}else{returnmoney;}}}12345678910111213141516171819202122232425packagetest;publicclassSalesEmployeeextendsColaEmployee {protectedintmonthsales;protecteddoublerate;publicSalesEmployee() {super();}publicSalesEmployee(String name,intmonth,intmonthsales,doublerate) {super(name, month);this.monthsales = monthsales;this.rate = rate;}publicdoublegetSalary(intmonth) {if(super.month == month) {returnmonthsales * rate +100;}else{returnmonthsales * rate;}}}1234567packagetest;publicclassCompany {publicvoidgetSalary(ColaEmployee c,intmonth) {System.out.println(c.name +"在"+ month +"月的月薪为"+ c.getSalary(month) );}}1234567891011121314packagetest;publicclassTest {publicstaticvoidmain(String[] args) {// TODO Auto-generated method stubColaEmployee c[] = {newSalariedEmployee("salar",3,15000),newHourlyEmployee("hourly",5,20,200),newSalesEmployee("sales",8,10000,0.6) };for(inti =0; i < c.length; i++) {newCompany().getSalary(c[i],6);}}}![]()



浙公网安备 33010602011771号