2022-7-21 第一小组 甘源册 学习笔记

知识点掌握情况

继承(理解) super关键字(了解) 方法的重写(理解)

学习心情(心路历程)

知识点难度不高,很好理解,心情还好。

1.继承

  • 类与类之间的关系
    • 属性关系
    • 参数关系
    • 继承关系

  • Animal ,Person,Cat都是Biology的子类

  • Animal是Bilogy类直接子类

  • Person,Cat是Biology类间接子类,是Animal的直接子类

  • Java是单继承,一个子类只能有一个父类,但是可以有多个间接父类

1.1继承的概念

一个类继承另一个类的属性,方法,构造器。这个类就是被继承类的子类,被继承的类就是它的父类。

继承能继承什么

  • 父类的属性
  • 父类的方法
  • 父类的构造器
    • 父类没有无参构造时,子类也不能无参构造

1.2super关键字

  • 代表调用父类的结构
  • 面试题:this与super的区别。
    • this永远是调用当前类
    • super永远是调用父类
    • 在使用super去调用父类的构造器时,必须是第一行
    • 在使用this去调用当前类的构造器时,必须是第一行
    • 在构造器中,如果需要使用this或super调用其他构造器,就只能二选其一,而且必须是第一句话
    • this代表当前类的对象,方法的调用者
    • super不代表任何对象

1.3方法的重写(覆写,覆盖,override)

  • 我们可以利用到父类中的方法已经运算过的结果,在这个基础上进行扩展
  • 核心:体现的就是继承的核心,为了扩展父类的功能。
  • 方法重写的规则(前提:继承与被继承的关系):
    1. 访问权限:重写的方法的权限不能低于被重写的方法。(开发中权限一般都是等于)。
    2. 返回值类型:重写的方法的返回值可以和被重写方法的返回值不同,但必须是它的子类。(开发中一般就是一样)。
    3. 方法名:必须一样。
    4. 参数列表:参数类型,参数个数必须相同。
    5. 抛出异常:重写的方法不能抛出比被重写的方法更大的异常。
  • 开发中,如果要重写,基本一模一样,我们只改变方法体(90%)

练习

Demo测试类
package com.gyc.morning.test;

import java.util.Scanner;

public class Demo {
    public static void main(String[] args) {
        Debit debit = new Debit("123","1344",100000.0,20000.0);
        Credit credit = new Credit("123","1344",100000.0,50000.0);

        Scanner scanner = new Scanner(System.in);
        a:for (;;) {
            System.out.println("请选择哪张卡 1:借记卡  2:信用卡 3:结束");
            Integer anInt = scanner.nextInt();
            switch (anInt) {
                case 1:
                    System.out.println("请选择功能 1:存款  2:取款");
                    Integer anInt1 = scanner.nextInt();
                    switch (anInt1) {
                        case 1:
                            System.out.println("请输入存款额度");
                            Double anInt2 = scanner.nextDouble();
                            System.out.println("你存了-- "+anInt2+" --余额是:"+debit.in(anInt2).getBalance());
                            continue a;
                        case 2:
                            System.out.println("请输入取款额度");
                            double v = scanner.nextDouble();
                            debit=debit.out(v);
                            if (debit.getB()!=false){
                                System.out.println("你取了--"+v+"--余额为:"+debit.getBalance()+"--转账额度是:"+debit.getTransferAmount());
                            }
                            continue a;
                        default:
                            continue a;
                    }
                case 2:
                    System.out.println("请选择功能 1:存款  2:取款");
                    Integer anInt4 = scanner.nextInt();
                    switch (anInt4) {
                        case 1:
                            System.out.println("请输入存款额度");
                            Double anInt2 = scanner.nextDouble();
                            credit=credit.in(anInt2);
                            System.out.println("你存了--"+anInt2+"--余额为:"+credit.getBalance()+"--信用额度为:"+credit.getCredits());
                            continue a;
                        case 2:
                            System.out.println("请输入取款额度");
                            double v = scanner.nextDouble();
                            credit=credit.out(v);
                            if (credit.getB()!=true){
                                System.out.println("你取了--"+v+"--余额为:"+credit.getBalance()+"--信用额度为:"+credit.getCredits());
                            }
                            continue a;

                        default:
                            continue a;
                    }
                case 3:
                    System.out.println("感谢使用!");
                    break a;
                default:
                    continue a;

            }
        }
    }
}


银行卡类(父类)
package com.gyc.morning.test;

import java.util.Date;

public class BankID {
    private String username;
    private String password;
    private Double balance;

    public BankID() {
    }

    public BankID(String username, String password, Double balance) {
        this.username = username;
        this.password = password;
        this.balance = balance;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    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 BankID in(Double money){
        this.setBalance(this.getBalance()+money);
        return this;

    }
    //取
    public BankID out(Double money){
        setBalance(getBalance()-money);
        return this;
    }
}

借记卡类(子类)
package com.gyc.morning.test;

public class Debit extends BankID{
    private  Double transferAmount;
    private Boolean b;
    public Debit() {
    }

    public Double getTransferAmount() {
        return transferAmount;
    }

    public Boolean getB() {
        return b;
    }

    public void setB(Boolean b) {
        this.b = b;
    }

    public void setTransferAmount(Double transferAmount) {
        this.transferAmount = transferAmount;
    }

    public Debit(Double transferAmount) {
        this.transferAmount = transferAmount;
    }

    public Debit(String username, String password, Double balance, Double transferAmount) {
        super(username, password, balance);
        this.transferAmount=transferAmount;
    }
    public Debit out(Double money){
        setB(false);
        if (super.getBalance()<money ){
            System.out.println("余额不足");
            return this;
        }else if (money>getTransferAmount()){
            System.out.println("转账额度不够,请去柜台办理");
            return this;
        }else if (getTransferAmount()<0){
            System.out.println("转帐额度不够");
            return this;
        }
        setB(true);
            super.out(money);
            setTransferAmount(getTransferAmount()-money);
        return this;
    }
}

信用卡类(子类)
package com.gyc.morning.test;

public class Credit extends BankID {
    private Double credits;
    private Boolean b;
    Double  temp;
    public Double getCredits() {
        return credits;
    }

    public void setCredits(Double credits) {
        this.credits = credits;
    }

    public Boolean getB() {
        return b;
    }

    public void setB(Boolean b) {
        this.b = b;
    }

    public Credit() {
    }

    public Credit(Double credits) {
        this.credits = credits;
    }

    public Credit(String username, String password, Double balance, Double credits) {
        super(username, password, balance);
        this.credits = credits;
        temp=credits;

    }

    public Credit in(Double money){

        if (this.getCredits()!=temp){
            if (getCredits()+money>temp){
                Double a=money+getCredits()-temp;
                setCredits(temp);
                super.in(a);
                return this;
            }else {
                setCredits(getCredits()+money);
                return this;
            }
        }
        super.in(money);
        return this;
    }
    public Credit out(Double money){
        setB(false);
        if (money >super.getBalance() && money<(getCredits()+super.getBalance())){
            setCredits(getCredits()+super.getBalance()-money);
            super.setBalance(0.0);
            return this;
        }else if(money<super.getBalance()){
            super.out(money);
            return this;
        }

        System.out.println("信用额度不足");
        setB(true);
        return this;
    }
}

##练习结果

借记卡

信用卡

posted @ 2022-07-21 19:14  (≧∇≦)(≧∇≦)(≧∇≦)  阅读(42)  评论(0)    收藏  举报