码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA基础篇—多态

时间:2017-08-18 23:49:12      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:stat   logs   gif   img   float   eth   src   employee   ext   

class ColaEmployee父类

技术分享
 1 package com.cola;
 2 
 3 public class ColaEmployee {
 4     private  String name;
 5     private  int bmonth;
 6     
 7     public ColaEmployee(String name,int bmonth){
 8         this.setName(name);   
 9         this.setBmonth(bmonth); 
10     }
11     
12      public String getName() {
13         return name;
14     }
15 
16     public void setName(String name) {
17         this.name = name;
18     }
19 
20     public int getBmonth() {
21         return bmonth;
22     }
23 
24     public void setBmonth(int bmonth) {
25         this.bmonth = bmonth;
26     }
27 
28     public int getSalary(int month){
29          if (this.getBmonth()==month) {
30             return 100;
31             
32         }
33          else {
34             return 0;
35         }
36      }
37     public  void print(){ 
38         System.out.print("信息"+"姓名"+this.getName()+"   月份"+this.getBmonth());  
39     }
40     
41 }
ColaEmployee

class SalariedEmployee

技术分享
 1 package com.cola;
 2 
 3 public class SalariedEmployee extends ColaEmployee{
 4     private int salary;
 5     public SalariedEmployee(String name, int bmonth,int salary) {
 6         super(name, bmonth);
 7         // TODO Auto-generated constructor stub
 8         setSalary(salary); 
 9     }
10     public int getSalary(int month) {
11         return this.salary+super.getSalary(month);
12     }
13     public void setSalary(int salary) {
14         this.salary = salary;
15     }
16 
17     
18     
19 }
SalariedEmployee

class HourlyEmployee

技术分享
 1 package com.cola;
 2 
 3 public class HourlyEmployee extends ColaEmployee{
 4     private int salary;
 5     private int hour;
 6      public HourlyEmployee(String name,int bmonth,int salary,int hour){
 7          super(name, bmonth);
 8          setSalary(salary); 
 9          setHour(hour); 
10          
11      }
12     public int getSalary() {
13         return salary;
14     }
15     public void setSalary(int salary) {
16         this.salary = salary;
17     }
18     public int getHour() {
19         return hour;
20     }
21     public void setHour(int hour) {
22         this.hour = hour;
23     }
24     public int getSalary(int month) {  
25         if(hour>160){    
26             return  (int)  (this.salary*160+(this.hour-160)*this.salary*1.5+super.getSalary(month));   
27             }   else{      
28                 return (int) (this.salary*this.hour+super.getSalary(month));   
29                 }   
30         } 
31     }
HourlyEmployee

class SalesEmployee

技术分享
 1 package com.cola;
 2 
 3 public class SalesEmployee extends ColaEmployee{
 4     private int sale;  
 5     private float commission;  
 6     public SalesEmployee(String name,int bmonth,int sale,float commission){ 
 7         super(name,bmonth);   
 8         setSale(sale);   
 9         setCommission(commission);  
10         }
11     public int getSale() {
12         return sale;
13     }
14     public void setSale(int sale) {
15         this.sale = sale;
16     }
17     public float getCommission() {
18         return commission;
19     }
20     public void setCommission(float commission) {
21         this.commission = commission;
22     }
23      
24     public int getSalary(int month) {     
25         return (int)(this.sale*this.commission+super.getSalary(month));     
26     }
27 
28     
29 }
SalesEmployee

class Company

技术分享
 1 package com.cola;
 2 
 3 public class Company extends ColaEmployee{
 4 
 5     
 6 
 7     public Company(String name, int bmonth) {
 8         super(name, bmonth);
 9         // TODO Auto-generated constructor stub
10     }
11 
12     public  void print(){    
13         System.out.print(this.getName()+this.getBmonth());  
14         }
15    
16 }
Company

class TestEmployee

技术分享
 1 package com.cola;
 2 
 3 public class TestEmployee extends ColaEmployee{
 4     public TestEmployee(String name, int bmonth) {
 5         super(name, bmonth);
 6         // TODO Auto-generated constructor stub
 7     }
 8 
 9     public static void main(String[] args) {   
10         SalariedEmployee a=new SalariedEmployee("????",2,3000);   
11         a.print();    
12         System.out.println(a.getSalary(3));    
13         HourlyEmployee a1=new HourlyEmployee("????",2,10,200);   
14         a1.print();    
15         System.out.println(a1.getSalary(2));    
16         SalesEmployee a2=new SalesEmployee("????",2,10000, 0.2f);   
17         a2.print();    
18         System.out.println(a2.getSalary(2));    
19           
20         }
21 }
TestEmployee

 

JAVA基础篇—多态

标签:stat   logs   gif   img   float   eth   src   employee   ext   

原文地址:http://www.cnblogs.com/lc-java/p/7392454.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!