131 - 员工类

Description

假定要为某个公司编写雇员工资支付程序,这个公司有各种类型的雇员(Employee),不同类型的雇员按不同的方式支付工资(都是整数):
(1)经理(Manager)——每月获得一份固定的工资
(2)销售人员(Salesman)——在基本工资的基础上每月还有销售提成
(3)一般工人(Worker)——则按他每月工作的天数计算工资
在Employee中提供函数getSalary()计算每个雇员一个月的工资,并在子类中重写。

在main函数中构造Employee的三个变量,分别指向Manager、Salesman、Worker的对象,调用getSalary函数,输出三个对象的工资
import java.util.Scanner;

public class Main {
	public static void main(String [] args) {
		Scanner sc = new Scanner(System.in);
		double a = sc.nextDouble();
		Manager manager = new Manager(a);
		//System.out.println();
		System.out.println((int)manager.getSalary());
		
		double b = sc.nextDouble();
		double c = sc.nextDouble();
		Salesman salesman = new Salesman(b,c);
		System.out.println((int)salesman.getSalary());

		int d = sc.nextInt();
		double e = sc.nextDouble();
		
		Worker worker = new Worker(d,e);
		System.out.println((int)worker.getSalary());
		
		sc.close();
	}
}

class Employee {
	double Salary;
	public double getSalary() {
		return 0;
	};

}

class Manager extends Employee{
	
	double Salary; 
	public Manager(double Salary) {
		//super(name);
		this.Salary = Salary;
		// TODO Auto-generated constructor stub
	}

	@Override
	public double getSalary() {
		// TODO Auto-generated method stub
		return Salary;
	}
	
}

class Salesman extends Employee{
	
	double baseSalary;
	double extrapay;
	public Salesman(double baseSalary,double extrapay) {
		//super(name);
		this.baseSalary = baseSalary;
		this.extrapay = extrapay;
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public double getSalary() {
		// TODO Auto-generated method stub
		return baseSalary + extrapay;
	}
	
}

class Worker extends Employee {

	int day;
	double daypay;
	public Worker(int day,double daypay) {
		//super(name);
		this.day = day;
		this.daypay = daypay;
		// TODO Auto-generated constructor stub
	}

	@Override
	public double getSalary() {
		// TODO Auto-generated method stub
		return day*daypay;
	}
	
}

 

你可能感兴趣的:(Java,Java,入门)