继承性与super的使用练习
练习1:
Account:
package com.aff.sup;
public class Account {
private int id;// 账号
private double balance;// 余额
private 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 this.annualInterestRate / 12;
}
// 取钱
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("成功取出" + amount);
} else {
System.out.println("余额不足");
}
}
// 存钱
public void deposit(double amount) {
balance += amount;
System.out.println("成功存入" + amount);
}
}
TestAccount:
package com.aff.sup; //写一个用户测试Account类, 在用户程序中创建一个账号为1122,余额20000,年利率4.5%的Account对象
//使用withdraw方法提款30000,并打印个余额
//再使用withdraw 方法提款2500 使用deposit方法存款3000,然后打印余额和月利率
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.deposit(3000);
System.out.println("当前余额:" + acct.getBalance() + "月利率:" + acct.getMonthlyInterest());
}
} 输出结果:
余额不足
当前余额:20000.0
成功取出2500.0
成功存入3000.0
当前余额:20500.0月利率:0.00375
练习2:创建Acount类的一个子类CheckAccount代表可透支账户,该账户中定义一个属性overdraft代表可透支余额,
在CheckAccount类中重写withdraw方法,
其算法如下:
如果(取款金额<账户余额), 可直接取款
如果(取款金额>账户余额),就散需要透支的额度,判断可透支overdraft是否足够支付本次透支需要,
如果可以将账户余额修改为0,重减可透支金额
如果不可以,提示用户超过可透支限额
Account:
package com.aff.sup;
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 this.annualInterestRate / 12;
}
// 取钱
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("成功取出" + amount);
} else {
System.out.println("余额不足");
}
}
// 存钱
public void deposit(double amount) {
balance += amount;
System.out.println("成功存入" + amount);
}
}
CheckAccount:
package com.aff.sup;
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 (balance >= amount) {
balance -= amount;
System.out.println("成功取出" + amount);
} else if (overdraft >= amount - balance) {
overdraft -= (amount - balance);
balance = 0;
} else {
System.out.println("超过可透支限额");
}
}
}
TestCheckAccount:
package com.aff.sup;
//写一个用户测试Account类, 在用户程序中创建一个账号为1122,余额20000,年利率4.5%的CheckAccount对象
//使用withdraw方法提款5000,并打印个余额与可透支额度
//再使用withdraw 方法提款18000, 并打印个余额与可透支额度
//再使用withdraw 方法提款3000 ,并打印个余额与可透支额度
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() + "可透支额度:" + ca.getOverdraft());
ca.withdraw(18000);
System.out.println("当前余额:" + ca.getBalance() + "可透支额度:" + ca.getOverdraft());
ca.withdraw(3000);
System.out.println("当前余额:" + ca.getBalance() + "可透支额度:" + ca.getOverdraft());
}
} 输出结果:
成功取出5000.0
当前余额:15000.0可透支额度:5000.0
当前余额:0.0可透支额度:2000.0
超过可透支限额
当前余额:0.0可透支额度:2000.0
继承性与super的使用练习的更多相关文章
- JAVASE(九)面向对象特性之 : 继承性、方法重写、关键字super、
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.继承性 1.1 为什么要类的继承性?(继承性的好处) ①减少了代码的冗余,提高了代码的复用性:②更好 ...
- java 面向对象(十四):面向对象的特征二:继承性 (三) 关键字:super以及子类对象实例化全过程
关键字:super 1.super 关键字可以理解为:父类的2.可以用来调用的结构:属性.方法.构造器3.super调用属性.方法:3.1 我们可以在子类的方法或构造器中.通过使用"supe ...
- Objective-C语言继承性
• 继承性是面向对象的重要概念之一,子类能够继承父类的某些方法和成员变量.作用域限定符为private 的成员变量是不可以被继承的.子还可以重写父类的方法. • 继承是单继承,要多继承引入了协议 •子 ...
- python高级编程之访问超类中的方法:super()
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #超类01 #它是一个内建类型,用于访问属于某个对象超类特性 pri ...
- java封装性、继承性及关键字
方法的参数传递(重点.难点)1.形参:方法声明时,方法小括号内的参数 实参:调用方法时,实际传入的参数的值 2.规则:java中的参数传递机制:值传递机制 1)形参是基本数据类型的:将实参的值传递 ...
- Java中的继承性特性
继承性是java中的第二特性之一.而继承性最为关键的地方为:代码重用性的问题,利用继承性可以从已有的类中继续派生出新的子类,也可以利用子类扩展出更多的操作功能. 继承性的实现代码为:class 子类 ...
- python第四十四课——继承性之单继承
2.继承性 继承: 使用场景: 1).生活层面:... 2).计算机层面: 两部分组成,一部分我们称为父类(基类.超类.superclass),另一部分我们称为子类(派生类.subclass), 子类 ...
- Java_面向对象中的this和super用法
this: 1.使用在类中,可以用来修饰属性.方法.构造器 2.表示当前对象或者是当前正在创建的对象 3.当形参与成员变量重名时,如果在方法内部需要使用成员变量,必须添加 this 来表明该变量时类成 ...
- Java学习:面向对象的三大特征:封装性、继承性、多态性之继承性
面向对象的三大特征:封装性.继承性.多态性. 继承 继承是多态的前提 ,如果没有继承,就没有多态. 继承主要解决的问题就是:共性抽取. 继承关系当中的特点: 子类可以拥有父类的“内容” 子类还可以拥有 ...
随机推荐
- 首次使用AWS服务器EC2
AWS有一年的免费套餐,这个便宜我得占. 申请的时候需要填写银行卡,AWS暂不支持储蓄卡,只好绑信用卡了. 创建EC2实例之后,下一个要解决的问题就是远程root访问. 1. 修改安全组设置 2. s ...
- 补 第三场多校杭电 费用流 K Subsequence
K Subsequence 这个题目是这个人想吃东西,但是他每次吃的都是他的美味值都必须不递减,可以吃k次,问这个最大的美味值是多少. 这个是一个比较明显的费用流,建图也很好建,但是呢,这个题目卡sp ...
- 线段树 I - Transformation 加乘优先级
I - Transformation Yuanfang is puzzled with the question below: There are n integers, a 1, a 2, …, a ...
- 5G新基建到来,图扑推出智慧路灯三维可视化方案
前言 作为智慧城市的重要组成部分,智慧灯杆管理系统采用信息化.数字化手段,把路灯及城市景观照明等各种不同对象的监控和数据采集及处理融于一体, 为城市管理者进行城市管理.进行科学决策提供了强有力的手段. ...
- Day_09【常用API】扩展案例6_将用户给定的字符串首个字符大写,并分别加上"set"和"get"输出
定义如下方法public static String getPropertyGetMethodName(String property) (1)该方法的参数为String类型,表示用户给定的成员变量的 ...
- Day_11【集合】扩展案例2_使用普通for循环获取集合中索引为3的元素并打印,统计集合中包含字符串"def"的数量,删除集合中的所有字符串",将集合中每个元素中的小写字母变成大写字母def",
分析以下需求,并用代码实现 1.定义ArrayList集合,存入多个字符串"abc" "def" "efg" "def" ...
- Liunx常用操作(五)-如何查询文档中的冒号与引号
liunx下面有如下一段包含json格式的文档 一.单查所有冒号: .txt | grep [:] 结果如下: 二.单查所有引号: 这里需要转义 .txt | grep [\"] 三.gre ...
- [ACdream 1212 New Year Bonus Grant]贪心
题意:员工之间形成一棵树,上级可以给下级发奖金,任何一个人最多可以给一个下级发,并且发了奖金后就不能接受奖金.求总共最多可以产生多少的奖金流动 思路:每次选择没有下级并且有上级的员工a,令它的上级为b ...
- linux(ubuntu) 1045, "Access denied for user 'root'@'localhost' (using password: YES)"
问题现象: 最近使用 flask 的 sqlalchemy 框架,在链接数据库(mysql)时出现报错 sqlalchemy.exc.OperationalError: (pymysql.err.Op ...
- go 函数 方法 接口
概论 函数 方法 接口 概论 方法在编译时静态绑定,依托于具体的类型 接口对应的方法是在运行时动态绑定 进程内初始化顺序 初始化导入包的常量和变量(可以导出的变量)--->包的init函数,不同 ...