abstract class Person{
創(chuàng)新互聯(lián)成立于2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站制作、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元西充做網(wǎng)站,已為上家服務(wù),為西充各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
private String name;
private String post;
Person(String name,String post){
this.name = name;
this.post = post;
}
public String getID(){
return (name+""+post);
}
public abstract double counting();
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setPost(String post){
this.post = post;
}
public String getPost(){
return this.post;
}
}
class Leader extends Person{
Leader(String name,String post){
super(name,post);
}
public double counting(){
return 3000d;
}
}
class Management extends Person{
private double basicWage=0d; //基本工資
private double allowance=0d; //津貼
Management(String name,String post){
super(name,post);
}
public void setBasicWage(double basicWage){
this.basicWage = basicWage;
}
public double getBasicWage(){
return this.basicWage;
}
public void setAllowance(double allowance){
this.allowance = allowance;
}
public double getAllowance(){
return this.allowance;
}
public double counting(){
return this.basicWage+this.allowance;
}
}
class Teacher extends Person{
private int hours; //課時(shí)
static final double ASSISTANT_STANDARD = 35.0d;//助教的課時(shí)收費(fèi)標(biāo)準(zhǔn)
static final double LECTOR_STANDARD = 45.0d;//講師的課時(shí)收費(fèi)標(biāo)準(zhǔn)
static final double ANOTHER_STANDARD = 55.0d;//其他課時(shí)收費(fèi)標(biāo)準(zhǔn)
Teacher (String name,String post){
super(name,post);
}
public double counting(){
double wage; //工資
if(this.getPost().equals("助教")){
wage = ASSISTANT_STANDARD*hours;
}else if(this.getPost().equals("講師")){
wage = LECTOR_STANDARD * hours;
}else{
wage = ANOTHER_STANDARD * hours;
}
return wage;
}
public void setHours(int hours){
this.hours = hours;
}
public int getHours(){
return this.hours;
}
}
class Test {
public static void main(String [] args){
Leader leader = new Leader("張三","領(lǐng)導(dǎo)");
System.out.println(leader.getName()+"工資為:"+leader.counting());
Management manage = new Management("李四","管理人員");
manage.setBasicWage(1000d);
manage.setAllowance(500d);
System.out.println(manage.getName()+"工資為:"+manage.counting());
Teacher teacher = new Teacher("王五","助教");
teacher.setHours(50);
System.out.println(teacher.getName()+"工資為:"+teacher.counting());
Teacher teacher1 = new Teacher("趙六","講師");
teacher1.setHours(60);
System.out.println(teacher1.getName()+"工資為:"+teacher1.counting
());
}
}
看下吧,有什么不足的請(qǐng)指出來(lái)
person類:
public abstract class Person {
public double pay; // 總工資
public int hour; // 課時(shí)
public double countPay(int hour) {
return pay;
}
}
助教類:
public class Assistant extends Person {
public final double BASE_PAY = 800; // 基本工資
public final double HOUR_PAY = 25; // 每課時(shí)的費(fèi)用
public double countPay(int hour) {
pay = BASE_PAY + hour * HOUR_PAY;
return pay;
}
}
講師類:
public class Instructor extends Person {
public final double BASE_PAY = 1000; // 基本工資
public final double HOUR_PAY = 35; // 每課時(shí)的費(fèi)用
public double countPay(int hour) {
pay = BASE_PAY + hour * HOUR_PAY;
return pay;
}
}
副教授類:
public class AssistantProfesson extends Person {
public final double BASE_PAY = 1200; // 基本工資
public final double HOUR_PAY = 40; // 每課時(shí)的費(fèi)用
public double countPay(int hour) {
pay = BASE_PAY + hour * HOUR_PAY;
return pay;
}
}
教授類:
public class Professor extends Person {
public final double BASE_PAY = 1400; // 基本工資
public final double HOUR_PAY = 50; // 每課時(shí)的費(fèi)用
public double countPay(int hour) {
pay = BASE_PAY + hour * HOUR_PAY;
return pay;
}
}
測(cè)試類:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) {
System.out.println("人員類型如下:");
System.out.println("1 = 助教\r\n2 = 講師\r\n3 = 副教授\r\n4 = 教授");
System.out.print("請(qǐng)選擇:");
BufferedReader personType = new BufferedReader(new InputStreamReader(
System.in));
String type = null;
int hour = 0;
try {
type = personType.readLine();
} catch (Exception e) {
e.printStackTrace();
}
if (type.matches("[1-4]{1}")) {
switch (Integer.valueOf(type)) {
case 1:
hour = getHour();
if(hour == 0){return;}
Person p1 = new Assistant();
double pay1 = p1.countPay(hour);
System.out.println("助教工作" + hour + "課時(shí)的工資為:" + pay1);
break;
case 2:
hour = getHour();
if(hour == 0){return;}
Person p2 = new Instructor();
double pay2 = p2.countPay(hour);
System.out.println("講師工作" + hour + "課時(shí)的工資為:" + pay2);
break;
case 3:
hour = getHour();
if(hour == 0){return;}
Person p3 = new AssistantProfesson();
double pay3 = p3.countPay(hour);
System.out.println("副教授工作" + hour + "課時(shí)的工資為:" + pay3);
break;
case 4:
hour = getHour();
if(hour == 0){return;}
Person p4 = new Professor();
double pay4 = p4.countPay(hour);
System.out.println("教授工作" + hour + "課時(shí)的工資為:" + pay4);
break;
}
} else {
System.out.println("輸入數(shù)據(jù)錯(cuò)誤!程序提前推出!");
return;
}
}
public static int getHour() {
System.out.print("請(qǐng)輸入工作時(shí)間:");
BufferedReader hours = new BufferedReader(new InputStreamReader(
System.in));
String strHour = null;
int hour = 0;
try {
strHour = hours.readLine();
} catch (Exception e) {
e.printStackTrace();
}
if (strHour.matches("^[0-9]+?")) {
hour = Integer.parseInt(strHour);
} else {
System.out.println("輸入?yún)?shù)不正確!程序提前推出!");
}
return hour;
}
}
一:將員工姓名、工資封裝成一個(gè)對(duì)象
public class Staff {
private String name;
private int salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Staff(String name, int salary) {
super();
this.name = name;
this.salary = salary;
}
}
二:初始化一個(gè)數(shù)組,算平均工資
public class Average {
public static void main(String[] args) {
Staff staffs[] = {new Staff("zhangsan", 1000), new Staff("lisi", 1100), new Staff("wangwu", 1200)};
int sum = 0;
for(Staff staff : staffs) {
sum = sum + staff.getSalary();
}
System.out.println("員工人數(shù):" + staffs.length + " 總工資:" + sum + " 平均工資:" + sum / staffs.length);
}
}
class?Employee?{
private?String?name;
private?String?department;
private?double?salary;
//構(gòu)造方法
public?Employee(String?name,?String?department,?double?salary)?{
this.name?=?name;
this.department?=?department;
this.salary?=?salary;
}
public?String?toString()?{
return?"姓名:"?+?name?+?"\t部門:"?+?department?+?"\t工資:"?+?salary;
}
public?void?raiseSalary(double?per)?{
this.salary?=?salary?+?salary?*?per;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?String?getDepartment()?{
return?department;
}
public?void?setDepartment(String?department)?{
this.department?=?department;
}
public?double?getSalary()?{
return?salary;
}
public?void?setSalary(double?salary)?{
this.salary?=?salary;
}
}
public?class?TestEmployee?{//測(cè)試類
public?static?void?main(String[]?args)?{
Employee?e1?=?new?Employee("張三",?"技術(shù)開(kāi)發(fā)部",?5000);
Employee?e2?=?new?Employee("趙四",?"后勤服務(wù)部",?3800);
Employee?e3?=?new?Employee("王五",?"產(chǎn)品營(yíng)銷部",?6800);
System.out.println(e1?+?"\n"?+?e2?+?"\n"?+?e3);
double?per?=?0.07;
e1.raiseSalary(per);
e2.raiseSalary(per);
e3.raiseSalary(per);
System.out.println("==============加薪7%===============");
System.out.println(e1?+?"\n"?+?e2?+?"\n"?+?e3);
}
}
輸出
姓名:張三 部門:技術(shù)開(kāi)發(fā)部 工資:5000.0
姓名:趙四 部門:后勤服務(wù)部 工資:3800.0
姓名:王五 部門:產(chǎn)品營(yíng)銷部 工資:6800.0
==============加薪7%===============
姓名:張三 部門:技術(shù)開(kāi)發(fā)部 工資:5350.0
姓名:趙四 部門:后勤服務(wù)部 工資:4066.0
姓名:王五 部門:產(chǎn)品營(yíng)銷部 工資:7276.0