java-抽象方法与抽象类

abstract:

1、用来修饰类,此类被称为抽象类。且此类不能够实例化,包含构造器的,因为子类对象实例化时,需要调用父类的构造器。可以没有抽象方法。

2、此方法称为抽象方法,此方法只有方法的声明,没有方法体。

其功能是确定的,通过方法的声明来确定,只是不知道功能如何实现(体现为方法体)

子类必须重写父类所有方法,方可实例化。否则仍是抽象类。

abstract不能修饰私有方法、static方法、final方法、final类

*私有方法不能重写,abstract则必须重写

*静态方法允许调用,而abstract的父类方法不能调用

*final方法不能重写,abstract则必须重写

*final类不能有子类,abstract有子类

例题:

Employee类:

public abstract class Employee {
    private String name;
    private int number;
    private Mydate birthday;

    public Employee() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public Mydate getBirthday() {
        return birthday;
    }

    public void setBirthday(Mydate birthday) {
        this.birthday = birthday;
    }

    public abstract double earning();
    public String toString(){
        return "姓名,"+name+"数字,"+number+"生日"+birthday.toDateString();
    }
}

Mydate类:

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;
    }

    public String toDateString(){
        return year+"年"+month+"月"+day+"日";
    }
}

SalariedEmployee类:

public class SalariedEmployee extends Employee{
    private double monthlySalary;

    public SalariedEmployee() {
    }

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


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

    public double earning(){
        return monthlySalary;
    }
    public String toString(){
        return "SalariedEmployee[" + super.toString()+"]";
    }

}

HourlyEmployee类:

public  class HourlyEmployee extends Employee {
    private double wage;
    private int hour;

    public HourlyEmployee() {
    }

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

    public double getWage() {
        return wage;
    }

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

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }
    public  double earning(){
        return wage*hour;
    }
    public String toString(){
        return "HourlyEmployee[" + super.toString()+"]";
    }
}

PayrollSystem类:

import java.util.Scanner;

public class PayrollSystem {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        Employee[] e=new Employee[2];
        e[0]=new SalariedEmployee("马国森", 123, new Mydate(2001, 1, 1),5000);
        e[1]=new HourlyEmployee("王悦",123,new Mydate(2001,2,2),32,5);
        System.out.println("请输入月份");
        int input=scanner.nextInt();
        for (int i = 0; i < e.length; i++) {
            System.out.println(e[i].toString());
            System.out.println("工资:"+e[i].earning());

            if (input == e[i].getBirthday().getMonth()) {
                System.out.println("生日快乐,加薪200!");
            }
        }
        scanner.close();




    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值