• JAVA基础篇—多态


    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
  • 相关阅读:
    Detect loop in a singly linked list
    Partition an array around an interger
    Binary search for the first element greater than target
    Searching in a rotated and sorted array
    where, group by, having
    [scalability] Find all documents that contain a list of words
    [DP] 堆盒子问题
    cocos2dx 内存管理的理解
    cocos2dx 2.x版本:简化提炼tolua++绑定自定义类到lua中使用
    OpenGl从零开始之坐标变换(下)
  • 原文地址:https://www.cnblogs.com/lc-java/p/7392454.html
Copyright © 2020-2023  润新知