1.任务一:抽象类
员工抽象类
包含3个属性:工号,姓名,工资。
一个抽象方法:工作描述(例如:不同员工有不同的工作内容)
项目经理子类
另外还有一个奖金属性
普通方法:踢足球
程序员子类
普通方法:看书
测试类:
测试经理和员工
interface Shape{
public double area();
}
class Triangle implements Shape{
private double height;
private double length;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
@Override
public double area() {
return length*height/2;
}
}
class Circle implements Shape{
private double radius;
private final double PI = 3.14;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double area() {
// TODO Auto-generated method stub
return PI * Math.pow(radius, 2);
}
}
public class TestShape {
public static void main(String[] args) {
// TODO Auto-generated method stub
Triangle t = new Triangle();
t.setHeight(44.5);
t.setLength(55.5);
System.out.println(t.area());
Circle c = new Circle();
c.setRadius(11.1);
System.out.println(c.area());
}
}
2.任务二:简单接口
飞行的接口:
IFly
飞机类:
Plane
麻雀类:
Spadger
测试类:
FlyTest
interface IFly{
void fly();
}
class Plane implements IFly{
@Override
public void fly() {
System.out.println("我是飞机");
}
}
class Spadger implements IFly{
@Override
public void fly() {
// TODO Auto-generated method stub
System.out.println("我是麻雀");
}
}
public class homewoek2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Plane plane = new Plane();
plane.fly();
Spadger spadger = new Spadger();
spadger.fly();
}
}
3.任务三:接口的定义和使用 计算面积
任务描述:
定义一个接口Shape,该接口有一个计算面积的方法area(),该方法返回一个double类型的返回值。
定义一个三角形类(Triangle),该类实现了Shape接口。该类有两个属性height(三角形的高),和length(三角形的底边长),该类实现了area()方法,以计算三角形的面积。
定义一个圆形类(Circle),该类实现了Shape接口。该类有一个属性radius(圆的半径),该类实现了area()方法,以计算圆形的面积。
定义Main类,在其main方法中,创建一个三角形类和一个圆形类,分别调用它们的area()方法以计算其面积。
interface Shape{
public double area() ;
}
class Triangle implements Shape{
int height;
int lenght;
@Override
public double area() {
double sum = height * lenght / 2;
return sum;
}
}
class Circle implements Shape{
int radius;
double Pi = 3.14;
@Override
public double area() {
double sum = radius * radius * Pi;
return sum;
}
}
public class homework3 {
public static void main(String[] args) {
Triangle triangle = new Triangle();
triangle.height = 3;
triangle.lenght = 4;
System.out.println(triangle.area());
Circle circle = new Circle();
circle.radius = 5;
System.out.println(circle.area());
}
}
4.任务四:抽象类及其实现类的使用
任务描述:
编写抽象员工类及其子类
编写抽象类Employee。该类有如下属性:
id(编号int型)
sex(性别 int型)
name(姓名String型)
duty(职务String型)
salary(薪水double型)
holidays(请假天数 int型)
该类有如下方法:
display(),无返回值,该方法能打印员工的姓名、性别以及职务三个属性
该类还有一个抽象方法
getSubsidy(int day) 返回值是int型。该方法能计算员工正常出勤补助工资。
编写普通员工类(NormalEmployee),该类是Employee的直接子类,并实现了getSubsidy (int day)方法。普通员工补助工资的计算方法是:如果请假天数小于等于3,则补助工资为20× 出勤天数;如果请假天数超过3天,则补助工资为15 × 出勤天数。参数day表示请假天数。
编写董事类(Director),该类也是Employee的直接子类,并实现了getSubsidy (int day)方法。董事补助工资的计算方法是:如果请假天数小于等于3,则补助工资为30× 出勤天数;如果请假天数超过3天,则补助工资为20 × 出勤天数。参数day表示请假天数。
编写Main类,在该类的main方法中,分别创建一个NormalEmployee和一个Director类的对象,并由用户输入其请假天数来计算他们正常出勤补助工资。
使用抽象的方法编写如下所示:
abstract class Employee1{
protected int id;
protected String sex;
protected String name;
protected String duty;
protected double salary;
protected int holidays;
public void display() {
System.out.println(name+"+"+sex+"+"+duty);
}
public Employee1() {}
public Employee1(int id, String sex, String name, String duty, double salary, int holidays) {
super();
this.id = id;
this.sex = sex;
this.name = name;
this.duty = duty;
this.salary = salary;
this.holidays = holidays;
}
abstract int getSubsidy(int day);
}
class NormalEmployee extends Employee1{
@Override
int getSubsidy(int day) {
if (day > 3) {
salary = 15 * day;
}
else if (day <= 3) {
salary = 20 * day;
}
return (int) salary;
}
}
class Director extends Employee1{
@Override
int getSubsidy(int day) {
if (day > 3) {
salary = 20 * day;
}
else if (day <= 3) {
salary = 30 * day;
}
return (int) salary;
}
}
public class homework4 {
public static void main(String[] args) {
NormalEmployee normalEmployee = new NormalEmployee();
System.out.println(normalEmployee.getSubsidy(5));
Director director = new Director();
System.out.println(director.getSubsidy(10));
}
}
5.第4题使用实例编写如下所示:
class Employee{
protected int id;
protected String sex;
protected String name;
protected String duty;
protected double salary;
protected int holidays;
public void display() {
System.out.println(sex+","+name+","+duty);
}
public Employee() {}
public Employee(int id, String sex, String name, String duty, double salary, int holidays) {
super();
this.id = id;
this.sex = sex;
this.name = name;
this.duty = duty;
this.salary = salary;
this.holidays = holidays;
}
public int getDecMoney(int day) {
if(day > 3) {
salary = salary - day*50;
}else if(day <= 3) {
salary = salary - day*30;
}
return (int)salary;
}
}
class Director extends Employee{
private String assistntName;
public Director(int id, String sex, String name, String duty, double salary, int holidays,String assistntName) {
super(id,sex,name,duty,salary,holidays);
this.assistntName = assistntName;
}
public int getDecMoney(int day) {
if(day > 3) {
salary = salary- day*80;
}else if(day <= 3) {
salary = salary - day*50;
}
return (int)salary;
}
public void display() {
System.out.println(sex+","+name+","+duty+","+assistntName);
}
}
public class TestEmpolyee {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee emp = new Employee(11,"男","郭靖","会计",5000,5);
emp.display();
int n = emp.getDecMoney(emp.holidays);
System.out.println(n);
Director huangrong = new Director(1, "女", "黄蓉", "懂事", 20000, 5, "穆念慈");
huangrong.display();
int m = huangrong.getDecMoney(huangrong.holidays);
System.out.println(m);
}
}