JAVA继承、多态练习

package PersonSalary;

abstract class Person {
	private int ID;
	private String name;
	private double salary;
	int holidays;
	public Person(){
		
	}
	public Person(int ID,String name,double salary,int holidays){
		this.ID = ID;
		this.name = name;
		this.salary = salary;
		this.holidays = holidays;
	}
	public double CalculateSalary(){
		if(holidays==0)
			return salary;
		else if(holidays>0&&holidays<5)
			return salary*0.75;
		else
			return salary*0.5;
	}
}


package PersonSalary;

public class Employee extends Person {
	public Employee(){
		super();
	}
	public Employee(int ID,String name,double salary,int holidays){
		super(ID,name,salary,holidays);
	}
	public double CalculateSalary(){
		return super.CalculateSalary()+super.CalculateSalary()*0.1
                       +super.CalculateSalary()*0.3;
	}
}


package PersonSalary;

public class Manager extends Person {
	public Manager(){
		super();
	}
	public Manager(int ID,String name,double salary,int holidays){
		super(ID,name,salary,holidays);
	}
	public double CalculateSalary(){
		return super.CalculateSalary()+super.CalculateSalary()*0.2+
                       super.CalculateSalary()*0.5+500;
	}
}


package PersonSalary;

public class Director extends Person 
{
	public Director(){
		super();
	}
	public Director(int ID,String name,double salary,int holidays){
		super(ID,name,salary,holidays);
	}
	public double CalculateSalary(){
		return super.CalculateSalary()+super.CalculateSalary()*0.2
                       +super.CalculateSalary()*0.5+500;
	}
}


package PersonSalary;

import java.util.*;

public class Test {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		System.out.println("请输入ID:");
		int ID = reader.nextInt();
		System.out.println("请输入姓名:");
		String name = reader.next();
		System.out.println("请输入工资:");
		double salary = reader.nextDouble();
		System.out.println("请输入请假天数:");
		int holidays = reader.nextInt();
		System.out.println("请选择职业:");
		System.out.println("1.普通员工    2.经理    3.董事");
		int choose = reader.nextInt();
		if(choose==1){
			Employee TCL = new Employee(ID,name,salary,holidays);
			System.out.println(name+"的工资为:"+TCL.CalculateSalary());
		}else if(choose==2){
			Manager TCL = new Manager(ID,name,salary,holidays);
			System.out.println(name+"的工资为:"+TCL.CalculateSalary());
		}else{
			Director TCL = new Director(ID,name,salary,holidays);
			System.out.println(name+"的工资为:"+TCL.CalculateSalary());
		}
	}
}

你可能感兴趣的:(【JAVA】)