封装继承相关的两个小练习(3)

1.

定义一个父类:形状类Shapes,里面有两个方法,分别是求面积和周长。

定义一个子类:矩形Rectangle

定义一个子类:圆 Circle

定义一个测试类:传入圆的半径4 输出周长和面积

传入矩形的长和宽4,5 输出周长和面积

public class Shapes {
//    方法
    public void square() {

    }

    public void round() {

    }
}
public class Circle extends Shapes{
//    属性
    private double Π=3.1415926;
    private double radius;
    
//    构造方法
    public Circle() {
        
    }
    
    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }
    
//  方法
    @Override
    public void square() {
        double square=Π*getRadius()*getRadius();
        System.out.println("圆形的面积为:"+square);
    }
    
    @Override
    public void round() {
        double round=2*Π*getRadius();
        System.out.println("圆形的周长为:"+round);
    }
}
public class Rectangle extends Shapes {
//    属性
    private double height;
    private double width;

//    构造方法
    public Rectangle() {

    }

    public Rectangle(double height, double width) {
        this.height = height;
        this.width = width;
    }

//    封装属性
    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

//    方法
    @Override
    public void square() {
        double square = getHeight() * getWidth();
        System.out.println("矩形的面积为:" + square);
    }

    @Override
    public void round() {
        double round = 2 * (getHeight() + getWidth());
        System.out.println("矩形的面积为:" + round);
    }
}
public class Test {
    public static void main(String[] args) {
//        实例化并测试方法
        Circle a = new Circle(4);
        Rectangle b = new Rectangle(4, 5);

        a.square();
        a.round();
        b.square();
        b.round();
    }
}

2.

编写工资系统,实现不同类型员工的按月发放工资。

如果员工的生日的月份+日期=26.则该员工为幸运员工,工资加500

(1)定义一个Employee类,

该类包含: private成员变量name,number(工号),birthday,

其中birthday为MyDate类的对象;

方法earnings(); --获取工资

(2)MyDate类包含:

private成员变量year,month,day ;

重写toString()方法返回日期对应的字符串:xxxx年xx月xx日

(3)定义SalariedEmployee类继承Employee类,实现按月固定计算工资的员工处理。

该类包括:private成员变量monthlySalary; --月薪

重写写方法earnings(),该方法返回该员工工资;

(4)定义HourlyEmployee类,实现按小时计算工资的员工处理。

该类包括: private成员变量wage(时薪)和hour(工作的小时数);

重写方法earnings(),该方法返回该员工工资;

(5)定义测试类,在测试类中定义各个类型对象并测试

public class Employee {
//    属性
    private String name;
    private String num;
    private MyDate birthday;
    
    
//    构造方法
    public Employee() {

    }
    
    public Employee(String name, String num, MyDate birthday) {
        this.name = name;
        this.num = num;
        this.birthday = birthday;
    }

//    封装属性
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }
    
//    方法
    public void earnings() {
    
    }
}
public class MyDate {
//    属性
    private int year;
    private int month;
    private int day;
    
//    构造方法
    public MyDate() {

    }
    
    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

//    封装属性
    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }
    
//    方法
    @Override
    public String toString() {
        return getYear()+"年"+getMonth()+"月"+getDay()+"日";
    }
}
public class HourlyEmployee extends Employee{
//    属性
    private double wage;
    private double hour;
    
//    构造方法
    public HourlyEmployee() {

    }

    public HourlyEmployee(double wage, double hour,MyDate birthday) {
        this.wage = wage;
        this.hour = hour;
        setBirthday(birthday);
    }

//    封装属性
    public double getWage() {
        return wage;
    }

    public void setWage(double wage) {
        this.wage = wage;
    }

    public double getHour() {
        return hour;
    }

    public void setHour(double hour) {
        this.hour = hour;
    }
    
 
//    方法
    @Override
    public void earnings() {
        if(getBirthday().getMonth()+getBirthday().getDay()==26) {
            System.out.println("恭喜您为幸运员工,获得500元");
            System.out.println("您的时薪为"+getWage()+"元,您总共工作"+getHour()+"小时,一共"+(getWage()*getHour()+500)+"元,请收好。");
        }else {
        System.out.println("您的时薪为"+getWage()+"元,您总共工作"+getHour()+"小时,一共"+getWage()*getHour()+"元,请收好。");
        }
    }
public class SalariedEmployee extends Employee {
//    属性
    private double monthlySalary;

//    构造方法
    public SalariedEmployee() {

    }

    public SalariedEmployee(double monthlySalary,MyDate birthday) {
        this.monthlySalary = monthlySalary;
        setBirthday(birthday);
    }


//    封装属性
    public double getMonthlySalary() {
        return monthlySalary;
    }

    public void setMonthlySalary(double monthlySalary) {
        this.monthlySalary = monthlySalary;
    }
    
  
//    方法
    @Override
    public void earnings() {
        if(getBirthday().getMonth()+getBirthday().getDay()==26) {
            System.out.println("恭喜您为幸运员工,获得500元");
            System.out.println("您的月薪为"+(getMonthlySalary()+500)+"元,请收好。");
        }else {
        System.out.println("您的月薪为"+getMonthlySalary()+"元,请收好。");
        }
    }
}
public class Test {

    public static void main(String[] args) {
//    实例化并测试
        MyDate a = new MyDate(2000, 1, 25);

        HourlyEmployee c = new HourlyEmployee(30, 50, a);
        c.earnings();

        SalariedEmployee e = new SalariedEmployee(20000, a);
        e.earnings();

        System.out.println("您的生日为" + a.toString());

    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值