JAVA基础篇—多态
2024-10-21 15:29:51
class ColaEmployee父类
package com.cola; public class ColaEmployee {
private String name;
private int bmonth; public ColaEmployee(String name,int bmonth){
this.setName(name);
this.setBmonth(bmonth);
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getBmonth() {
return bmonth;
} public void setBmonth(int bmonth) {
this.bmonth = bmonth;
} public int getSalary(int month){
if (this.getBmonth()==month) {
return ; }
else {
return ;
}
}
public void print(){
System.out.print("信息"+"姓名"+this.getName()+" 月份"+this.getBmonth());
} }
ColaEmployee
class SalariedEmployee
package com.cola; public class SalariedEmployee extends ColaEmployee{
private int salary;
public SalariedEmployee(String name, int bmonth,int salary) {
super(name, bmonth);
// TODO Auto-generated constructor stub
setSalary(salary);
}
public int getSalary(int month) {
return this.salary+super.getSalary(month);
}
public void setSalary(int salary) {
this.salary = salary;
} }
SalariedEmployee
class HourlyEmployee
package com.cola; public class HourlyEmployee extends ColaEmployee{
private int salary;
private int hour;
public HourlyEmployee(String name,int bmonth,int salary,int hour){
super(name, bmonth);
setSalary(salary);
setHour(hour); }
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getSalary(int month) {
if(hour>){
return (int) (this.salary*+(this.hour-)*this.salary*1.5+super.getSalary(month));
} else{
return (int) (this.salary*this.hour+super.getSalary(month));
}
}
}
HourlyEmployee
class SalesEmployee
package com.cola; public class SalesEmployee extends ColaEmployee{
private int sale;
private float commission;
public SalesEmployee(String name,int bmonth,int sale,float commission){
super(name,bmonth);
setSale(sale);
setCommission(commission);
}
public int getSale() {
return sale;
}
public void setSale(int sale) {
this.sale = sale;
}
public float getCommission() {
return commission;
}
public void setCommission(float commission) {
this.commission = commission;
} public int getSalary(int month) {
return (int)(this.sale*this.commission+super.getSalary(month));
} }
SalesEmployee
class Company
package com.cola; public class Company extends ColaEmployee{ public Company(String name, int bmonth) {
super(name, bmonth);
// TODO Auto-generated constructor stub
} public void print(){
System.out.print(this.getName()+this.getBmonth());
} }
Company
class TestEmployee
package com.cola; public class TestEmployee extends ColaEmployee{
public TestEmployee(String name, int bmonth) {
super(name, bmonth);
// TODO Auto-generated constructor stub
} public static void main(String[] args) {
SalariedEmployee a=new SalariedEmployee("����",,);
a.print();
System.out.println(a.getSalary());
HourlyEmployee a1=new HourlyEmployee("����",,,);
a1.print();
System.out.println(a1.getSalary());
SalesEmployee a2=new SalesEmployee("����",,, 0.2f);
a2.print();
System.out.println(a2.getSalary()); }
}
TestEmployee
最新文章
- Javascript图片预加载详解
- Semaphore(计数信号量)
- jQuery事件笔记
- symfony2 twig模板引擎
- Javascript字数统计
- include指令和include标签的区别
- CSS3常用选择器(一)
- OpenVirteX 安装
- C#:WiFi
- HTTP协议请求方式: 中GET、POST和HEAD的介绍以及错误提示码
- 聊聊 Tomcat 的单机多实例
- 【HNOI2017】影魔
- C#利用Attribute实现简易AOP介绍
- postgres 数据库的安装
- #宽带选择# V2EX讨论
- .NET-记一次架构优化实战与方案-目录
- 阿里云三台CentOS7.2配置安装CDH5.12
- python网络爬虫笔记(三)
- Java 之递归遍历目录
- SpringBoot-整合@transactional注解