Java面向对象(中级)练习题与总结

Java面向对象(中级)练习题与总结

题目

  1. 理清继承
  2. 一个编写思路
  3. equals与==的区别
  4. 多态的总结

对应的代码和思路

  1. 继承代码分析在这里插入图片描述
    在这里插入图片描述
    输出“Test”“Demo”“Rose”“Jack”
    输出"john"“Jack”

在这里插入图片描述
先编写一个测试类,再逐步完善功能

package Bank;

public class myAccount {
    private double balance;


    public myAccount(double balance) {
        this.balance = balance;
    }

    public myAccount() {
    }

    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;
    }
}
//===========================================================================
package Bank;
//测试类
public class bankAccount extends myAccount{

    @Override
    public void deposit(double amount) {
        super.deposit(amount );
    }

    @Override
    public void withdraw(double amount) {
        super.withdraw(amount );
    }

    public bankAccount(double balance) {
        super(balance);
    }

    public bankAccount() {
    }
}
//======================================================================
package Bank;

public class savingsAccount extends myAccount{
    private double rate = 0.1;
    private int count = 0;

    public savingsAccount(double balance) {
        super(balance);
    }

    @Override
    public void deposit(double amount) {
        if (count < 3){
            super.deposit(amount );
        }else {
            super.deposit(amount - 1);
        }
        count++;
    }

    @Override
    public void withdraw(double amount) {
        if(count <= 3) {
            super.withdraw(amount );
        }
        else {
            super.withdraw(amount + 1);
        }
    }
    public void earnMonthlyInterest(){
        count = -1;
        deposit(getBalance() * rate);
    }

    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;
    }
}
//====================================================================
package Bank;

public class test {
    public static void main(String[] args) {
        savingsAccount person = new savingsAccount(1000);
        person.deposit(100);
        person.deposit(100);
        person.deposit(100);
        System.out.println(person.getBalance());
        person.deposit(100);
        System.out.println(person.getBalance());
        person.earnMonthlyInterest();
        person.deposit(100);
        person.deposit(100);
        person.deposit(100);
        System.out.println(person.getBalance());
    }
}

在上面的bankAccount是测试类,savingsAccount是在bankAccount上的衍生与功能完善.
在实际编程中应该应用这种建立模块功能的搭建.
3.在这里插入图片描述
4.
在这里插入图片描述

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

挽天java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值