java关于类的多态特性(雇员工资问题)

内容:

1. 定义抽象类Employee。

2. 继承雇员类定义类Manager,Salesman,Worker。

3. 建立主函数,使用这些类。

4.继承Worker类定义计时雇员类(DailyWorker)和计件雇员类(PieceWorker), 计时雇员按天计算工资额(每天100元),计件雇员按件计算工资额(每件1元)

代码实现:

import java.util.*;
abstract class Employee
{
	public int sum;
    abstract void work();

}
class Manager
{
	void work()
	{
		System.out.println("经理");
	}
}
class Salesman extends Employee
{
	void work()
	{
		System.out.println("销售");
	}
}
class Worker extends Employee
{
	void work()
	{
		System.out.println("职员");
	}
}

class DailyWorker
{
	int day;
	int dailymoney=100;
	int summoney;
	public int Getday()
	{
		return this.day;
	}
	public void Setday(int day)
	{
		this.day=day;
		summoney=dailymoney*day;
		System.out.println("DailyWorker的工资为:"+summoney);
	}

}
class PieceWorker extends Worker
{
	int count;
	int summoney;
	public  int Getcount()
	{
		return this.count;
	}
	public void Setcount(int count)
	{
		this.count=count;
		summoney=count;
		System.out.println("PieceWorker的工资为:"+summoney);
	}

		
	
}

public class test5 {
        
    public test5() {
    }
    public static void main(String[] args) {
        Manager p1 = new Manager();
        p1.work();
        Salesman p2 = new Salesman();
        p2.work();
        Worker p3 = new Worker();
        p3.work();
        PieceWorker p4 = new PieceWorker();
        p4.Setcount(666);
        System.out.println("count="+p4.Getcount());       
        DailyWorker p5 = new DailyWorker();
        p5.Setday(66);
        System.out.println("day="+p5.Getday());
       
        
    }
}

结果:java关于类的多态特性(雇员工资问题)_第1张图片

 

你可能感兴趣的:(java学习,java)