面向對象之抽象類

本篇為Java學習過程中所記筆記,如有不全或錯誤,還請指正

抽象類與抽象方法
abstract關鍵字
1.可用來修飾類、方法
abstract class ***
public abstract void eat();
2.修飾類
    》此類不能實例化
    》抽象類一定有構造器,供子類調用
    》開發中,都會提供抽象類的子類,讓子類實例化完成一定操作
3.修飾方法
    》抽象方法只有方法的聲明,無方法體
    》抽象方法一定在抽象類中,反之不一定
    》若子類沒有重寫父類的抽象方法,則子類也必須為抽象類
 4.abstract使用注意
   1.不能修飾屬性、構造器、代碼塊
   2.不能修飾私有方法、靜態方法、final的方法(這些不能重寫)
以下為樣例:
abstract class Employee{
    private String name;
    private int number;
    private MyDate birthday;

    public void setName(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void setNumber(int number){
        this.number=number;
    }
    public int getNumber(){
        return number;
    }
    public void setBirthday(MyDate birthday){
        this.birthday=birthday;
    }
    public MyDate getBirthday(){
        return birthday;
    }
    public Employee(String name,int number,MyDate birthday){
        super();
        this.name=name;
        this.number=number;
        this.birthday=birthday;
    }

    public abstract double earings();

    public String toString() {
        return "name "+name+",number "+number+",birthday "+birthday.toDateString();
    }
}
class MyDate{
    private int year;
    private int month;
    private int day;
    public void setYear(int year){
        this.year=year;
    }
    public int getYear(){
        return year;
    }
    public void setMonth(int month){
        this.month=month;
    }
    public int getMonth(){
        return month;
    }
    public void setDay(int day){
        this.day=day;
    }
    public int getDay(){
        return day;
    }
    public MyDate(int year,int month,int day){
        super();
        this.year=year;
        this.month=month;
        this.day=day;
    }
    public String toDateString(){
        return year+"年"+month+"月"+day+"日";
    }
}
class SalariedEmployee extends Employee{
    private double monthlySalary;//月工資

    public SalariedEmployee(String name,int number,MyDate birthday){
        super(name,number,birthday);
    }
    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 getMonthlySalary() {
        return monthlySalary;
    }

    public double earings(){

        return monthlySalary;
    }
    public String toString(){
        return "SalariedEmployee["+super.toString()+"]";
    }
}
class HourlyEmployee extends Employee{
    private double wage;
    private double hour;
    public HourlyEmployee(String name,int number,MyDate birthday){
        super(name,number,birthday);
    }
    public HourlyEmployee(String name,int number,MyDate birthday,double wage,double hour){
        super(name,number,birthday);
        this.wage=wage;
        this.hour=hour;
    }

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

    public double getWage() {
        return wage;
    }

    public void setHour(double hour) {
        this.hour = hour;
    }

    public double getHour() {
        return hour;
    }

    public double earings(){
        return wage*hour;
    }
    public String toString(){
        return "HourlyEmployee["+super.toString()+"]";
    }
}
class PayrollSystem{
    public static void main(String[] args) {
        Employee employee[]=new Employee[2];
        int i;
        Scanner scan=new Scanner(System.in);
        System.out.println("請輸入當前月份:");
        int month=scan.nextInt();
        employee[0]=new SalariedEmployee("張三",1002,new MyDate(1992,2,28),10000);
        employee[1]=new HourlyEmployee("李四",2001,new MyDate(1991,5,6),60,240);
        for(i=0;i<employee.length;i++){
            System.out.println(employee[i]);
            double salary=employee[i].earings();
            System.out.println("月工資為:"+salary);
            if(month==employee[i].getBirthday().getMonth()){
                System.out.println("生日快樂!獎勵100元");
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值