zoukankan      html  css  js  c++  java
  • 继承与super用法

    package hello;

    public class Account {
    protected int id;
    protected double balance;
    protected double annualInterestRate;
    public Account(int id, double balance, double annualInterestRate) {
    super();
    this.id = id;
    this.balance = balance;
    this.annualInterestRate = annualInterestRate;
    }
    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;
    }
    public double getMonthlyInterest(){
    return annualInterestRate/12;
    }
    public void withdraw(double amount){
    if(balance>=amount){
    balance-=amount;
    }else{
    System.out.println("余额不足");
    }
    }
    public void despoit(double amount){
    balance+=amount;
    }
    }

    package hello;

    public class TestAccount {
    public static void main(String[] args){
    Account acct=new Account(1122,20000,0.045);
    acct.withdraw(30000);
    System.out.println("余额为:"+acct.getBalance());
    acct.withdraw(2500);
    acct.despoit(3000);
    System.out.println("余额为:"+acct.getBalance());
    System.out.println("月利率为:"+acct.getMonthlyInterest());
    }
    }

    package hello;

    public class CheckAccount extends Account {
    private double overdraft;
    public CheckAccount(int id, double balance, double annualInterestRate,double overdraft){
    super(id,balance,annualInterestRate);
    this.overdraft=overdraft;
    }
    public double getOverdraft() {
    return overdraft;
    }
    public void setOverdraft(double overdraft) {
    this.overdraft = overdraft;
    }
    public void withdraw(double amount){
    if(amount<=balance){
    balance-=amount;
    }else if(overdraft>=(amount-balance)){
    overdraft-=(amount-balance);
    balance=0;
    }else{
    System.out.println("超过可透支额度");
    }
    }
    }

    package hello;

    public class TestCheckAccount {
    public static void main(String[] args){
    CheckAccount ca=new CheckAccount(1122,20000,0.045,5000);
    ca.withdraw(5000);
    System.out.println("账户余额为:"+ca.getBalance());
    System.out.println("可透支额度为:"+ca.getOverdraft());
    }
    }

  • 相关阅读:
    网易数帆实时数据湖 Arctic 的探索和实践
    私有化场景下大规模云原生应用的交付实践
    Apache Kyuubi 在 T3 出行的深度实践
    Win7 32位原版镜像无法安装VMware Tools
    VB.NET代码转C#的方法
    ArcGIS Pro导入OSGB倾斜摄影数据
    基于倾斜摄影测量的三维建模实验
    解决QTTabBar标签不能置顶的问题
    GIS中图斑椭球面积的计算
    ISaveAs导出栅格显示异常
  • 原文地址:https://www.cnblogs.com/alhh/p/5368465.html
Copyright © 2011-2022 走看看