​1.设计四个类,分别是:(知识点:抽象类及抽象方法)

(1) Shape 表示图形类,有面积属性 area 、周长属性 per ,颜色属性 color ,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是: getArea 计算面积、 getPer 计算周长、 showAIl 输出所有信息,还有一个求颜色的方法 getColor 。

(2)2个子类:Rectangle 表示矩形类,增加两个属性,Width 表示长度、 height 表示宽度,重写getPer 、 getArea 和 showAl 三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。 Circle 表示圆类,增加1个属性, radius 表示半径,重写 getPer 、 getArea 和 showAl 三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。

(3)一个测试类 PolyDemo ,在 main 方法中,声明创建每个子类的对象,并调用2个子类的 showAl 方法。​

package Test;

public abstract class Shape {
private double area,per;
private 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 Test;

public class Rectangle extends Shape {
private double width,height;
public Rectangle(String color,double width, double height) {
super(color);
this.width = width;
this.height = height;
}

@Override
public double getArea() {
// TODO Auto-generated method stub
return(this.height*this.width);
}

@Override
public double getPer() {
// TODO Auto-generated method stub
return(2*(this.height+this.width));
}

@Override
public void showAll() {
// TODO Auto-generated method stub
System.out.println("矩形的长:"+this.height+";宽:"+this.width+";颜色:"+this.getColor()+";面积:"+this.getArea()+";周长:"+this.getPer());
}

}
package Test;

public class Circle extends Shape {
public static final double PI=3.14159;
private double radius;
public Circle(String color,double radius) {
super(color);
this.radius = radius;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return (PI*radius*radius);
}

@Override
public double getPer() {
// TODO Auto-generated method stub
return (2*PI*radius);
}

@Override
public void showAll() {
// TODO Auto-generated method stub
System.out.println("圆的半径:"+this.radius+";颜色:"+this.getColor()+";面积:"+this.getArea()+";周长:"+this.getPer());
}

}
package Test;

public class PolyDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
Shape rectangle=new Rectangle("白色",2,4);
Shape circle=new Circle("红色",2);
rectangle.showAll();
circle.showAll();
}
}

运行结果如下:

JAVA实验--抽象类及抽象方法_子类

2.Cola 公司的雇员分为以下若干类:(知识点:多态)

(1) ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法: getSalary ( int month )根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。

(2) SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。属性:月薪

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

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

(5)定义一个类 company ,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类 Testcompany ,在 main 方法,把若干各种类型的员工放在一个 ColaEmployee 数组里,并单元出数组中每个员工当月的工资。

思路:本题目的重点是从所有不同类型的员工中抽象出他们共有的特性,对于本题而言,所有的员工都有getSalary这一个方法,从而明白定义抽象类。之后按照题意创建company类,此类的目的是为了完成多态性,在运行时确定具体的类,不用修改源程序代码。

package Test;

public abstract class ColaEmployee {//抽象类
private String name;
private int month;
public ColaEmployee(String name, int month) {
super();
this.name = name;
this.month = month;
}

public String getName() {
return name;
}

public int getMonth() {
return month;
}

public abstract double getSalary(int month);//抽象方法,无方法体

}
package Test;

public class SalariedEmployee extends ColaEmployee {
private double monthMoney;
public SalariedEmployee(String name,int month,double monthMoney) {
super(name,month);//调用父类的有参构造方法
this.monthMoney = monthMoney;
}

@Override
public double getSalary(int month) {
// TODO Auto-generated method stub
if(month==this.getMonth()) {
return this.monthMoney+100;
}else {
return this.monthMoney;
}
}

}
package Test;

public class HourlyEmployee extends ColaEmployee {
private int hour;
private double hourMoney;
public HourlyEmployee(String name, int month, int hour, double hourMoney) {
super(name, month);
this.hour = hour;
this.hourMoney = hourMoney;
}
@Override
public double getSalary(int month) {
// TODO Auto-generated method stub
double money=0;
if(this.hour>160) {
money= (160*this.hourMoney+(this.hour-160)*this.hourMoney*1.5);
}else {
money= this.hourMoney *this.hour ;
}
if(month==this.getMonth()) {
return money+100;
}else
return money;
}

}
package Test;

public class SalesEmployee extends ColaEmployee {
private double monthPay;
private double rate;
public SalesEmployee(String name, int month, double monthPay, double rate) {
super(name, month);
this.monthPay = monthPay;
this.rate = rate;
}
@Override
public double getSalary(int month) {
// TODO Auto-generated method stub
double money=0;
money= this.monthPay*(1+this.rate);
if(month==this.getMonth()) {
return money+100;
}else
return money;
}

}
package Test;

public class Company {
public void getMoney(ColaEmployee c,int month) {
System.out.println("第"+month+"月,员工"+c.getName()+
"的工资为:"+c.getSalary(month)+"元");
}
}
package Test;

import java.util.Calendar;

public class TestCompany {

public static void main(String[] args) {
// TODO Auto-generated method stub
ColaEmployee c[]=new ColaEmployee[3];
c[0]=new SalariedEmployee("Lily",5,5000);
c[1]=new HourlyEmployee("Tom",3,100,75);
c[2]=new SalesEmployee("Jerry",3,5500,0.02);
Company company=new Company();
Calendar calendar = Calendar.getInstance();
for(int i=0;i<c.length;i++) {
company.getMoney(c[i],calendar.get(Calendar.MONTH)+1);
}
}

}

运行结果如下:

JAVA实验--抽象类及抽象方法_多态_02