抽象类的使用

B.抽象类的使用
Time Limit: 1000 MSMemory Limit: 32768 K
Total Submit: 26 (20 users)Total Accepted: 17 (17 users)Special Judge: No
Description
先定义一个抽象Teacher类,有求工资的computeSalary抽象方法(返回值为薪酬);
 然后定义一个从Teacher派生的教授Professor类,有基本工资5000元,每学时补贴70元;从Teacher派生的副教授ViceProfessor类,有基本工资4500元,每学时补贴60元;从Teacher派生的副教授Lecturer类,有基本工资4000元,每学时补贴50元。他们都有重写的computeSalary抽象方法。
 薪酬的计算方法是:基本工资+每学时补贴*时间
 要求main方法中至少包含如下代码(这些语句不要求必须放在一起):
 Teacher t;
 t=new Professor(r);
 t=new ViceProfessor(r); 
 t=new Lecturer(r); 
 t.computeSalary();
Input
有N组数据。每组数据由一个字符、一个字符串和一个整型数组成,第一个如何是'P'表示Professor;是'V'表示ViceProfessor;是'L'表示Lecturer。后面跟的是教师名,然后是时间,单位为小时,是整数。
Output
教师的薪酬。参见样例。
Sample Input
P zhansan 20
V lisi 20
L wangwu 20 
L aliu   30
Sample Output
zhansan:6400
lisi:5700
wangwu:5000
aliu:5500



import java.util.*;

abstract class teacher
{
	public abstract int computeSalary();
}

class Professor extends teacher
{
	int num;
	String name;
	public Professor(String name,int num)
	{
		this.name = name;
		this.num = num;
	}
	
	public int computeSalary() {
		int ans;
		ans = 5000+70*num;
		return ans;
	}
	public String getname()
	{
		return name;
	}
}

class ViceProfessor extends teacher
{
	int num;
	String name;
	public ViceProfessor(String name,int num)
	{
		this.name = name;
		this.num = num;
	}
	
	public int computeSalary() {
		int ans;
		ans = 4500+60*num;
		return ans;
	}
	public String getname()
	{
		return name;
	}
}

class Lecturer extends teacher
{
	int num;
	String name;
	public Lecturer(String name,int num)
	{
		this.name = name;
		this.num = num;
	}
	
	public int computeSalary() {
		int ans;
		ans = 4000+50*num;
		return ans;
	}
	public String getname()
	{
		return name;
	}
}

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		while(input.hasNext())
		{
			String s = input.next();
			char c = s.charAt(0);
			String ss = input.next();
			int age = input.nextInt();
			teacher tea;
			if(c == 'P')
			{
				tea = new Professor(ss,age);
				System.out.printf("%s:%d\n",ss,tea.computeSalary());
			}
			if(c == 'V')
			{
				tea = new ViceProfessor(ss,age);
				System.out.printf("%s:%d\n",ss,tea.computeSalary());
			}
			if(c == 'L')
			{
				tea = new Lecturer(ss,age);
				System.out.printf("%s:%d\n",ss,tea.computeSalary());
			}
		}
		
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值