2022-07-21 第二小组 张鑫 学习笔记
实训第十三天 继承
1.学习重点
1.继承
2.super关键字
3.重写规则
4.链式规则
2.学习心得
今天因为身体原因,白天的课没有上,原本我想晚上来补,结果晚上老师给我们所有同学一个大惊喜!!出去放松吃烧烤,今天不想其他的事,开心放松就完了!今天所学的内容明天一定要弄会!
3.学习重点
继承
类与类之间的关系:
1.属性关系
2.参数关系
3.继承关系
extends
在Java中,继承是单继承,一个子类只能有一个直接父类,但是可以有多个间接父类
extends后面只能有一个类。
继承了父类的属性
继承了父类的方法
创建子类对象时,父类先被实例化,再去实例化子类
当一个类被实例化时,一定会先实例化它的直接和间接父类
子类的构造器可以有多个,但是必须和父类的构造器形式上统一
super关键字
代表调用父类的结构(属性,方法,构造器)
在子类中当使用super调用父类的构造器时,super(age)必须是第一句话
在当前类使用this调用本类的构造器时,this(name )必须是第一句话
在构造器中,如果使用super或this调用其他构造器,只能二选一,而且必须是第一句话
super指向父类,不代表任何对象
this指向本类,代表当前类的对象,代表方法的调用者
重写规则
子类可以重写父类的写法
方法的重写,我们可以利用到父类中方法已经运算过的结果,在结果的基础上进行扩展
方法的重写,体现的是继承的核心,就是为了拓展父类的功能
方法的重写的规则
前提:继承与被继承的关系
1.访问权限:重写的方法的权限不低于被重写方法的权限,开发中一般都是等于
2.返回值类型:重写的方法的返回值可以和被重写的方法不同,但是必须是被重写方法的返回值的子类,开发中一般都是相同的
3.方法名:必须相同
4.参数列表:参数类型,参数个数必须相同
5.抛出异常:重写的方法不能抛出比被重写的方法更大的异常
开发中,如果要重写,基本就是一摸一样,我们只变方法体
案例:
有一个银行卡类,有账号和密码,余额,有存款和取款的方法。
创建一个借记卡类,继承于银行卡,转账额度。
存款和取款的方法,取款的时候,
如果超过了20000,则提示需要去柜台拿身份证办理!!!
创建一个信用卡类,继承于银行卡,信用额度。
有存款和取款的方法。
取款:如果余额大于0,则从余额中扣,
如果余额小于等于0,则会走信用额度。
余额:100,信用额度:50000,要取钱数:1000
card 类
public class Card {
private String cardId;
private String password;
private Double balance;
public Card in(Double money) {
balance += money;
this.setBalance(balance);
return this;
}
public Card out(Double money) {
balance -= money;
Card card = new Card(cardId,password,balance);
return card;
}
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;
}
}
Creditcard类
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 CreditCard in(Double money) {
// 信用卡的存款,首先要还额度,其次此时存款
if(temp == 0.0){
return null;
}
// 先还额度,剩下的才是余额
if(temp >= money) {
credits += money;
temp -= money;
return null;
}
if(temp < money) {
credits += temp;
setBalance(money - temp);
temp = 0.0;
return null;
}
return null;
}
public CreditCard out(Double money) {
// 取款先走余额,再走信用额度
/*
1、余额,余额大于要取的钱数,直接走余额
2、没有余额,直接走信用额度
3、有点余额,余额小于要去的钱数,信用额度是大于要取的钱数
balance=1000,credits=50000,money=50500, 取60000
money > balance
money > balance + credits:取款失败。
*/
if(getBalance() >= money) {
return null;
}else if(getBalance() < money && credits < money){
return null;
}else if(getBalance() < money && credits > money) {
double d = money - getBalance();
credits = credits - d;
temp = d;
return null;
}
return null;
}
}
Debitcard类
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 DebitCard out(Double money) {
DebitCard debitCard = new DebitCard();
// 借记卡的取款,除了要考虑余额是否足够
// 转账额度的问题。20000
if(getBalance() >= money && transferAmount >= money){
// 说明余额足够,直接调用父类的方法
Card card = super.out(money);
// 把父类的属性设置给子类
debitCard.setCardId(card.getCardId());
debitCard.setPassword(card.getPassword());
debitCard.setBalance(card.getBalance());
return debitCard;
}else if(getBalance() < money){
debitCard.setBalance(Double.valueOf(-1));
return debitCard;
}else if(transferAmount < money) {
debitCard.setBalance(Double.valueOf(-2));
return debitCard;
}
return null;
}
}
public class Demo {
public static void main(String[] args) {
DebitCard debitCard = new DebitCard("123456","123465",100.0,20000.0);
Card card = debitCard.in(2000.0);
System.out.println("存款成功:" + card.getCardId() + "的余额为:" + card.getBalance());
DebitCard d = debitCard.out(5000.0);
if(d.getBalance() >= 0){
System.out.println("取款成功:" + d.getCardId() + "的余额为:" + d.getBalance());
}else if(d.getBalance() == -1){
System.out.println("余额不足!");
}else if(d.getBalance() == -2){
System.out.println("取款额度不足!");
}
}
}