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调方属性的测试
父类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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( "人走路" ); } } |
子类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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 } } |
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 调父类方法测试、
父类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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( "人走路" ); } } |
子类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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 } } |
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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(); } } |
父类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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( "人走路" ); } } |
子类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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); } } |
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 |
账户项目测试
子类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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); } } |
父类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 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(); // } } |
测试类
1 2 3 4 5 6 7 8 9 10 | 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()); } } |
测试结果
1 2 | 账户余额为 0.0 可透支余额为 4000.0 |
草都可以从石头缝隙中长出来更可况你呢
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
2020-04-08 MySQL 的查询
2019-04-08 docker 镜像仓库的安装与使用