Java教程

Java 抽象类 练习

本文主要是介绍Java 抽象类 练习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
 1 package com.bytezreo.template;
 2 
 3 import java.util.Calendar;
 4 import java.util.Scanner;
 5 
 6 /**
 7  * 
 8  * @Description    抽象类 练习
 9  * @author Bytezero·zhenglei!        Email:420498246@qq.com
10  * @version
11  * @date 下午2:50:49
12  * @
13  *
14  */
15 public class PayrollSystem {
16 
17     public static void main(String[] args) {
18         
19         //方式一:
20 //        Scanner scanner = new Scanner(System.in);
21 //        System.out.println("请输入当月的月份:");
22 //        int month = scanner.nextInt();
23         
24         //方式二
25         Calendar calendar = Calendar.getInstance();
26         int month =calendar.get(Calendar.MONTH); //获取当前的月份
27         System.out.println(month+1); //一月份 0
28         
29         
30         
31         
32         Employee[] emps = new Employee[2];
33         
34         emps[0] = new SalariedEmployee("Tom", 1002, new MyDate(2000, 3, 15),10000);
35         emps[1] = new HourlyEmployee("jery", 2001, new MyDate(2001,10, 17),60,240);
36     
37         for(int i =0; i <emps.length;i++) {
38             System.out.println(emps[i]);
39             
40              double salary =emps[i].earnings();
41              System.out.println("月工资为:" + salary);
42              
43              if((month+1) == emps[i].getBirthady().getMonth()) {
44                  System.out.println("生日快乐!奖励100元");
45              }
46              
47              
48         }
49     
50     }
51 }
 1 package com.bytezreo.template;
 2 
 3 /**
 4  * 
 5  * @Description
 6  * @author Bytezero·zhenglei!        Email:420498246@qq.com
 7  * @version
 8  * @date 下午2:23:00
 9  * @
10  *
11  */
12 public abstract class Employee {
13         private String name;
14         private int number;
15         private MyDate birthady;
16         
17     
18         public Employee(String name, int number, MyDate birthady) {
19             super();
20             this.name = name;
21             this.number = number;
22             this.birthady = birthady;
23         }
24 
25         public String getName() {
26             return name;
27         }
28 
29         public void setName(String name) {
30             this.name = name;
31         }
32 
33         public int getNumber() {
34             return number;
35         }
36 
37         public void setNumber(int number) {
38             this.number = number;
39         }
40 
41         public MyDate getBirthady() {
42             return birthady;
43         }
44 
45         public void setBirthady(MyDate birthady) {
46             this.birthady = birthady;
47         }
48 
49         public abstract double earnings();
50 
51         @Override
52         public String toString() {
53             return "name=" + name + ", number=" + number + ", birthady=" + birthady.toDateString();
54         }
55         
56         
57         
58 }
 1 package com.bytezreo.template;
 2 
 3 
 4 public class SalariedEmployee extends Employee{
 5     
 6     private double monthlySalary;  //月工资
 7 
 8     public SalariedEmployee(String name, int number, MyDate birthady) {
 9         super(name, number, birthady);
10         
11     }
12     
13     public SalariedEmployee(String name, int number, MyDate birthady,double monthlySalary) {
14         super(name, number, birthady);
15         this.monthlySalary = monthlySalary;
16     }
17 
18     
19     public double getMonthlySalary() {
20         return monthlySalary;
21     }
22 
23 
24     public void setMonthlySalary(double monthlySalary) {
25         this.monthlySalary = monthlySalary;
26     }
27 
28 
29     @Override
30     public double earnings() {
31         
32         return monthlySalary;
33     }
34 
35     public String toString() {
36         return "SalariedEmployee[ " +super.toString()+"]";
37     }
38 
39 
40 
41     
42     
43 }
 1 package com.bytezreo.template;
 2 
 3 
 4 public class HourlyEmployee extends Employee {
 5 
 6     private int wage;  //每小时的工资
 7     private int hour;  //月工作的小时数
 8     
 9     public HourlyEmployee(String name, int number, MyDate birthady) {
10         super(name, number, birthady);
11         
12     }
13 
14     public HourlyEmployee(String name, int number, MyDate birthady,int wage,int hour) {
15         super(name, number, birthady);
16         this.wage = wage;
17         this.hour = hour;
18     }
19     
20     
21     public int getWage() {
22         return wage;
23     }
24 
25     public void setWage(int wage) {
26         this.wage = wage;
27     }
28 
29     public int getHour() {
30         return hour;
31     }
32 
33     public void setHour(int hour) {
34         this.hour = hour;
35     }
36 
37     @Override
38     public double earnings() {
39         
40         return wage * hour;
41     }
42 
43 
44     public String toString() {
45         return "HourlyEmployee[ " +super.toString()+"]";
46     }
47     
48 }

 

 

 

 

这篇关于Java 抽象类 练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!