第九次
3、设计三个类,分别如下:(知识点:抽象类及抽象方法)[必做题]
·3.1设计 Shape 表示图形类,有面积属性 area 、周长属性 per ,
颜色属性 color ,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是: getArea 计算面积、 getPer 计算周长、 showAll 输出所有信息,还有一个求颜色的方法 getColor 。
·3.2设计2个子类:
·3.2.1 Rectangle 表示矩形类,增加两个属性, Width 表示长度
、 height 表示宽度,重写 getPer 、 getArea 和 showAll 三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。
3.2.2 Circle 表示圆类,增加1个属性, radius 表示半径,重写
getPer 、 getArea 和 showAll 三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。
·3.3在 main 方法中,声明创建每个子类的对象,并调用2个
子类的 showAll 方法。
package sykjxy.com;
public abstract class shape {
double area;
double per;
String color;
public shape() {
super();
}
public shape(String color) {
super();
this.color = color;
}
public abstract double getArea();
public abstract double getPer();
public abstract void showAll();
public String getColor() {
return color;
}
}
package sykjxy.com;
public class Rectangle extends shape {
double Width;
double height;
public Rectangle() {
super();
}
public Rectangle(double width, double height, String color) {
super(color);
Width = width;
this.height = height;
}
public double getArea() {
double Area = Width * height;
return Area;
}
public double getPer() {
double Per = 2 * Width + 2 * height;
return Per;
}
public void showAll() {
System.out.println("长:" + height);
System.out.println("宽:" + Width);
System.out.println("面积:" + getArea());
System.out.println("周长:" + getPer());
System.out.println("颜色" + color);
}
}
package sykjxy.com;
public class Circle extends shape{
public Circle() {
super();
}
public Circle(double radius,String color) {
super(color);
this.radius = radius;
}
double radius;
public double getArea() {
double Area=radius*radius*3.14;
return Area;
}
public double getPer()
{
double Per=2*3.14*radius;
return Per;
}
public void showAll()
{
System.out.println("半径:"+radius);
System.out.println("面积:"+getArea());
System.out.println("周长:"+getPer());
System.out.println("颜色"+color);
}
}
package sykjxy.com;
public class use {
public static void main(String[] args) {
// TODO Auto-generated method stub
shape test_1 = new Circle(3, "银色");
shape test_2 = new Rectangle(6, 7, "白的");
test_1.showAll();
test_2.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 数组里,并单元出数组中每个员工当月的工资。
资的员工。属性:月薪
package sykjxy.com;
public class ColaEmployee {
protected String name;
protected int month;
public ColaEmployee() {
super();
}
public ColaEmployee(String name, int month) {
super();
this.name = name;
this.month = month;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public double getSalary(int month) {
return 0;
}
}
package sykjxy.com;
public class company {
public void getSalary(ColaEmployee c, int month) {
System.out.println(c.name + "在" + month + "月的月薪为" + c.getSalary(month) + "元");
}
}
package sykjxy.com;
public class HourlyEmployee extends ColaEmployee {
protected int hour;
protected int hoursalary;
public HourlyEmployee() {
super();
}
public HourlyEmployee(String name, int month, int hour, int hoursalary) {
super(name, month);
this.hour = hour;
this.hoursalary = hoursalary;
}
public double getSalary(int month) {
if (super.month == month) {
if (hour > 160) {
return hoursalary * 160 + hoursalary * (hour - 160) * 1.5 + 100;
} else {
return hoursalary * hour + 100;
}
} else {
if (hour > 160) {
return hoursalary * 160 + hoursalary * (hour - 160) * 1.5;
} else {
return hoursalary * hour;
}
}
}
}
package sykjxy.com;
public class SalesEmployee extends ColaEmployee {
protected int yuexiaoshou;
protected double tuchenglv;
public SalesEmployee() {
super();
}
public SalesEmployee(String name, int month, int yuexiaoshou, double tuchenglv) {
super(name, month);
this.yuexiaoshou = yuexiaoshou;
this.tuchenglv = tuchenglv;
}
public double getSalary(int month) {
if (super.month == month) {
return yuexiaoshou * tuchenglv + 100;
} else {
return yuexiaoshou * tuchenglv;
}
}
}
package sykjxy.com;
public class SalariedEmployee extends ColaEmployee {
private double salary;
public SalariedEmployee(double salary) {
super();
this.salary = salary;
}
public SalariedEmployee(String name, int month, double salary) {
super(name, month);
this.salary = salary;
}
public double getSalary(int month) {
if (super.month == month) {
return month + 100;
} else {
return salary;
}
}
}
package sykjxy.com;
public class testCompany {
public static void main(String[] args) {
// TODO Auto-generated method stub
ColaEmployee c1[] = {
new SalariedEmployee("lisi", 9, 20000),
new HourlyEmployee("wangwu", 6, 300, 200),
new SalesEmployee("zhangsan", 4, 30000, 0.5)
};
for (int i = 0; i < c1.length; i++) {
new company().getSalary(c1[i], 3);
}
}
}
浙公网安备 33010602011771号