• java 子类对象实例化的过程与习题


    大家往往听说  子类继承了父类就有了父类中的所有的非私有的属性,但是怎么就有了父类的属性了呢 ?且听下面分解

    子类对象实例化的过程:

    1 从结果上来看:(继承性)

      子类继承了父类以后 就获取了父类中声明的属性和方法

      创建子类对象 在堆空间中 就会加载所有父类中声明的属性

    2 从过程来上看 :

      当我们通过子类的构造器创建子类的对象是 一定会直接或间接的调用其父类的构造器 进而调用父类的父类的构造器.直到调用了java.langObject类中空参的构造器为止,正因为加载过所有的父类结构 所以才可以看到内存中有父类的结构 子类对象才可以考虑进行调用  

    明确: 虽然创建子类对象时,调用了父类的构造器 但是自始至终就创建了一个对象 即为new的子类对象

    习题:

    实验  类的继承,super 
    1、写一个名为 Account 的类模拟账户。该类的属性和方法如下图所示。该类包括的属性: 账号 id,余额 balance,年利率 annualInterestRate;包含的方法:
    访问器方法(getter 和 setter 方法),返回月利率的方法 getMonthlyInterest(),取款方法 withdraw(),存款方法 deposit()。
    Account private int id private double balance private double annualInterestRate
    public Account (int id, double balance, double annualInterestRate )
    public int getId() public double getBalance() public double getAnnualInterestRate()
    public void setId( int id) public void setBalance(double balance)
    public void setAnnualInterestRate(double annualInterestRate)
    public double getMonthlyInterest()
    public void withdraw (double amount)
    public void deposit (double amount)
    写一个用户程序测试 Account 类。在用户程序中,创建一个账号为
    1122、余额为 20000、
    年利率 4.5%的 Account 对象。使用 withdraw 方法提款 30000 元,并打印余额。 再使用 withdraw 方法提款 2500 元,使用 deposit 方法存款 3000 元,然后打印余额和月利 率。 提示:在提款方法 withdraw 中,需要判断用户余额是否能够满足提款数额的要求,如果不 能,应给出提示。 运行结果如图所示:

    2、创建 Account 类的一个子类 CheckAccount 代表可透支的账户,该账户中定义一个属性 overdraft 代表可透支限额。在 CheckAccount 类中重写 withdraw 方法,其算法如下: 如果(取款金额<账户余额), 可直接取款 如果(取款金额>账户余额), 计算需要透支的额度 判断可透支额 overdraft 是否足够支付本次透支需要,如果可以 将账户余额修改为 0,冲减可透支金额 如果不可以 提示用户超过可透支额的限额

    要求:写一个用户程序测试 CheckAccount 类。在用户程序中,创建一个账号为 1122、余 额为 20000、年利率 4.5%,可透支限额为 5000 元的 CheckAccount 对象。 使用 withdraw 方法提款 5000 元,并打印账户余额和可透支额。 再使用 withdraw 方法提款 18000 元,并打印账户余额和可透支额。 再使用 withdraw 方法提款 3000 元,并打印账户余额和可透支额。

    提示: (1) 子类 CheckAccount 的构造方法需要将从父类继承的 3 个属性和子类自己的属性全 部初始化。 (2) 父类Account的属性balance被设置为private,但在子类CheckAccount的withdraw 方法中需要修改它的值,因此应修改父类的 balance 属性,定义其为 protected。




       

    import java.util.Scanner;
    
    public class AprilFaceToAccount {
        public static void main(String[] args) {
            AprilFiveAccount account = new AprilFiveAccount(1122,20000,0.0045);
            Scanner scanner = new Scanner(System.in);
            int money = scanner.nextInt();
            account.withdraw(money);
    
    
    
        }
    }
    
    class AprilFiveAccount{
        private int id;
        private double balance;  // 余额
        private double annualInterestRate;  // 年利率
    
        public AprilFiveAccount (int id, double balance, double annualInterestRate ){
            this.id = id;
            this.balance = balance;
            this.annualInterestRate = annualInterestRate;
        }
    
        public double getMonthlyInterest(){
            // 返回月利率
            return getAnnualInterestRate()/12;
        }
    
        public void withdraw (double amount){  // 取款
            if(amount > balance){
                System.out.println("余额不足");
                System.out.println("余额为:"+balance);
    
            }else{
                balance -= amount;
                System.out.println("余额为:"+balance);
                System.out.println("月利率为:"+getMonthlyInterest());
            }
    
    
        }
    
        public void deposit (double amount){ // 存款
            if(amount <=0){
                System.out.println("输入的不合法");
            }
            balance+=amount;
            System.out.println("余额为:"+balance);
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public double getBalance() {
            return balance;
        }
    
        public void setBalance(double balance) {
            this.balance = balance;
        }
    
        public double getAnnualInterestRate() {
            return annualInterestRate;
        }
    
        public void setAnnualInterestRate(double annualInterestRate) {
            this.annualInterestRate = annualInterestRate;
        }
    }
    习题1
    class AprilSixAfternoonCheckAccount extends AprilFiveAccount{
    
        public AprilSixAfternoonCheckAccount(int id, double balance, double annualInterestRate,double overdraft) {
            super(id, balance, annualInterestRate);
            this.overdraft = overdraft;
        }
    
        private double overdraft; // 可透支额度
    
        public void withdraw(double amount){ // 重写取款方法
            double residueue = getBalance();
            if(amount < getBalance()){
                setBalance(getBalance()-amount);
                System.out.println("成功取出: "+amount+"余额为: "+getBalance()+"可透支金额:"+overdraft);
            }else if (amount > getBalance()){
                if(amount-getBalance() > overdraft){
                    System.out.println("用户超过可透支金额");
                    System.out.println("withDraw is success 余额为 : "+amount+"额度剩余: "+overdraft);
    
                }else{
                      overdraft = amount-getBalance()-overdraft ; // 透支额度校对
                    setBalance(0);
                    System.out.println("withDraw is success 余额为 : "+amount+"额度剩余: "+overdraft);
                }
            }
    
    
        }
    
    
    }
    习题2
  • 相关阅读:
    [Swift]LeetCode922.按奇偶排序数组 II | Sort Array By Parity II
    [Swift]LeetCode921.使括号有效的最少添加 | Minimum Add to Make Parentheses Valid
    [Swift实际操作]八、实用进阶-(5)通过间接代理进行对象间的消息传递
    [Swift]LeetCode927. 三等分 | Three Equal Parts
    [Swift]LeetCode928. 尽量减少恶意软件的传播 II | Minimize Malware Spread II
    我是大牛,我自豪
    程序员拓展人脉的重要性
    程序员拓展人脉的重要性
    2013年总结(3)-活动篇
    2013年总结(3)-活动篇
  • 原文地址:https://www.cnblogs.com/zhaoyunlong/p/10977979.html
Copyright © 2020-2023  润新知