• Java基础之继承性与super


    super 关键字的使用
    super 理解为父类可以调属性方法构造器
    super 的使用
    在子类的方法或构造器中使用。通过“super.属性”或者“super.方法”的方式显示的调用
    在父类中声明的属性或者方法。但是,通常情况下习惯省略super关键字
    当子类和父类中定义了同名的属性时,想要调用父类的属性需要使用“super.属性”调用父类中的属性
    当子类重写父类方法后想在子类中调用父类的方法就需要“super.方法名”调用父类的方法
    super 构造器的调用
    可以在子类的构造器中显示的使用“super(形参列表)”调用父类的构造器
    “super(形参列表)”的使用必须出现在,子类构造器的首行
    在构造器中只能调一次父类的构造器针对“this(形参列表)”与super(形参列表)只能二选一
    在构造器的首行没有声明“this(形参类别)”或“super(形参列表)”,默认调用的时父类中空参构造器“super()”
    在类的多个构造器中至少有一个类的构造器使用了super(构造器),调用父的构造器
    子类对象实例化的全过程
    1.从结果上来看(继承性)
    子类继承父类后,就获取了父类中声明的属性与方法
    创建子类对象在堆空间中加载所有父类中声明的属性和对象
    2.过程上来看
    通过子类的构造器创建子类对象时,一定会直接或者间接调用父类的构造器,进而调用父类的父类的构造器
    直到调用了java.lang.Object类中空参构造器为止,正因为加载过所有父类的结构,所以才可以看到内存中所有父类
    的结构、子类对象才可以考虑调用
    虽然创建子类对象时,调用了父类的构造器,但自始至终只创建一个对象


    super调方属性的测试
    父类
    package com.chenxi.java3;
    
    public class Person {
        String name ;
        int age;
        int id= 1001;
        public  Person(){
    
        }
        public Person(String name){
            this.name= name;
        }
        public Person(int age){
            this.age= age;
        }
        public Person(String name,int age){
            this.age=age;
            this.name= name;
        }
        public void ead(){
            System.out.println("四圣汇诛仙");
        }
        public  void walk(){
            System.out.println("人走路");
        }
    
    }
    

      子类

    package com.chenxi.java3;
    public class Student  extends  Person{
        String major;
        int id =1002;
        public Student(){
    
        }
        public Student(String major){
            this.major=major;
        }
        public void ead(){
            System.out.println("分辨忠与奸");
        }
        public  void  study(){
            System.out.println("哪吒");
        }
        public void show(){
            //name 表示当前对象。age表示调父类中对象
            System.out.println(this.name+super.age);
            System.out.println(id);//子类属性ID
            System.out.println(super.id);//super父类属性中ID
        }
    }
    

      测试类

    
    
    package com.chenxi.java3;
    /*
    super 关键字的使用
    1.super理解为父类的
    2.super 可以调用属性方法构造器
     */
    
    public class StudentTewst {
        public static void main(String[] args) {
            Student s1 = new Student();
            s1.show();
        }
    }
    
    测试
    
    null0
    1002
    
    
    

      super 调父类方法测试、

    父类

    package com.chenxi.java3;
    
    public class Person {
        String name ;
        int age;
        int id= 1001;
        public  Person(){
    
        }
        public Person(String name){
            this.name= name;
        }
        public Person(int age){
            this.age= age;
        }
        public Person(String name,int age){
            this.age=age;
            this.name= name;
        }
        public void ead(){
            System.out.println("四圣汇诛仙");
        }
        public  void walk(){
            System.out.println("人走路");
        }
    
    }
    

      子类

    package com.chenxi.java3;
    public class Student  extends  Person{
        String major;
        int id =1002;
        public Student(){
    
        }
        public Student(String major){
            this.major=major;
        }
        public void ead(){
            System.out.println("分辨忠与奸");
        }
        public  void  study(){
            System.out.println("哪吒");
            super.ead();//父类方法
            this.ead();//子类方法
    
        }
        public void show(){
            //name 表示当前对象。age表示调父类中对象
           // System.out.println(this.name+super.age);
            System.out.println(id);//子类属性ID
            System.out.println(super.id);//super父类属性中ID
        }
    }
    

      测试类

    package com.chenxi.java3;
    /*
    super 关键字的使用
    1.super理解为父类的
    2.super 可以调用属性方法构造器
     */
    
    public class StudentTewst {
        public static void main(String[] args) {
            Student s1 = new Student();
            s1.show();
            s1.study();
        }
    }
    

      父类

    package com.chenxi.java3;
    
    public class Person {
        String name ;
        int age;
        int id= 1001;
        public  Person(){
    
        }
        public Person(String name){
            this.name= name;
        }
        public Person(int age){
            this.age= age;
        }
        public Person(String name,int age){
            this.age=age;
            this.name= name;
        }
        public void ead(){
            System.out.println("四圣汇诛仙");
        }
        public  void walk(){
            System.out.println("人走路");
        }
    
    }
    

      子类

    package com.chenxi.java3;
    public class Student  extends  Person{
        String major;
        int id =1002;
        public Student(){
    
        }
        public Student(String name,int age,String major){
            super(name,age);//表示调用父类的参数构造器name和age
            this.major=major;
        }
        public Student(String major){
            this.major=major;
        }
        public void ead(){
            System.out.println("分辨忠与奸");
        }
        public  void  study(){
            System.out.println("哪吒");
            super.ead();//父类方法
            this.ead();//子类方法
    
        }
        public void show(){
            //name 表示当前对象。age表示调父类中对象
           // System.out.println(this.name+super.age);
            System.out.println(id);//子类属性ID
            System.out.println(super.id);//super父类属性中ID
            System.out.println("父类age"+super.age+"父类name"+super.name);
        }
    }
    

      测试类

    public class StudentTewst {
        public static void main(String[] args) {
    //        Student s1 = new Student();
    //        s1.show();
    //        s1.study();
            Student s2 = new Student("chenxi",9,"玉虚法术");
            s2.show();
        }
    }
     
    测试结果
    1002
    1001
    父类age9父类namechenxi
    

       账户项目测试

    子类

    package com.chenxi.java3;
    
    public class Account {
        private int id;
        private double balance;
        private double annualInterestRate;
        public Account (int id, double balance, double annualInterestRate ){
            this.id= id;
            this.balance= balance;
            this.annualInterestRate=annualInterestRate;
    
        }
    //    public Account(){
    //
    //    }
        public int getId(){
            return id;
        }
    
        public double getBalance() {
            return balance;
        }
    
        public double getAnnualInterestRate() {
            return annualInterestRate;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public void setBalance(double balance) {
            this.balance = balance;
        }
    
        public void setAnnualInterestRate(double annualInterestRate) {
            this.annualInterestRate = annualInterestRate;
        }
        public  double getMonthlylnterest(){
            return annualInterestRate/12;
        }
    
        /**
         *
         * @param amount
         * 取款操作
         */
        public void withdraw(double amount){
            if (balance >= amount){
                balance-=amount;
            }else {
                System.out.println("余额不足!");
            }
        }
        public void  deposit(double amount){
            balance+=amount;
            System.out.println("存钱成功,存入金额"+amount+"共计"+balance);
    
    
        }
    
    }
    

      父类

    package com.chenxi.java3;
    /*
    、创建 Account 类的一个子类 CheckAccount 代表可透支的账户,
    该账户中定义一个属性 overdraft 代表可透支限额。
    在 CheckAccount 类中重写 withdraw 方法,其算法如下:
     如果(取款金额<账户余额),
     可直接取款 如果(取款金额>账户余额),
      计算需要透支的额度 判断可透支额 overdraft 是否足够支付本次透支需要,
      如果可以  将账户余额修改为 0,
      冲减可透支金额 如果不可以  提示用户超过可透支额的限额
    
     */
    public class CheckAccount extends Account{
        private double overdraft;//可透支限额
    //    public CheckAccount(){
    //
    //    }
        public CheckAccount(int id, double balance, double annualInterestRate ,double overdraft){
            super(id,balance,annualInterestRate);//super构造器的调用
            this.overdraft= overdraft;
        }
        public void withdraw(double amount){
         if (getBalance()>=amount){
             setBalance(getBalance()-amount);
            }
         else if((overdraft+getBalance())>=amount){
              //  setBalance(0);
                overdraft -= amount;
                overdraft += getBalance();
                setBalance(0);
             //   return;
    
         }
    //        else  if (overdraft>=amount-getBalance()){
    //            overdraft-= (amount-getBalance());
    //           setBalance(0);
    //        }
            else {
             System.out.println("账户余额不足,取款失败");
            }
         }
    
        public double getOverdraft() {
            return overdraft;
        }
    
        public void setOverdraft(double overdraft) {
            this.overdraft = overdraft;
        }
    //    public void  deposit(double amount) {
    //        overdraft+=amount;
    //
    //        setBalance();
    //    }
    }
    

      测试类

    package com.chenxi.java3;
    
    public class CheckAccountTest {
        public static void main(String[] args) {
            CheckAccount checkAccount= new CheckAccount(1122,20000,0.0089,5000);
            checkAccount.withdraw(21000);
            System.out.println("账户余额为"+checkAccount.getBalance());
            System.out.println("可透支余额为"+checkAccount.getOverdraft());
        }
    }
    

      测试结果

    账户余额为0.0
    可透支余额为4000.0
    

      

      





    草都可以从石头缝隙中长出来更可况你呢
  • 相关阅读:
    ESP32环境搭建(arduino)
    uPyCraftIDE的介绍以及使用
    ESP32项目汇总
    MicroPython固件烧写
    Server 架构
    base家族在线解密工具
    第1年2月24日 iOS相机旋转问题 为什么UIimage.ciimage值为空
    第1年2月23日 du -sh * | sort -n 统计当前文件夹(目录)大小,并按文件大小排序
    第1年2月14日 mac 运行java
    第1年2月10日 检查iOS app证书过期时间信息
  • 原文地址:https://www.cnblogs.com/rdchenxi/p/14631207.html
Copyright © 2020-2023  润新知