Java之抽象类与抽象方法

抽象类与抽象方法

1.abstract可以用来修饰的结构:类、方法

2、abstract修饰类:抽象类

①此类不能实例化;
②抽象类中一定有构造器,便于子类实例化时调用(涉及:子类对象实例化的全过程);
③开发中,都会提供抽象类的子类,让子类对象实例化,完成相关的操作。

3、abstract修饰方法:抽象方法

①抽象方法只有方法的声明,没有方法体(比如:public abstract void run( ););
②包含抽象方法的类,一定是一个抽象类。反之,抽象类中可以没有抽象方法;
③若子类重写了父类中的所有的抽象方法后,此子类方可以进行实例化;若子类没有重写父类中的所有的抽象方法,则子类也是一个抽象类,仍需使用abstract来修饰。

abstract使用上应该注意的点:

(1)不能用来修饰:属性、构造器等结构;
(2)不能用来修饰私有方法、静态方法、final的方法、final的类

(应用举例)以下程序编写出一个工资系统, 实现不同类型员工(多态)的按月发放工资。 如果当月出现某个Employee对象的生日, 则将该雇员的工资增加100元。

package com.java.oop5;

/**
 * @description: 定义一个Employee类,该类包含:
 * private成员变量name,number,birthday,其中birthday 为MyDate类的对象;
 * abstract方法earnings();
 * toString()方法输出对象的name,number和birthday。
 * @author: Fish_Vast
 * @Date: 2021/8/10
 * @version: 1.0
 */
public abstract class Employee {
    private String name;    //员工姓名
    private int number;     //员工号
    private MyDate birthday;    //员工生日

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

    public String getName() {
        return name;
    }


    public int getNumber() {
        return number;
    }


    public MyDate getBirthday() {
        return birthday;
    }

    public abstract int earnings();

    @Override
    public String toString() {
        return "{name='" + name + '\'' +
                ", number=" + number +
                ", birthday=" + birthday +
                '}';
    }
}
package com.java.oop5;

/**
 * @description: private成员变量year, month, day ;
 * toDateString()方法返回日期对应的字符串: xxxx年xx月xx日
 * @author: Fish_Vast
 * @Date: 2021/8/10
 * @version: 1.0
 */
public class MyDate {
    private int year;   //年
    private int month;  //月
    private int day;    //日

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

    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }

    @Override
    public String toString() {
        return   year +"年"+
                 month +"月"+
                 day +"日";
    }
}
package com.java.oop5;

/**
 * @description: 定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处
 * 理。该类包括: private成员变量monthlySalary;
 * 实现父类的抽象方法earnings(),该方法返回monthlySalary值; toString()方法输
 * 出员工类型信息及员工的name, number,birthday。
 * @author: Fish_Vast
 * @Date: 2021/8/10
 * @version: 1.0
 */
public class SalariedEmployee extends Employee {
    private int monthlySalary;  //月工资

    public SalariedEmployee(String name, int number, MyDate birthday, int monthlySalary) {
        super(name, number, birthday);
        this.monthlySalary = monthlySalary;
    }

    public int getMonthlySalary() {
        return monthlySalary;
    }

    public void setMonthlySalary(int monthlySalary) {
        this.monthlySalary = monthlySalary;
    }

    @Override
    public int earnings(){
         return this.monthlySalary;
    }

    @Override
    public String toString() {
        return  super.toString()+"{" +
                "月工资为:" + monthlySalary +
                "} ";
    }
}
package com.java.oop5;

/**
 * @description: 继承Employee类,实现按小时计算工资的
 * 员工处理。该类包括:private成员变量wage和hour;
 * 实现父类的抽象方法earnings(),该方法返回wage*hour值;
 * toString()方法输出员工类型信息及员工的name, number,birthday。
 * @author: Fish_Vast
 * @Date: 2021/8/10
 * @version: 1.0
 */
public class HourlyEmployee extends Employee{
    private int wage;   //每小时的工资
    private int hour;   //工作时长

    public HourlyEmployee(String name, int number, MyDate birthday, int wage, int hour) {
        super(name, number, birthday);
        this.wage = wage;
        this.hour = hour;
    }

    public int getWage() {
        return wage;
    }

    public int getHour() {
        return hour;
    }

    public int earnings(){
        return wage * hour;
    }

    @Override
    public String toString() {
        return  super.toString()+"{" +
                "每小时工资为:" + wage +
                ", 工作时长为:" + hour +
                "} " ;
    }
}
package com.java.oop5;

import java.util.Calendar;
import java.util.Scanner;

/**
 * @description: 创建Employee变量数组并初始化,该数组存放各
 * 类雇员对象的引用。利用循环结构遍历数组元素,输出各个对象的类
 * 型,name,number,birthday。当键盘输入本月月份值时,如果本
 * 月是某个Employee对象的生日,还要输出增加工资信息。
 * @author: Fish_Vast
 * @Date: 2021/8/10
 * @version: 1.0
 */
public class PayrollSystem {
    public static void main(String[] args) {
        //方式一,从键盘获取月份
//        System.out.println("请输入月份:");
//        Scanner scanner = new Scanner(System.in);
//        int month = scanner.nextInt();

        //方式二,直接用Calendar类生成月份
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);//获取当前的月份

        Employee [] emp = new Employee[2];
        //匿名对象 new MyDate()
        //对象数组的引用
        emp[0] = new SalariedEmployee("Seven",1001,new MyDate(1997,2,20),10000);
        emp[1] = new HourlyEmployee("Water",1002,new MyDate(1996,8,10),30,240);
        //数组遍历
        for (int i = 0; i < emp.length; i++) {
            System.out.println(emp[i]);
            int salary = emp[i].earnings();
            System.out.println(emp[i].getName()+"的月工资为:"+salary+"元");

            //获取到的月份,默认一月为0,所以整体的月份要做加1处理
            if( month++ == emp[i].getBirthday().getMonth()){
                System.out.println("生日快乐,奖励一百元");
                System.out.println(emp[i].getName()+"的月工资为:"+(salary+100)+"元");
            }
        }
    }
}

在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fish_Vast

您的打赏是对我最大的支持!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值