1.对象数组排序
定义一个Person类{name,age,job},初始化Person对象数组,有3个person对象,并按照age从小到大进行冒泡排序;再按照name的长度从小到大进行选择排序。
public class HomeWork01 {public static void main(String[] args) {Person[] persons = new Person[4];persons[0] = new Person("zhangsan", 18, "java工程师");persons[1] = new Person("lisi", 32, "项目经理");persons[2] = new Person("wangwu", 10, "小学生");persons[3] = new Person("tom", 50, "BOSS");System.out.println("原始数组:" + Arrays.toString(persons));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()) {Person temp = persons[j];persons[j] = persons[j + 1];persons[j + 1] = temp;}}}System.out.println("按照年龄[从小到大]进行冒泡排序:" + Arrays.toString(persons));for (int i = 0; i < persons.length - 1; i++) {for (int j = i + 1; j < persons.length; j++) {if (persons[i].getName().length() > persons[j].getName().length()) {Person temp1 = persons[i];persons[i] = persons[j];persons[j] = temp1;}}}System.out.println("按照姓名长度[从小到大]进行选择排序:" +Arrays.toString(persons));}
}class Person {private String name;private int age;private String 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;}public Person(String name, int age, String job) {this.name = name;this.age = age;this.job = job;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", job='" + job + '\'' +'}';}
}
运行结果:
2.super与this区别
3.判断输出结果(super与this区分)
public class Homework07 {public static void main(String[] args) {}
}class Test { //父类String name = "Rose";Test() {System.out.println("Test");//(1)}Test(String name) {//name johnthis.name = name;//这里把父类的 name 修改 john}
}class Demo extends Test {//子类String name = "Jack";Demo() {super();System.out.println("Demo");//(2)}Demo(String s) {super(s);}public void test() {System.out.println(super.name);//(3) Rose (5) johnSystem.out.println(this.name);//(4) Jack (6) Jack}public static void main(String[] args) {//1. new Demo()new Demo().test(); //匿名对象new Demo("john").test();//匿名}
}
运行结果:
4.模拟银行存取款业务
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;}
}
题目要求:
(1)在上面类的基础上扩展新类CheckingAccount,对每次存款和取款都收取1美元的手续费。
首先,我们对BankAccount类添加get与set方法:
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:
class CheckingAccount extends BankAccount {public CheckingAccount(double initialBalance) {super(initialBalance);}/*** 存款收取1美元手续费,相当于少存1美元** @param amount*/@Overridepublic void deposit(double amount) {super.deposit(amount - 1);}/*** 取款收取1美元手续费,相当于再给银行1美元** @param amount*/@Overridepublic void withdraw(double amount) {super.withdraw(amount + 1);}
}
结果检验:
public class HomeWork08 {public static void main(String[] args) {CheckingAccount checkingAccount = new CheckingAccount(1000);checkingAccount.deposit(100);System.out.println(checkingAccount.getBalance()); // 1099.0checkingAccount.withdraw(20);System.out.println(checkingAccount.getBalance()); // 1078.0}
}
(2)扩展前一个练习的BankAccount类,新类SavingsAccount每个月都有利息产生(earnMonthlyInterest方法被调用),并且有每月三次免手续费的存款或取款。在earnMonthlyInterest方法中重置交易计数。
SavingsAccount类:
class SavingsAccount extends BankAccount {// 定义计费次数private int count = 3;// 定义银行利率private double rate = 0.01;public SavingsAccount(double initialBalance) {super(initialBalance);}/*** 每月重新计算利率*/public void earnMonthlyInterest() {// 重新初始化计费次数this.count = 3;// 每月重新计算计算余额super.deposit(getBalance() * rate);}/*** 重写存款方法,存款前查看存款是否超过3次** @param amount 存入的金额*/@Overridepublic void deposit(double amount) {if (count > 0) {super.deposit(amount);} else {super.deposit(amount - 1);}count--;}/*** 重写取款方法,取款前查看取款是否超过3次** @param amount 取款的金额*/@Overridepublic void withdraw(double amount) {if (count > 0) {super.withdraw(amount);} else {super.withdraw(amount + 1);}count--;}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;}
}
结果检验:
public class HomeWork08 {public static void main(String[] args) { SavingsAccount savingsAccount = new SavingsAccount(1000);savingsAccount.deposit(100);savingsAccount.deposit(100);savingsAccount.deposit(100);System.out.println(savingsAccount.getBalance()); // 1300.0savingsAccount.withdraw(10); // 第4次存取,需要征收手续费System.out.println(savingsAccount.getBalance()); // 1289.0// 月初初始化计费次数savingsAccount.earnMonthlyInterest();System.out.println(savingsAccount.getBalance()); // 1301.89savingsAccount.deposit(100);System.out.println(savingsAccount.getBalance()); // 1401.89savingsAccount.withdraw(50);savingsAccount.withdraw(50);savingsAccount.withdraw(50); // 征收手续费System.out.println(savingsAccount.getBalance()); // 1250.89}
}
5.重写equals方法判断两个对象是否相等
编写Doctor类{name,age,job,gender,sal}相应的getter()和setter()方法,5个参数的构造器,重写父类(Object)的equals()方法:public boolean equals(Object obj),并判断测试类中创建的两个对象是否相等。相等就是判断属性是否相同。
public class HomeWork10 {public static void main(String[] args) {Doctor doctor = new Doctor("张三", 32, "主治医师", '男', 25000);Doctor doctor2 = new Doctor("张三", 32, "主治医师", '男', 25000);System.out.println(doctor.equals(doctor2)); // true}
}class Doctor {private String name;private int age;private String job;private char gender;private double sal;@Overridepublic boolean equals(Object obj) {if (this == obj) {return true;}// 判断obj 是否是 Doctor类型或其子类if (!(obj instanceof Doctor)) {return false;}// 向下转型, 因为obj的运行类型是Doctor或者其子类型Doctor doctor = (Doctor) obj;return this.name.equals(doctor.getName()) &&this.age == doctor.getAge() &&this.job.equals(doctor.getJob()) &&this.gender == doctor.getGender() &&this.sal == doctor.getSal();}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;}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;}
6.写出向上转型和向下转型代码
public class HomeWork11 {public static void main(String[] args) {// 向上转型代码// 父类的引用指向子类对象Person2 p = new Student();p.run(); // student runp.eat(); // person eat// 向下转型代码// 把指向子类对象的父类引用,转成指向子类对象的子类引用Student student = (Student) p;// 注意动态绑定问题student.run(); // student runstudent.study(); // student study..// 因为Student是Person2的子类,所以可以调用父类eat方法student.eat(); // person eat}
}class Person2 {public void run() {System.out.println("person run");}public void eat() {System.out.println("person eat");}
}class Student extends Person2 {@Overridepublic void run() {System.out.println("student run");}public void study() {System.out.println("student study..");}
}
7.判断输出结果
public class HomeWork14 {public static void main(String[] args) {C c = new C();}
}class A {public A() {System.out.println("我是A类");}
}class B extends A {public B() {System.out.println("我是B类的无参构造");}public B(String name) {System.out.println(name + "我是B类的有参构造");}
}class C extends B {public C() {this("hello");System.out.println("我是C类的无参构造");}public C(String name) {super("hahah");System.out.println("我是C类的有参构造");}
}
运行结果: