题目A1:
- (1)编写老师类,要求有属性:姓名name,年龄age,职称post,基本工资salary
- (2)编写业务方法:introduce(),实现输出一个教师的信息
- (3)编写教师类的三个子类:教授类(Professor)、副教授类、讲师类,工资级别分为:教授1.3、副教授1.2、讲师类1.1。再三个子类重写父类的introduce()方法
- (4)定义并初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印。
分析:
将类的属性定义好后,构造器、getter、setter方法码好,在这个题中有一个”级别“存在,当然这个级别可以在子类中重写父类的工资方法里面去直接乘阿拉伯数字,当然为了方便,以及后期可能级别会发生改变,我们可以将其级别给定义成一个变量在里面,然后再工资计算的方法中去将变量用进去,这样后期的更改更方便。
代码:
public class Test {
public static void main(String[] args) {
Professor professor = new Professor("小王", 21, "教授", 5200, 1.3);
System.out.println(professor.introduce());
Professor1 professor1 = new Professor1("江仔", 22, "副教授", 5000, 1.2);
System.out.println(professor1.introduce());
Professor2 professor2 = new Professor2("啦啦", 23, "讲师", 4500, 1.1);
System.out.println(professor2.introduce());
}
}
class Teacher{
private String name;
private int age;
private String post;
private double salary;
private double grade ;
public Teacher(String name, int age, String post, double salary, double grade) {
this.name = name;
this.age = age;
this.post = post;
this.salary = salary;
this.grade = grade;
}
//* (2)编写业务方法:introduce(),实现输出一个教师的信息
public String introduce(){
return "姓名="+getName()+"\t年龄="+getAge()+"\t职称="+getPost()+"\t工资"+getSalary();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
//* (3)编写教师类的三个子类:教授类(Professor)、副教授类、讲师类,
//* 工资级别分为:教授1.3、副教授1.2、讲师类1.1。再三个子类重写父类的introduce()方法
public class Professor extends Teacher{
public Professor(String name, int age, String post, double salary, double grade) {
super(name, age, post, salary, grade);
}
@Override
public double getSalary() {
return super.getSalary()*getGrade();
}
@Override
public String introduce() {
return super.introduce();
}
}
class Professor1 extends Teacher{
public Professor1(String name, int age, String post, double salary, double grade) {
super(name, age, post, salary, grade);
}
@Override
public double getSalary() {
return super.getSalary()*getGrade();
}
@Override
public String introduce() {
return super.introduce();
}
}
class Professor2 extends Teacher{
public Professor2(String name, int age, String post, double salary, double grade) {
super(name, age, post, salary, grade);
}
@Override
public double getSalary() {
return super.getSalary()*getGrade();
}
@Override
public String introduce() {
return super.introduce();
}
}
输出:
姓名=小王 年龄=21 职称=教授 工资6760.0
姓名=江仔 年龄=22 职称=副教授 工资6000.0
姓名=啦啦 年龄=23 职称=讲师 工资4950.0
题目A2:
/**
- 父类:员工类, 子类:部门经理类、普通员工类
- (1)、部门经理工资=1000+单日工资**天数*等级(1.2)
- (2)、普通员工工资=单日工资**天数*等级(1.0)
- (3)、员工属性:姓名、单日工资、工作天数
- (4)、员工方法:打印工资
- (5)、普遍员工及部门经理都是员工的子类,需要重写打印工资的方法
- (6)、定义并初始化普通员工对象,调用打印工资方法输出工资,定义并初始化部门经理对象,调用并打印工资方法输出工资
*/
分析:和A1题类似的,将其等级也定义成一个变量,直接上代码吧:
public class HomeWork04 {
public static void main(String[] args) {
Manage tom = new Manage("tom", 200, 30, 1.1, 1000);
System.out.println(tom.print());
GeneralStaff jam = new GeneralStaff("jam", 180, 30, 1.0);
System.out.println(jam.print());
}
}
class Staff {
private String name;
private double daysalary;
private int days;
private double grade;//等级
private double salary;//最终工资
//构造器
public Staff(String name, double daysalary, int days, double grade) {
this.name = name;
this.daysalary = daysalary;
this.days = days;
this.grade = grade;
}
//打印工资方法
public String print(){
return "name="+getName()+"\t单日工资="+getDaysalary()+"\t天数="+getDays()+"\t等级="+getGrade()+"\t最终工资"+getSalary();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getDaysalary() {
return daysalary;
}
public void setDaysalary(double daysalary) {
this.daysalary = daysalary;
}
public int getDays() {
return days;
}
public void setDays(int days) {
this.days = days;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public double getSalary() {
return getDaysalary()*getDays()*getGrade();
}
public void setSalary(double salary) {
this.salary = salary;
}
}
/*(5)、普遍员工及部门经理都是员工的子类,需要重写打印工资的方法*/
public class GeneralStaff extends Staff {
public GeneralStaff(String name, double daysalary, int days, double grade) {
super(name, daysalary, days, grade);
}
@Override
public double getSalary() {
return super.getSalary();
}
@Override
public String print() {
return "普通员工信息:" + super.print();
}
}
/*(5)、普遍员工及部门经理都是员工的子类,需要重写打印工资的方法*/
public class Manage extends Staff{
private double bonus;//经理独有属性:奖金
public Manage(String name, double daysalary, int days, double grade, double bonus) {
super(name, daysalary, days, grade);
this.bonus = bonus;
}
@Override
public double getSalary() {
return super.getSalary()+getBonus();
}
@Override
public String print() {//重写打印方法
return "经理信息:\t"+super.print()+"\t奖金="+getBonus();
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
输出:
经理信息: name=tom 单日工资=200.0 天数=30 等级=1.1 最终工资7600.0 奖金=1000.0
普通员工信息:name=jam 单日工资=180.0 天数=30 等级=1.0 最终工资5400.0
题目A3:
/**
- 设计父类一员工类,子类:工人类(Worker)、农民类(Peasant)、
教师类(Teacher)、科学家类(Scientist)、服务生类(Waiter) - (1)、其中工人、农民、服务生只有基本工资
- (2)、教师除基本农资外,还有课酬(元/天)
- (3)、科学家除基本工资外,还有年终奖
- (4)、编写一个测试类,将各种类型的员工的全年工资打印出来
*/
和上面差别不大,直接上代码,测试类放在最后的
public class Staff {
//共有属性:姓名、(salary)
private String name;
private double salary;
//父类构造器
public Staff(String name, double salary) {
this.name = name;
this.salary = salary;
}
//打印年工资的方法
public void print(){
System.out.println("姓名="+getName()+"\t年工资="+getSalary()*12);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
//(1)、其中工人、农民、服务生只有基本工资
public class Worker extends Staff{
public Worker(String name, double salary) {
super(name, salary);
}
@Override
public void print() {
System.out.print("工人:");
super.print();
}
}
public class Waiter extends Staff{
public Waiter(String name, double salary) {
super(name, salary);
}
@Override
public void print() {
System.out.print("服务生:");
super.print();
}
}
//(2)、教师除基本农资外,还有课酬(元/天),注意:老师只上8个月的班
public class Teacher extends Staff{
private double claSalary;//课酬
private int days;//每个月上课天数
public Teacher(String name, double salary) {
super(name, salary);
}
public double getClaSalary() {
return claSalary;
}
public void setClaSalary(double claSalary) {
this.claSalary = claSalary;
}
public int getDays() {
return days;
}
public void setDays(int days) {
this.days = days;
}
//重写打印年工资方法
@Override
public void print() {
System.out.print("老师:");
System.out.println("\t姓名="+getName()+"\t年工资="+(getSalary()+getDays()*getClaSalary()));
}
}
//(3)、科学家除基本工资外,还有年终奖
public class Scientist extends Staff{
private double yearEndAwards;//年终奖
private int month;//上班月数
public Scientist(String name, double salary) {
super(name, salary);
}
//重写打印方法
@Override
public void print() {
System.out.print("科学家:");
System.out.println("姓名="+getName()+"\t年工资="+(getYearEndAwards()+getSalary()*getMonth()));
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public double getYearEndAwards() {
return yearEndAwards;
}
public void setYearEndAwards(double yearEndAwards) {
this.yearEndAwards = yearEndAwards;
}
}
public class Peasant extends Staff{
public Peasant(String name, double salary) {
super(name, salary);
}
@Override
public void print() {
System.out.print("农民:");
super.print();
}
}
//(4)、编写一个测试类,将各种类型的员工的全年工资打印出来
//设计父类一员工类,子类:工人类(Worker)、农民类(Peasant)、
//教师类(Teacher)、科学家类(Scientist)、服务生类(Waiter)
public class OOP05Test {
public static void main(String[] args) {
//测试工人类
Worker worker = new Worker("江仔工人", 4000);
worker.print();
//测试农民类
Peasant peasant = new Peasant("王川农民", 3000);
peasant.print();
//测试老师类
Teacher teacher = new Teacher("露露", 3500);
teacher.setClaSalary(150);//设置课酬
teacher.setDays(280);//设置上课天数
teacher.print();
//测试科学家类
Scientist scientist = new Scientist("小王", 5500);
scientist.setYearEndAwards(50000);
scientist.setMonth(10);//设置上课月份数;
scientist.print();
//测试服务生类
Waiter waiter = new Waiter("啦啦", 4200);
waiter.print();
}
}
输出:
工人:姓名=江仔工人 年工资=48000.0
农民:姓名=王川农民 年工资=36000.0
老师:姓名=露露 年工资=45500.0
科学家:姓名=小王 年工资=105000.0
服务生:姓名=啦啦 年工资=50400.0