30天学会JAVA—练习题(2021韩顺平)——Day9
Test Demo Rose Jack John Jack
class BankAccount{
private int balance;
public BankAccount(int balance) {
super();
this.balance = balance;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public void deposit(double amount){
balance += amount;
}
public void withdraw(double amount){
balance -= amount;
}
}
class CheckingAccount extends BankAccount{
public CheckingAccount(int balance) {
super(balance);
}
@Override
public void deposit(double amount) {
super.deposit(amount-1);
}
@Override
public void withdraw(double amount) {
super.withdraw(amount+1);
}
}
class SavingsAccount extends BankAccount{
private double rate = 0.01;
private int count = 3;
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public SavingsAccount(int balance) {
super(balance);
}
@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);
}else{
super.withdraw(amount + 1);
}
count--;
}
public void earnMonthlyInterest(){
//每个月初,统计上个月的利息,同时将count = 3
count = 3;
super.deposit(getBalance() * rate);
}
}
9. 代码
class Point{
private double x;
private double y;
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
class LabeledPoint extends Point{
private String label;
public LabeledPoint(double x, double y, String label) {
super(x, y);
this.label = label;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
10.代码
class Doctor{
private String name;
private int age;
private String job;
private char gender;
private int sal;
public Doctor(String name, int age, String job, char gender, int sal) {
super();
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 int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Doctor){
//判断是否为Doctor类
if((((Doctor) obj).name.equals(name) &&
((Doctor) obj).age == age &&
((Doctor) obj).job.equals(job)&&
((Doctor) obj).gender == gender&&
((Doctor) obj).sal == sal)){
return true;
}
}
return false;
}
}
public class Test {
public static void main(String[] args) {
Doctor doctor1 = new Doctor("jack", 34, "doctor", '男', 2000);
Doctor doctor2 = new Doctor("jack", 34, "doctor", '男', 2000);
System.out.println(doctor1.equals(doctor2));
}
}
/** * 从子类一步步查找,若没有,到父类查找 * 动态绑定 * 调用方法时,看运行类型;调用属性时,哪里声明,哪里使用 */
//向上转型[父类的引用指向子类的对象] 左编译,右运行
Person person = new Student();
/* * 可调能调用父类的方法[编译类型Person] * 可调用Person.run(),Person.eat() * 输出 :student run[动态绑定], person eat */
//向下转型[强制转型](先向上转型之后,才可以向下转型)
Student student = (Student) person;
/* * 可调用父类和子类的方法[编译类型Student] * 可调用Student.eat(),Student.run(),Student.study() * 输出:person eat,student run,student study */