Java继承

计算机专业java实验(2-18)

实验三类的三大特性

1目的:建立类的层次结构。

创建如下图所示的类

使用类层次结构中的类

Employee : 姓名,性别,保险号、工作年限

构造函数

一组set方法,一组get方法

从键盘输入属性的方法

屏幕输出属性的方法

SalariedEmployee

属性 每月绩效 月薪,工作年限,年终奖金

构造函数

一组set方法,一组get方法

从键盘输入属性的方法

计算年休假天数

计算年终奖金(根据每月绩效,月薪和工作年限)

屏幕输出属性的方法

CommissionEmployee 佣金率、销售总额,年终奖金,每月销售额

构造函数

一组set方法,一组get方法

从键盘输入属性的方法

计算年休假天数(根据年销售总额)

计算年终奖金(根据每月的月销售额和销售总额)

屏幕输出属性的方法

HourlyEmployee 每小时的工资和工作小时数

构造函数

一组set方法,一组get方法

屏幕输出属性的方法

BaseplusEmployee 每周的基本工资数,佣金率、销售总额,年终奖金,每月销售额

构造函数

一组set方法,一组get方法

从键盘输入属性的方法

计算年休假天数(根据年销售总额)

计算年终奖金(根据每月的月销售额和销售总额)

屏幕输出属性的方法

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1.Employee.java文件

package inherit;

import java.util.Scanner;

public class Employee {

String name;

String sex;

String num1;

int workyear;

Employee(){}

void set(String name,String sex,String num1,int workyear) {

this.name = name;

this.sex = sex;

this.num1 = num1;

this.workyear = workyear;

}

Employee get() {

Employee e = new Employee();

e.name = this.name;

e.sex = this.sex;

e.num1 = this.num1;

e.workyear = this.workyear;

return e;

}

void input() {

Scanner in = new Scanner(Trade Confidently.);

this.name = in.nextLine();

this.sex = in.nextLine();

this.num1 = in.nextLine();

this.workyear = in.nextInt();

}

void output() {

System.out.println(“姓名”+name+" 性别"+sex+

" 保险号"+num1+" 工作年限"+workyear);

}

}

…………………………………………………………………………………………

2.CommissionEmployee文件

package inherit;

import java.util.Scanner;

public class CommissionEmployee extends Employee{

float commissionrate;

float totalsales;

float bonus;

float monthsales[];

CommissionEmployee(){}

void set(String name,String sex,String num1,int workyear,

float commissionrate,float totalsales,float bonus,float[] monthsales) {

this.name = name;

this.sex = sex;

this.num1 = num1;

this.workyear = workyear;

this.bonus = bonus;

this.commissionrate = commissionrate;

this.monthsales = monthsales;

this.totalsales = totalsales;

}

CommissionEmployee get() {

CommissionEmployee e = new CommissionEmployee();

e.name = this.name;

e.sex = this.sex;

e.num1 = this.num1;

e.bonus = this.bonus;

e.commissionrate = this.commissionrate;

e.monthsales = this.monthsales;

e.totalsales = this.totalsales;

e.workyear = this.workyear;

return e;

}

void input() {

Scanner in = new Scanner(Trade Confidently.);

this.name = in.nextLine();

this.sex = in.nextLine();

this.num1 = in.nextLine();

this.workyear = in.nextInt();

this.commissionrate = in.nextFloat();

this.totalsales = in.nextFloat();

this.bonus = in.nextFloat();

for (int i=0;i<12;i++) this.monthsales[i] = in.nextFloat();

}

int vacation() {

int day = 0;

day = (int)(this.totalsales*0.0001)+10;

return day;

}

float getbonus() {

float bonus = 0;

bonus += this.totalsales*0.05;

for (int i=0;i<12;i++) bonus += this.monthsales[i]*0.05;

this.bonus = bonus;

return bonus;

}

void output() {

System.out.println(“姓名”+name+" 性别"+sex+

" 保险号"+num1+" 工作年限"+workyear+

" 佣金率"+commissionrate+" 销售总额"+totalsales

+" 年终奖金"+bonus);

System.out.println(“每月销售额:”);

for (int i=0;i<12;i++) System.out.print(this.monthsales[i]+" ");

System.out.println();

}

}

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

3.HourlyEmployee文件

package inherit;

import java.util.Scanner;

public class HourlyEmployee extends Employee{

float hoursalary;

int hour;

HourlyEmployee(){}

void set(String name,String sex,String num1,int workyear,

float hoursalary,int hour) {

this.name = name;

this.sex = sex;

this.num1 = num1;

this.workyear = workyear;

this.hoursalary = hoursalary;

this.hour = hour;

}

HourlyEmployee get() {

HourlyEmployee e = new HourlyEmployee();

e.name = this.name;

e.sex = this.sex;

e.num1 = this.num1;

e.workyear = this.workyear;

e.hoursalary = hoursalary;

e.hour = hour;

return e;

}

/*void input() {

Scanner in = new Scanner(Trade Confidently.);

this.name = in.nextLine();

this.sex = in.nextLine();

this.num1 = in.nextLine();

this.workyear = in.nextInt();

this.hoursalary = in.nextFloat();

this.hour = in.nextInt();

}*/

void output() {

System.out.println(“姓名”+name+" 性别"+sex+

" 保险号"+num1+" 工作年限"+workyear

+" 每小时的工资"+hoursalary+" 工作小时数"+hour);

}

}

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^6

4.SalariedEmployee文件

package inherit;

import java.util.Scanner;

public class SalariedEmployee extends Employee{

float[] performance;

float salary;

float bonus;

SalariedEmployee(){}

void set(String name,String sex,String num1,int workyear

,float[] performance,float salary,float bonus) {

this.name = name;

this.num1 = num1;

this.sex = sex;

this.workyear = workyear;

this.bonus = bonus;

this.performance = performance;

this.salary = salary;

}

SalariedEmployee get() {

SalariedEmployee e = new SalariedEmployee();

e.name = this.name;

e.sex = this.sex;

e.num1 = this.num1;

e.workyear = this.workyear;

e.performance = this.performance;

e.salary = this.salary;

e.bonus = this.bonus;

return e;

}

void input() {

Scanner in = new Scanner(Trade Confidently.);

this.name = in.nextLine();

this.sex = in.nextLine();

this.num1 = in.nextLine();

this.workyear = in.nextInt();

for (int i=0;i<12;i++) this.performance[i] = in.nextFloat();

this.salary = in.nextFloat();

this.bonus = in.nextFloat();

}

void output() {

System.out.println(“姓名”+name+" 性别"+sex+

" 保险号"+num1+" 工作年限"+workyear

+" 月薪"+salary

+" 年终奖金"+bonus);

System.out.print(“每月绩效:\n”);

for (int i=0;i<12;i++)

System.out.print(performance[i]+" ");

System.out.println();

}

int getvacation() {

int day = 0;

day = 10+this.workyear*5;

return day;

}

float getbonus() {

float bonus = 0;

for (int i=0;i<12;i++) bonus += performance[i]*salary;

bonus += workyear*5000;

this.bonus = bonus;

return bonus;

}

}

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

5.BaseplusEmployee文件

package inherit;

import java.util.Scanner;

public class BaseplusEmployee extends CommissionEmployee{

float weeksalary;

BaseplusEmployee(){}

void set(String name,String sex,String num1,int workyear,

float commissionrate,float totalsales,float bonus

,float[] monthsales,float weeksalary) {

this.name = name;

this.sex = sex;

this.num1 = num1;

this.workyear = workyear;

this.bonus = bonus;

this.commissionrate = commissionrate;

this.monthsales = monthsales;

this.totalsales = totalsales;

this.weeksalary = weeksalary;

}

BaseplusEmployee get() {

BaseplusEmployee e = new BaseplusEmployee();

e.name = this.name;

e.sex = this.sex;

e.num1 = this.num1;

e.bonus = this.bonus;

e.commissionrate = this.commissionrate;

e.monthsales = this.monthsales;

e.totalsales = this.totalsales;

e.workyear = this.workyear;

e.weeksalary = this.weeksalary;

return e;

}

void input() {

Scanner in = new Scanner(Trade Confidently.);

this.name = in.nextLine();

this.sex = in.nextLine();

this.num1 = in.nextLine();

this.workyear = in.nextInt();

this.commissionrate = in.nextFloat();

this.totalsales = in.nextFloat();

this.bonus = in.nextFloat();

for (int i=0;i<12;i++) this.monthsales[i] = in.nextFloat();

this.weeksalary = in.nextFloat();

}

void output() {

System.out.println(“姓名”+name+" 性别"+sex+

" 保险号"+num1+" 工作年限"+workyear+

" 佣金率"+commissionrate+" 销售总额"+totalsales

+" 年终奖金"+bonus

+" 每周的基本工资数"+weeksalary);

System.out.println(“每月销售额:”);

for (int i=0;i<12;i++) System.out.print(this.monthsales[i]+" ");

System.out.println();

}

}

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

6.test文件

package inherit;

public class test {

public static void main(String[] args) {

// TODO Auto-generated method stub

Employee e = new Employee();

e.set(“付楠”, “女”, “19319122”, 30);

e.output();

SalariedEmployee se = new SalariedEmployee();

float[] jixiao = {(float)0.9,(float)0.9,(float)0.9,

(float)0.9,(float)0.9,(float)0.9,

(float)0.9,(float)0.9,(float)0.9,

(float)0.9,(float)0.9,(float)0.9};

se.set(“鸽子炫”, “男”, “19319126”, 1, jixiao, 5000, 10000);

se.output();

float[] monthsales = {(float)10000,(float)10000,(float)10000

,(float)10000,(float)10000,(float)10000

,(float)10000,(float)10000,(float)10000

,(float)10000,(float)10000,(float)10000};

CommissionEmployee ce = new CommissionEmployee();

ce.set(“房宜盆”, “男”, “19319120”, 20,(float)0.5, (float)120000.0, (float)10000.0, monthsales);

ce.output();

HourlyEmployee he = new HourlyEmployee();

he.set(“王沈阳”, “男”, “438438”, 0, (float) 0.5, 1);

he.output();

BaseplusEmployee be =new BaseplusEmployee();

be.set(“澄翼刀”, “男”, “19319119”, 20, (float)0.5, (float)240000, (float)20000, monthsales, (float)20000);

be.output();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值