第1题
package com.hspedu.homework;
/**
* @author:寰愬悏瓒�
* @date:2024/12/19 version:1.0
*/
public class Homework01 {
public static void main(String[] args) {
//初始化Person对象数组,有3个Person对象;
Person[] persons = new Person[3];
persons[0] = new Person("tom", 87, "医生");
persons[1] = new Person("jack", 28, "老师");
persons[2] = new Person("smith", 56, "护士");
Person temp = null;
for (int i = 0; i < persons.length-1; i++) {
for (int j = 0; j < persons.length - 1 - i; j++) {
if(persons[j].getAge() > persons[j+1].getAge()){
temp = persons[j];
persons[j] = persons[j+1];
persons[j+1] = temp;
}
}
}
//输出结果
for (int i = 0; i < persons.length; i++) {
System.out.println(persons[i].toString());
}
}
static 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", job='" + job + '\'' +
'}';
}
}
}
第2题
第3题
Teacher类
package com.hspedu.homework;
/**
* @author:寰愬悏瓒�
* @date:2024/12/20 version:1.0
*/
public 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);
}
}
Professor类
package com.hspedu.homework;
/**
* @author:寰愬悏瓒�
* @date:2024/12/20 version:1.0
*/
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();
}
}
Homework3
package com.hspedu.homework;
import jdk.nashorn.internal.ir.CallNode;
public class Homework03 {
public static void main(String[] args) {
Professor professor = new Professor("林老师",30,"高级职称",30000,1.3);
professor.introduce();
}
}
第4题
Worker类
package com.hspedu.homework;
/**
* @author:寰愬悏瓒�
* @date:2024/12/20 version:1.0
*/
public class Worker {
private String name;
private double salary;
private int day;
// 需要一个等级
private double garde;
public Worker(String name, double salary, int day, double garde) {
this.name = name;
this.salary = salary;
this.day = day;
this.garde = garde;
}
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 double getGarde() {
return garde;
}
public void setGarde(double garde) {
this.garde = garde;
}
public void print() {
System.out.println(name + " 工资为:" + salary*day*garde);
}
}
PTworker类
package com.hspedu.homework;
/**
* @author:寰愬悏瓒�
* @date:2024/12/20 version:1.0
*/
public class PTworker extends Worker{
public PTworker(String name, double salary, int day, double garde) {
super(name, salary, day, garde);
}
@Override
public void print() {
System.out.print("普通员工");
super.print();
}
}
MAworker类
package com.hspedu.homework;
/**
* @author:寰愬悏瓒�
* @date:2024/12/20 version:1.0
*/
public class MAworker extends Worker{
//特有属性
private double bonus;
public MAworker(String name, double salary, int day, double garde) {
super(name, salary, day, garde);
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public void print() {
System.out.println("部门经理" + getName()+" 工资为:" + (bonus+getSalary()*getDay()*getGarde()));
}
}
Homework04类
package com.hspedu.homework;
/**
* @author:寰愬悏瓒�
* @date:2024/12/20 version:1.0
*/
public class Homework04 {
public static void main(String[] args) {
PTworker jack = new PTworker("jack", 260, 20, 1.0);
MAworker tom = new MAworker("tom", 500, 25, 1.2);
//设置奖金
tom.setBonus(3000);
jack.print();
tom.print();
}
}
第5题
Yugong类
package com.hspedu.homework.homework5;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class Yungong {
private String name;
private double sal;
//带薪月数
private int salMonth = 12;
public Yungong(String name, double sal) {
this.name = name;
this.sal = sal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public int getSalMonth() {
return salMonth;
}
public void setSalMonth(int salMonth) {
this.salMonth = salMonth;
}
//打印工资
public void print_() {
System.out.println(name + " 的全年工资为:" + (salMonth * sal));
}
}
Worker
package com.hspedu.homework.homework5;
import java.io.StringReader;
public class Worker extends Yungong{
public Worker(String name, double sal) {
super(name, sal);
}
//打印工资
@Override
public void print_() {
super.print_();
}
}
Peasant
package com.hspedu.homework.homework5;
public class Peasant extends Yungong{
public Peasant(String name, double sal) {
super(name, sal);
}
//打印工资
@Override
public void print_() {
super.print_();
}
}
Teacher类
package com.hspedu.homework.homework5;
import java.util.SplittableRandom;
public class Teacher extends Yungong{
//一年上的多少节课
private double classDay;
//课时费
private double classSal;
public Teacher(String name, double sal) {
super(name, sal);
}
public double getClassDay() {
return classDay;
}
public void setClassDay(double classDay) {
this.classDay = classDay;
}
public double getClassSal() {
return classSal;
}
public void setClassSal(double classSal) {
this.classSal = classSal;
}
//打印工资
@Override
public void print_() {
System.out.println(getName() + " 全年工资为:" + (getSalMonth() * getSal() + getClassDay() * getClassSal()));
}
}
Scientist
package com.hspedu.homework.homework5;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class Scientist extends Yungong{
private double bonus;
public Scientist(String name, double sal) {
super(name, sal);
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
//打印工资
@Override
public void print_() {
System.out.println(getName() + " 全年工资为:" + (getSalMonth() * getSal() + getBonus()));
}
}
Waiter
package com.hspedu.homework.homework5;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class Waiter extends Yungong{
public Waiter(String name, double sal) {
super(name, sal);
}
//打印工资
@Override
public void print_() {
super.print_();
}
}
测试类Homework05
package com.hspedu.homework.homework5;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class Homework05 {
public static void main(String[] args) {
Worker jack = new Worker("jack", 1000);
Peasant tom = new Peasant("tom", 500);
Teacher sim = new Teacher("sim", 3500);
sim.setClassDay(300);
sim.setClassSal(1000);
Scientist scient = new Scientist("scient", 6000);
scient.setBonus(10000);
Waiter wait = new Waiter("wait", 2000);
jack.print_();
tom.print_();
sim.print_();
scient.print_();
wait.print_();
}
}
第6题
super从父类(不局限于直接父类)开始,不找本类;this从本类开始查找,再找父类
super(父类对象)可以访问父类及以上,除了 private修饰、静态、覆盖的成员
this访问不了父类中被重写(或者说 覆盖)的属性和方法
第7题
代码
package com.hspedu.homework;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class Homework07 {
}
class Test{
String name = "Rose";
Test() {
System.out.println("Test");//(1)Test
}
Test(String name) {
this.name = name; //将Test类的 name 修改为 john
}
}
class Demo extends Test{
String name = "Jack";
Demo() {
super();
System.out.println("Demo");//(2)Demo
}
Demo(String s) {
super(s); //s == john
}
public void test() {
System.out.println(super.name);//(3)Rose (5)john
System.out.println(this.name);//(4)Jack (6) Jack
}
//程序从这里开始执行
public static void main(String[] args) {
new Demo().test();
new Demo("john").test();
}
}
第8题
BankAccount
package com.hspedu.homework;
import com.sun.corba.se.impl.ior.ObjectAdapterIdNumber;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class BankAccount {//父类
private double balance; //余额
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
//存款
public void deposit(double amount) {
balance += amount;
}
//取款
public void withdraw(double amount) {
balance -= amount;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
CheckingAccount
package com.hspedu.homework;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class CheckingAccount extends BankAccount {
public CheckingAccount(double initialBalance) {
super(initialBalance);
}
@Override
public void deposit(double amount) {
super.deposit(amount-1);
//1块钱入银行的账户
}
@Override
public void withdraw(double amount) {
super.withdraw(amount+1);
//1块钱入银行的账户
}
}
SavingsAccount
package com.hspedu.homework;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class SavingsAccount extends BankAccount {
private int count = 3;
private double rate = 0.01; //利率
public SavingsAccount(double initialBalance) {
super(initialBalance);
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
//每个月初,统计上个月的利息,并重置 减免手续费次数的值 count = 3;
public void earnMonthlyInterest(){
count = 3;
super.deposit(getBalance() * rate);
}
//重写存款方法
@Override
public void deposit(double amount) {//存款
//判断是否还可以免手续费
if(count > 0){
super.deposit(amount);
} else {
super.deposit(amount-1);
}
count--; //减去一次
}
//重写 取款方法
@Override
public void withdraw(double amount) { //取款
//判断是否还可以免手续费
if(count > 0) {
super.withdraw(amount); //1块转入银行账户
} else {
super.withdraw(amount+1);
}
count--; //减去一次
}
}
Homework08
package com.hspedu.homework;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class Homework08 {
public static void main(String[] args) {
// CheckingAccount checkingAccount = new CheckingAccount(1000);
// checkingAccount.deposit(10);//存10块,-1手续费 1009
// checkingAccount.withdraw(10);//取10块,-1手续费 998
//
// System.out.println(checkingAccount.getBalance());
SavingsAccount savingsAccount = new SavingsAccount(1000);
savingsAccount.deposit(100);//存款100 免手续费
savingsAccount.deposit(100);//存款100 免手续费
savingsAccount.deposit(100);//存款100 免手续费
System.out.println(savingsAccount.getBalance());//余额 1300
savingsAccount.deposit(100);//手续费 -1
System.out.println(savingsAccount.getBalance());//余额 1300+100-1=1399
// //月初,定时器自动调用一下, earnMonthlyInterest
savingsAccount.earnMonthlyInterest();//统计利息
System.out.println(savingsAccount.getBalance());//1399 + 13.99=
savingsAccount.deposit(100);//存款100 免手续费
savingsAccount.deposit(100);//存款100 免手续费
savingsAccount.deposit(100);//存款100 免手续费
}
}
第9题
Point
package com.hspedu.homework;
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
LabeledPoint
package com.hspedu.homework;
import com.hspedu.homework.Point;
public class LabeledPoint extends Point {
private String label;
public LabeledPoint( String label,double x, double y) {
super(x, y);
this.label = label;
}
}
Homework09
package com.hspedu.homework;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class Homework09 {
public static void main(String[] args) {
new LabeledPoint("Black",1929,230.04);
}
}
第10题
Doctor
package com.hspedu.homework;
import javax.print.Doc;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class Doctor {
private String name;
private int age;
private String job;
private char gender;
private double sal;
public Doctor(String name, int age, String job, char gender, double sal) {
this.name = name;
this.age = age;
this.job = job;
this.gender = gender;
this.sal = sal;
}
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 getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
@Override
public boolean equals(Object obj) {
//判断两个比较对象是否相同
if(this == obj) {
return true;
}
//判断obj 是否是 Doctor类型或其子类
//过关斩将 校验方式
if(! (obj instanceof Doctor)){ //不是的话
return false;
}
//这里因为 编译类型是 object,无法访问Doctor属性,
// 所以需要向下转型
Doctor doctor = (Doctor)obj;
return this.name.equals(doctor.name) && this.age == doctor.age && this
.gender == doctor.gender && this.job.equals(doctor.job) && this.sal == doctor.sal;
}
}
Homework10
package com.hspedu.homework;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class Homework10 {
public static void main(String[] args) {
Doctor doctor1 = new Doctor("jack", 20, "医生", '男', 20000);
Doctor doctor2 = new Doctor("jack", 20, "医生", '男', 20000);
System.out.println(doctor1.equals(doctor2));//true
}
}
第11题
向上转型:父类的引用指向子类的对象
Person p = new Student();
p.run(); // 能调用父类的run(),实际运行的是子类的run();
p.eat();//能调用父类的eat(),因为子类中没有eat(),所以执行的是父类的eat();
编译看左边(父类),执行看右边
意思就是向上转型只能找到父类方法,运行的时候就得看运行类型怎么说了,如果Student类有相同的方法,那就执行Student类的;如果没有,那就去父类找,有就执行;
向下转型:把指向子类对象的父类引用,转成指向子类对象的子类引用
第12题
第13题
Person
package com.hspedu.homework.homework13;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class Person {
private String name;
private char sex;
private int age;
public Person(String name, char sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//玩的方法
public String play() {
return name + "爱玩";
}
//打印方法
public String basicInfo() {
return "性别:" + name + "\n年龄:" + age + "\n性别:" + sex;
}
@Override
public String toString() {
return "name='" + name + '\'' +
", sex=" + sex +
", age=" + age +",";
}
}
Student
package com.hspedu.homework.homework13;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class Student extends Person{
private String stu_id;
public Student(String name, char sex, int age, String stu_id) {
super(name, sex, age);
this.stu_id = stu_id;
}
public String getStu_id() {
return stu_id;
}
public void setStu_id(String stu_id) {
this.stu_id = stu_id;
}
public void study() {
System.out.println("我承诺,我会好好学习");
}
@Override
public String play() {
return super.play() + "足球";
}
//编写一个输出信息的方法,这样体现封装
public void printInfo() {
System.out.println("学生的信息:");
System.out.println(super.basicInfo());
System.out.println("学号:" + stu_id);
study();
System.out.println(play());
}
@Override
public String toString() {
return "Student{" +super.toString()+
"stu_id='" + stu_id + '\'' +
'}';
}
}
Teacher
package com.hspedu.homework.homework13;
/**
* @author:寰愬悏瓒�
* @date:2024/12/21 version:1.0
*/
public class Teacher extends Person{
private int work_age;
public Teacher(String name, char sex, int age, int work_age) {
super(name, sex, age);
this.work_age = work_age;
}
public int getWork_age() {
return work_age;
}
public void setWork_age(int work_age) {
this.work_age = work_age;
}
public void teach(){
System.out.println("我承诺,我会认真教学");
}
@Override
public String play() {
return super.play() +"象棋";
}
public void printInfo() {
System.out.println("老师的信息:");
System.out.println(super.basicInfo());
System.out.println("工龄:" + work_age);
teach();
System.out.println(play());
}
@Override
public String toString() {
return "Teacher{" + super.toString() +
"work_age=" + work_age +
'}';
}
}
Homework13
package com.hspedu.homework.homework13;
public class Homework13 {
public static void main(String[] args) {
// Teacher teacher = new Teacher("张飞", '男', 30, 5);
// teacher.printInfo();
// System.out.println("---------------------------");
//
// Student student = new Student("小明", '男', 15, "00023102");
// student.printInfo();
Person[] person= new Person[4];
person[0] = new Student("tom", '男', 23,"00001");
person[1] = new Student("ming", '男', 24,"00002");
person[2] = new Teacher("smith", '女', 30,10);
person[3] = new Teacher("wujing", '女', 46,20);
Homework13 homework13 = new Homework13();
homework13.bubbleSort(person);
System.out.println("-------------排序后的Person数组------------");
for (int i = 0; i < person.length; i++) {
System.out.println(person[i]); //这是,实际上是调用person[i].toString()
}
//遍历数组,调用test方法
System.out.println("==================");
for (int i = 0; i <person.length; i++) { //遍历多态数组
homework13.test(person[i]); // test方法去判断是学生还是老师
}
}
//排序方法-冒泡排序
private void bubbleSort(Person[] person) {
Person temp = null;
//按照年龄从高到低排序
for (int i = 0; i < person.length-1; i++) {
for (int j = 0; j < person.length-i-1; j++) {
if(person[j].getAge() < person[j+1].getAge()){
temp = person[j];
person[j] = person[j+1];
person[j+1] = temp;
}
}
}
}
//定义方法,形参为Person类型,功能:调用学生的study或教师的teach方法
//这里会使用到向下转型和类型判断
public void test(Person p){
if(p instanceof Student){ // p 的运行类型如果是Student
((Student) p).study();
} else if(p instanceof Teacher) { // p 的运行类型如果是 Teacher
((Teacher) p).teach();
} else {
System.out.println("do nothing...");
}
}
}
第14题
第15题