欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

java小练习

时间:2023-06-10
文章目录

面向对象编程练习1

练习2 面向对象编程 练习1

题目:编写老师类 Homework03.java
(1)要求有属性“姓名name”, “年龄age” “职称post”, “基本工资salary
(2)编写业务方法,introduce(),实现输出一个教师的信息。
(3)编写教师类的三个子类:教授类、副教授类、讲师类。工资级别分别为:教授为1.3、副教授为1.2、讲师类1.1。在三个子类里面都重写父类的introduce()方法。(4)定义井初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印。

分析: 1.老师类 属性 姓名name,年龄age,职称post,基本工资salary,级别grade

demo
老师类

class teacher{//定义属性 private String name; private String post; private int age; private double salary; private double grade; //打印信息方法 public void introduce() { System.out.println("name="+ name +" age="+age+" post="+post+" salary="+salary+" grade="+grade); }} //构造方法 public teacher(String name, String post, int age, double salary ,double grade) { this.name = name; this.post = post; this.age = age; this.salary = salary; this.grade = grade; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPost() { return post; } public void setPost(String post) { this.post = post; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } 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; }

教授类

class Parofessor extends teacher{ public Parofessor(String name, String post, int age, double salary, double grade) { super(name, post, age, salary, grade); } public void introduce(){ //复用代码 System.out.println("这是教授的信息"); super.introduce(); }}

总结:体现了面向对象编程的好处,通过复用代码减少代码行数


练习2

题目: 通过继承实现员工工资核算打印功能
父类:员工类
子类:部门经理类,普通员工类
(1)部门经理工资=1000+单日工资 * 天数*等级(1.2)。
(2)普通员工工资=单日工资 * 天数 * 等级(1.0);
(3)员工属性:姓名,单日工资,工作天数;
(4)员工方法(打印工资);
(5)普通员工及部门经理的都是员工子类。需要重写打印工资方法。
(6)定义并初始化普通员工对象,调用打印工资方法输入工资,定义并初始化部门经理对象,调用打印工资方法输入工资

demo
员工类:

public class staff{ private String name; private double daySal; private int workDays; private double grade; //构造器,get,set public staff(String name, double daySal, int workDays, double grade) { this.name = name; this.daySal = daySal; this.workDays = workDays; this.grade = grade; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getDaySal() { return daySal; } public void setDaySal(double daySal) { this.daySal = daySal; } public int getWorkDays() { return workDays; } public void setWorkDays(int workDays) { this.workDays = workDays; } public double getGrade() { return grade; } public void setGrade(double grade) { this.grade = grade; } //方法 打印工资 viod printSal(){} public void printSal(){ System.out.println(name+"工资="+daySal+workDays+grade); }}

普通员工类

public class work extends staff { public work(String name, double daySal, int workDays, double grade) { super(name, daySal, workDays, grade); } @Override public void printSal() { super.printSal(); }}

经理类

public class manager extends staff{ private double bonus; //因为奖金是不确定的,不用构造函数,使用set方法(这样我们可以更灵活的更改) public manager(String name, double daySal, int workDays, double grade) { super(name, daySal, workDays, grade); } public double getBonus() { return bonus; } public void setBonus(double bonus) { this.bonus = bonus; } //重写打印工资方法 @Override public void printSal() { System.out.println("经理:"+getName()+"t工资=" +(bonus+getDaySal()*getWorkDays()*getGrade())); }}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。