ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

20220721 第一组 于芮 父类与子类的继承(第十四天)

2022-07-21 18:35:36  阅读:128  来源: 互联网

标签:balance money String 于芮 Double 第十四天 子类 password public


  小白成长记——第十四天      今天学习的一个新的概念就是---继承,这个在我们的生活中也是经常说起的一个概念,只是在计算机中将这个概念永遇乐计算机的编程中,并且,这个概念也变得更加抽象,更加不易于理解,来看一下今天的学习笔记吧!

extends-继承(在java中,是单继承,一个子类只能有一个直接父类,可以有多个间接父类)
类与类之间的关系
1.属性关系
2.参数关系
3.继承关系

继承的作用
1.继承父类的属性和方法
2.创建子类对象是,父类先被实例化,然后实例化子类
3.当一个类被实例化时,先执行实例化的直接父类和间接父类
4.子类的构造器可以有多个,但是必须和父类的构造器形式上统一

super关键字-代表调用父类的结构
this和super的区别(super--父类,不代表任何对象,this--当前类,代表方法的调用者,返回值可以用this)
1.使用super调用父类构造器时,super()必须是第一句话
使用this调用本类的构造器时,this(必须是第一句话)
2.在构造器中,如果要使用super和this调用其他构造器时,只能二选一,放在第一句

方法的重写(复写,覆盖,)
利用父类中的方法已经运算过得结果,在结果的基础上
体现的是继承的核心,为了扩展父类的功能

方法中重写的规则(前提,继承与被继承的关系)
1.访问权限 重写方法的权限不能低于被重写的方法
2.返回值类型 重写方法的返回值可以和被重写方法的返回值不同,但是必须是被重写方法的子类。
3.方法名 必须相同
4.参数列表 参数类型,参数个数,必须相同
5.重写的方法不能抛出比被重写的方法更大的异常

开发中,如果要重写,基本是一模一样,只有方法体不同

    今天的学习内容不止这些,还有一个实际的案例,就是昨天的银行系统2.0,今天的将这个系统的功能做得更加完善了一些,上代码!

 1 public class Card {
 2     private  String cardId;
 3     private  String password;
 4     private Double balance;
 5     public  Double in(Double money){
 6         return balance + money;
 7     }
 8     public Double out(Double money){
 9         return balance - money;
10     }
11     public Card(){
12 
13     }
14     public  Card(String cardId,String password,Double balance){
15         this.cardId=cardId;
16         this.password=password;
17         this.balance=balance;
18     }
19     public String getCardId(){
20         return cardId;
21     }
22     public  void setCardId(String cardId){
23         this.cardId=cardId;
24 
25     }
26     public String getPassword(){
27         return password;
28     }
29     public void setPassword(String password){
30         this.password=password;
31     }
32     public  Double getBalance(){
33         return balance;
34     }
35     public  void setBalance(Double balance){
36         this.balance=balance;
37     }
38 
39 }
 1 public class CreditCard  extends Card{
 2     private Double credits;
 3     private Double temp=0.0;
 4     public  CreditCard(){
 5 
 6     }
 7     public CreditCard(String cardId,String password,Double balabce,Double credits){
 8         super(cardId,password,balabce);
 9         this.credits=credits;
10     }
11     public Double in(Double money){
12         if(temp==0){
13             return  super.in(money);
14         }
15         if(temp>=money){
16             credits+=money;
17             temp-=money;
18             return  credits;
19         }
20         if(temp<money){
21             credits+=money;
22             setBalance(money-temp);
23             temp=0.0;
24             return  getBalance();
25         }
26         return null;
27     }
28     public  Double out(Double money) {
29         if (getBalance() >= money) {
30             return  super.out(money);
31 
32         }else if(getBalance()<money&&credits<money){
33             return  Double.valueOf(-1);
34 
35 
36         }else if(getBalance()<money&&credits>money){
37             double d=money-getBalance();
38             credits=credits-d;
39             temp=d;
40             return credits;
41         }
42         return  null;
43     }
44 }
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 Double out(Double money){
        if(getBalance()<money){
            return  super.out(money);
        } else if (getBalance()<money) {
            return  Double.valueOf(-1);
        } else if (transferAmount<money) {
            return Double.valueOf(-2);
        }
        return Double.valueOf(-3);
    }
}
public class Demo {
    public static void main(String[] args) {
      /*  Scanner in= new Scanner(System.in);
        System.out.println("请输入账号:");
        String cardId=in.next();
        System.out.println("请输入密码:");
        String password=in.next();
        System.out.println("请输入余额:");
        Double balance=in.nextDouble();*/
      DebitCard debitCard=new DebitCard("123456","123456",Double.valueOf(100),Double.valueOf(20000));
      Double in=debitCard.in(10000.0);
      in=debitCard.out(30000.0);
      if(in>=0){
          System.out.println("余额为:"+in);
      } else if (in==-1) {
          System.out.println("取款失败,余额不足!");

      } else if (in==2) {
          System.out.println("取款失败,今日取款额度不足!");

      }
      CreditCard creditCard=new CreditCard("123456","123456",1000.0,50000.0);
      creditCard.out(2000.0);
        System.out.println(creditCard.in(3000.0));


    }
}

 

标签:balance,money,String,于芮,Double,第十四天,子类,password,public
来源: https://www.cnblogs.com/dijiuzu/p/16503027.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有