学习心得

今天练习了一个关于信用卡和借记卡的一道题,要运用到继承的知识,子类可以调用父类的方法和属性,一定要分清每一个类里面取得名字都代表什么意思,写程序是要让别人看懂,注意分清大小写。

心情

相比于前一天,对封装的了解更多了,感觉反应时间变短了,今天听同学讲解有一种醍醐灌顶的感觉,前一天晚上老师讲的也很细,适合我这种初学者。

 

面对对象,继承:👇👇👇
类与类之间的关系:
1.属性关系
2.参数关系
3.继承关系

例如下面三个阶层:
生物      一个一个继承,extends 后面加你要选择继承的对象
动物
人  猫

基类
  Person ,cat,Animal可以叫Biology类的子类
  Animal叫做Biology的直接子类
  Person,cat是Biology的间接子类
超类
  Person,cat的直接父类是Animal
  Persoon,cat的间接父类是Biology

  extends 继承
  在Java中继承是单继承,一个子类只能有一个直接父类,但可以有很多间接父类
  extends后面只能写一个类,


  继承了父类的属性
  继承了父类的方法

  创建子类对象时,父类先被实例化,再实例化子类
  当一个类被实例化时,一定会先实例化他的直接父类和间接父类
  父类里要是没有无参构造器,子类里也不能有。之类的构造器可以有多个,但是必须和父类的构造器形式上统一


  super关键字:
  代表调用父类结构(属性,方法,构造器)


  面试题:thissuper的区别?
  在子类中使用super调用父类构造器时,必须是第一句话
  在当前类使用this调用本类的构造器时也是必须写在第一句话
  构造器中,他俩二选一,不能同时出现
  super指向的父亲,不代表任何对象,只能调属性 方法,代表不了对象
  this指向的本类,代表当前类的对象,方法的调用者 

 

  方法的重写  (覆写,覆盖override)
  子类可以重写父类的方法
  方法的重写,我们可以利用到父类的方法已经运算完的结果
  体现了继承的核心,就是为了拓展父类的功能


方法重写的规则前提,继承与被继承的关系
1.访问权限  重写的方法的权限不能低于被重写的方法,开发中一般都是等于
2.返回值类型   重写的方法的返回值可以与被重写的方法不同,
3.方法名   必须相同
4.参数列表   参数类型,参数个数必须相同
5.抛出异常:重写的方法不能抛出比被重写的方法更大的异常
开发中,如果要重写,基本就是一模一样,我门只变方法

🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱🔱

💥💥💥练习题:

*  有一个银行卡类,有账号和密码,余额,有存款和取款的方法。
*  创建一个借记卡类,继承于银行卡,转账额度。
*      存款和取款的方法,取款的时候,
*      如果超过了20000,则提示需要去柜台拿身份证办理!!!
*  创建一个信用卡类,继承于银行卡,信用额度。
*      有存款和取款的方法。
*      取款:如果余额大于0,则从余额中扣,如果余额小于等于0,则会走信用额度。
*      余额:100,信用额度:50000,要取钱数:1000

先创建一个父类:

public class Card {

    private String cardId;
    private String password;
    private Double balance;

    public Double in(Double money) {
        balance += money;
        return balance;
    }

    public Double out(Double money) {
        balance -= money;
        return balance;
    }

    public Card() {
    }

    public Card(String cardId, String password, Double balance) {
        this.cardId = cardId;
        this.password = password;
        this.balance = balance;
    }

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public Double getBalance() {
        return balance;
    }


    public void setBalance(Double balance) {
        this.balance = balance

 

第一个信用卡子类

public class CreditCard extends Card {


    private Double credits;


    private Double temp = 0.0;//离你那个卡初始能拿走的钱差多少,正数显示
    public CreditCard() {
    }


    public CreditCard(String cardId, String password, Double balance, Double credits) {
        super(cardId, password, balance);
        this.credits = credits;
    }


    public Double in(Double money) {
        // 信用卡的存款,首先要还额度,其次此时存款
        if(temp == 0.0){
            return super.in(money);
        }
        // 先还额度,剩下的才是余额
        if(temp >= money) {
            credits += money;
            temp -= money;//欠的钱变少了所以减了
            return credits;
        }
        if(temp < money) {
            credits += temp;
            setBalance(money - temp);
            temp = 0.0;
            return getBalance();
        }
        return null;
    }


    public Double out(Double money) {
        // 取款先走余额,再走信用额度
        if(getBalance() >= money) {
            return super.out(money);
        }else if(getBalance() < money && credits < money){
            return Double.valueOf(-1);//就是返回个值,-1就是一种情况,代表一下
        }else if(getBalance() < money && credits > money) {
            double d = money - getBalance();
            credits = credits - d;
            temp = d;
            return credits;
        }
        return null;

 

第二个借记卡子类

public class DebitCard extends Card {


    // 转账额度
    private Double transferAmount;//这个单词就是起个名字 转账金额


    public DebitCard() {
    }


    public DebitCard(String cardId, String password, Double balance,Double transferAmount) {
        super(cardId, password, balance);
        this.transferAmount = transferAmount;
    }


    public Double out(Double money) {
        // 借记卡的取款,除了要考虑余额是否足够
        // 转账额度的问题。20000
        if(getBalance() >= money && transferAmount >= money){
            // 说明余额足够,直接调用父类的方法
            return super.out(money);
        }else if(getBalance() < money){
            return Double.valueOf(-1);
        }else if(transferAmount < money) {
            return Double.valueOf(-2);
        }
        return Double.valueOf(-3);

最后Demo里面执行一下

public class Demo {


    public static void main(String[] args) {
        //借记卡
//        DebitCard debitCard = new DebitCard("123456","123456",Double.valueOf(100),Double.valueOf(20000));
//        Double in = debitCard.in(1000000.0);
//        in = debitCard.out(300000.0);
//        if (in >= 0) {
//            System.out.println("余额为:" + in);
//        }else if(in == -1){
//            System.out.println("取款失败,余额不足!");
//        }else if(in == -2){
//            System.out.println("取款失败,今日取款额度不足!");
//        }
        //信用卡
        CreditCard creditCard = new CreditCard("123456","123465",1000.0,50000.0);
        // 余额为0,额度还剩49000,欠了1000
        creditCard.out(2000.0);
        System.out.println(creditCard.in(3000.0));
    }
}
🧡💚💙💜🤎🧡💛💚💙💜🤎🧡💛💚💙💜🤎🧡💛💚💙💜🤎
 
🧡💚💙💜🤎🧡💛💚💙💜🤎🧡💛💚💙💜🤎🧡💛💚💙💜🤎
posted on 2022-07-21 19:51  骐琳  阅读(30)  评论(0编辑  收藏  举报

你点我就回上面去了ヾ(≧O≦)〃嗷~