loading...
1.定义一个Person类{name, age, job},初始化Person对象数组,有3个person对象,并按照 age 从 大到 小进行排序,提示,使用冒泡排序Homework01.java
难点1:初始化数组Person[] people = new Person[3];
难点2:获得年龄数据people[j].getAge()
难点3:System.out.println(people[i]); //默认调用对象toString 方法
package com.hspedu.extend_.exercise.OOP;
public class HomeWork01 {
public static void main(String[] args) {
Person[] people = new Person[3];
people[0] = new Person("小莫", 19, "学生");
people[1] = new Person("小季", 20, "学生");
people[2] = new Person("小林", 19, "学生");
for (int i = 0; i < people.length; i++) {
System.out.println(people[i]); //默认调用对象toString 方法
}
Person tmp = null; //因为要交换的是Person 类型的对象
//冒泡排序
for (int i = 0; i < people.length - 1; i++) {
for (int j = 0; j < people.length - 1 - i; j++) {
//年龄从大到小排序
if(people[j].getAge() < people[j+1].getAge()) {
tmp = people[j];
people[j] = people[j+1];
people[j+1] = tmp;
}
}
}
//排序后
System.out.println("======排序后=====");
for (int i = 0; i < people.length; i++) {
System.out.println(people[i]); //默认调用对象toString 方法
}
}
}
class Person {
private String name;
private int age;
private String job;
public Person(String name, int age, String job) {
this.name = name;
this.age = age;
this.job = job;
}
// public String getName() {
// return name;
// }
//
// public void setName(String name) {
// this.name = name;
// }
// public String getJob() {
// return job;
// }
//
// public void setJob(String job) {
// this.job = job;
// }
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", job='" + job + '\'' +
'}';
}
}
结果:
Person{name='小莫', age=19, job='学生'}
Person{name='小季', age=20, job='学生'}
Person{name='小林', age=19, job='学生'}
======排序后=====
Person{name='小季', age=20, job='学生'}
Person{name='小莫', age=19, job='学生'}
Person{name='小林', age=19, job='学生'}
2.写出四种访问修饰符和各自的访问权限Homework02.java
访问级别 | 访问控制修饰符 | 同类 | 同包 | 子类 | 不同包 |
公开 | public | √ | √ | √ | √ |
受保护 | protected | √ | √ | √ | × |
默认 | 没有修饰符 | √ | √ | × | × |
私有 | private | √ | × | × | × |
3.编写老师类 Homework03.java
(1)要求有属性“姓名name”,“年龄age”,“职称post”,“基本工资salary”
(2)编写业务方法,introduce(),实现输出一个教师的信息。
(3) 编写教师类的三个子类:教授类(Professor )、副教授类、讲师类。工资级别分别为:教授为1.3、副教授为1.2、讲师类1.1。在三个子类里面都重写父类的introduce()方法。
(4)定义并初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印.
难点1:明确知晓在子类中无法得到父类的私有信息
难点2:知晓子类建构造器后要用super(),访问父类构造器
难点3:
即使一个方法没有定义任何参数,它仍然可以访问和操作类中的字段(属性)或全局变量。这些变量可以在方法被调用之前或之后定义,并且可以在方法内部使用。
此外,如果方法没有参数,但需要使用一些值进行操作,你可以在方法内部定义局部变量。这些变量只能在方法内部使用,并且它们的生命周期仅限于该方法的执行期间
package com.hspedu.extend_.exercise.OOP;
public class HomeWork03 {
public static void main(String[] args) {
Professor professor = new Professor("贾宝玉", 30, "高级职称", 30000, 1.3);
professor.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;
}
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;
}
public void introduce() {
System.out.println("姓名:" + name + " 年龄:" + age + " 职称:"
+ post + " 基本工资" + salary + " 工资级别" + grade);
}
}
package com.hspedu.extend_.exercise.OOP;
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 void introduce() {
System.out.println(" 这是教授的信息 ");
super.introduce();
}
}
运行结果:
这是教授的信息
姓名:贾宝玉 年龄:30 职称:高级职称 基本工资30000.0 工资级别1.3
4.通过继承实现员工工资核算打印功能
父类:员工类
子类: 部门经理类、普通员工类
(1)部门经理工资=1000+单日工资*天数*等级 (1.2) 。
(2)普通员工工资=单日工资*天数*等级(1.0);
(3)员工属性:姓名,单日工资,工作天数
(4)员工方法(打印工资)
(5)普遍员工及部门经理都是员工子类,需要重写打印工资方法。
(5)定义并初始化普通员工对象,调用打印工资方法输入工资,定义并初始化部门经理对象,调用打印工资方法输入工资
难点1:细品bonus
难点2:注意类名取名规则
package com.hspedu.extend_.exercise.OOP;
public class HomeWork04 {
public static void main(String[] args) {
manage manage = new manage("刘备", 100, 20, 1.2);
manage.setBonus(3000);
manage.printSal();
Worker worker = new Worker("关羽", 100, 20, 1.2);
worker.printSal();
}
}
class employee {
private String name;
private double salary;
private int day;
//分析出还有一个属性等级
private double grade;
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public employee(String name, double salary, int day, double grade) {
this.name = name;
this.salary = salary;
this.day = day;
this.grade = grade;
}
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;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
//打印工资方法
public void printSal() {
System.out.println(name + " 工资是= " + salary * day * grade);
}
}
package com.hspedu.extend_.exercise.OOP;
public class manage extends employee{
public manage(String name, double salary, int day, double grade) {
super(name, salary, day, grade);
}
//特有属性bonus为变化的,不合适初始化
private double bonus = 0;
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public void printSal() {
System.out.println("经理 " + getName() + " 工资是="
+ (bonus + getDay() * getSalary() * getGrade()));
} //细品bonus
}
package com.hspedu.extend_.exercise.OOP;
public class Worker extends employee{
public Worker(String name, double salary, int day, double grade) {
super(name, salary, day, grade);
}
// @Override
// public void printSal() {
// super.printSal();
// }
}
运行结果:
经理 刘备 工资是=5400.0
关羽 工资是= 2400.0
5.不要忘记默认的super();调用父类的有参构造器
属性没有动态绑定机制,哪里声明,哪里使用