Java基础super关键字详解

た 入场券 2023-10-08 05:01 107阅读 0赞

文章目录

    • —:super概述:
      • 示例代码01:
        • 运行结果:
    • 二:super的子类构造方法执行时必然调用父类构造方法
      • 示例代码02:
        • 运行结果:
    • 三:super实参的用法
      • 示例代码03:
        • 运行结果:
        • 内存分析图一:
      • 示例代码04:
        • 运行结果:
          • 内存分析图二:
          • 内存分析图三:
    • 四:super什么时候不能省略
      • 示例代码05:
        • 运行结果:
    • 五:super使用后后面必须有个点
      • 示例代码06:
        • 运行结果:
    • 六:使用super调用父类方法
      • 示例代码07:
        • 运行结果:

—:super概述:

1.super能出现在实例方法和构造方法中。

  • super的语法是:“super.”,“super()”
  • super不能使用在静态方法中
  • super. 大部分情况下是可以省略的

2.super();

  • 表示通过子类的构造方法调用父类的构造方法
    模拟现实世界中的这种场景:要想有儿子,需要先有父亲

2.重要结论:

当一个构造方法第一行:

  • 既没有this()又没有super()的话,默认会有一个super(); 表示通过当前子类的构造方法调用父类的无参构造方法, 所以必须保证父类的无参数构造方法是存在的

3.注意

  • this()和super()不能共存,它们都是只能出现在构造方法第一行

示例代码01:

  1. public class SuperTest01{
  2. public static void main(String[] args){
  3. new B();
  4. }
  5. }
  6. class A{
  7. //建议手动的将一个类的无参数构造方法写出来
  8. public A(){
  9. System.out.println("A类的无参构造方法!");
  10. }
  11. //一个类中如果没有手动提供任何构造方法,系统会默认提供一个无参数构造方法
  12. //一个类如果手动提供了一个构造方法,那么无参数构造系统将不再提供
  13. public A(int n){
  14. System.out.println("A类的有参构造方法!(int n)");
  15. }
  16. }
  17. class B extends A{
  18. /* public B(){
  19. super(123);
  20. System.out.println("B类的无参数构造方法");
  21. } */
  22. public B(){
  23. super();//调用父类中有参数的构造方法
  24. //super(100);
  25. this("zhangsan");
  26. System.out.println("B类的无参构造方法!");
  27. }
  28. public B(String name){
  29. super(100);
  30. System.out.println("B类的有参构造方法!(String name)");
  31. }
  32. }
运行结果:

SuperTest01

二:super的子类构造方法执行时必然调用父类构造方法

在java语言中不管是new什么对象,最后老祖宗的Object类的无参数构造方法

示例代码02:

  1. public class SuperTest02{
  2. public static void main(String[] args){
  3. new C();
  4. }
  5. }
  6. //老祖宗
  7. /* class Object(){
  8. public Object(){
  9. }
  10. } */
  11. class A{
  12. public A(){
  13. System.out.println(1);
  14. }
  15. }
  16. class B extends A{
  17. public B(){
  18. this("zhangsan");
  19. System.out.println(2);
  20. }
  21. public B(String name){
  22. System.out.println(3);
  23. }
  24. }
  25. class C extends B{
  26. public C(){
  27. this("lisi");
  28. System.out.println(4);
  29. }
  30. public C(String name){
  31. this("lisi",5);
  32. System.out.println(5);
  33. }
  34. public C(String name,int a){
  35. super(name);
  36. System.out.println(6);
  37. }
  38. }
运行结果:

在这里插入图片描述

三:super实参的用法

1、举个例子:在恰当的时间使用:super(实际参数列表);

2、注意:在构造方法执行过程中一连串调用了父类的构造方法,父类的构造方法又继续向下调用它的父类的构造方法,但是实际上,对象只创建了一个。

3、思考:“super(实参)”到底是干啥的?

  • super(实参)的作用是:初始化当前对象的父类型特征。
  • 并不是创建新对象。实际上对象只创建了1个。

4、super关键字代表什么呀?

  • super关键字代表的就是“当前对象”的那部分父类型特征。

    我继承了我父亲的一部分特征:

    1. 例如:眼睛、皮肤等.
    2. super代表的就是“眼睛、皮肤等”。
    3. “眼睛、皮肤等”虽然是继承了父亲的,但这部分是在我身上呢。

示例代码03:

  1. public class SuperTest03{
  2. public static void main(String[] args){
  3. CreditAccount ca1 = new CreditAccount();
  4. System.out.println(ca1.getActno() + " " + ca1.getBalance() + " " + ca1.getCredit() + " ");
  5. CreditAccount ca2 = new CreditAccount("123456",500000.0,"123556");
  6. System.out.println(ca2.getActno() + " " + ca2.getBalance() + " " + ca2.getCredit() + " ");
  7. }
  8. }
  9. class Account{
  10. private String actno;
  11. private double balance;
  12. public Account(){
  13. }
  14. public Account(String actno,double balance){
  15. this.actno = actno;
  16. this.balance = balance;
  17. }
  18. public String getActno() {
  19. return actno;
  20. }
  21. public void setActno(String actno) {
  22. this.actno = actno;
  23. }
  24. public double getBalance() {
  25. return balance;
  26. }
  27. public void setBalance(double balance) {
  28. this.balance = balance;
  29. }
  30. }
  31. class CreditAccount extends Account{
  32. //属性:信誉度(诚信值)
  33. //子类特有的一个特征,父类没有
  34. private String credit;
  35. public CreditAccount(){
  36. }
  37. //提供有参数的构造方法
  38. public CreditAccount(String actno,double balance,String credit){
  39. //私有的属性,只能在本类中访问
  40. /* this.actno = actno;
  41. this.balance = balance; */
  42. //以上两行代码在恰当的位置,正好可以使用:super(actno,balance);
  43. //通过子类的构造方法调用父类的构造方法
  44. super(actno,balance);
  45. this.credit = credit;
  46. }
  47. public String getCredit() {
  48. return credit;
  49. }
  50. public void setCredit(String credit) {
  51. this.credit = credit;
  52. }
  53. public CreditAccount(String credit){
  54. this.credit = credit;
  55. }
  56. }
运行结果:

在这里插入图片描述

内存分析图一:

在这里插入图片描述

示例代码04:

  1. public class SuperTest04{
  2. public static void main(String[] args){
  3. Vip v = new Vip("张三");
  4. v.shopping();
  5. }
  6. }
  7. class Customer{
  8. String name;
  9. public Customer(){
  10. }
  11. public Customer(String name){
  12. super();
  13. this.name = name;
  14. }
  15. public String getName(){
  16. return name;
  17. }
  18. public void SetName(String name){
  19. this.name = name;
  20. }
  21. }
  22. class Vip extends Customer{
  23. public Vip(){
  24. }
  25. public Vip(String name){
  26. super(name);
  27. }
  28. //super和this都不能出现在静态方法中
  29. public void shopping(){
  30. //this表示当前对象
  31. System.out.println(this.name + "在购物!");
  32. //super表示的是当前对象的父类型特征。(super是this指向的那个对象中的一块空间)
  33. System.out.println(super.name + "在购物!");
  34. System.out.println(name + "在购物!");
  35. }
  36. }
运行结果:

在这里插入图片描述

内存分析图二:

在这里插入图片描述

内存分析图三:

在这里插入图片描述

四:super什么时候不能省略

1.”this.”和“super.”大部分情况下都是可以省略的

2.this.什么时候不能省略?

  1. public void setName(String name){
  2. this.name = name;
  3. }

3.super.什么时候不能省略?

  • 父中有,子中又有,如果想在子中访问“父的特征”,super.不能省略

示例代码05:

  1. public class SuperTest05{
  2. public static void main(String[] args){
  3. Vip v = new Vip("张三");
  4. v.shopping();
  5. v.doSome();
  6. }
  7. }
  8. class Customer{
  9. String name;
  10. public Customer(){
  11. }
  12. public Customer(String name){
  13. super();
  14. this.name = name;
  15. }
  16. public String getName(){
  17. return name;
  18. }
  19. public void SetName(String name){
  20. this.name = name;
  21. }
  22. public void doSome(){
  23. System.out.println(this.name + "在购物!"); //张三在购物
  24. System.out.println(name + "在购物!");//张三在购物
  25. //super表示的是当前对象的父类型特征。(super是this指向的那个对象中的一块空间)
  26. // System.out.println(super.name + "在购物!");//编译报错:找不到符号
  27. }
  28. }
  29. class Vip extends Customer{
  30. //假设子类也有一个同名属性
  31. //java中允许在子类中出现和父类一样的同名变量/同名属性
  32. String name;//实例变量
  33. public Vip(){
  34. }
  35. public Vip(String name){
  36. super(name);
  37. //this.name = null;当前对象默认值为空
  38. }
  39. //super和this都不能出现在静态方法中
  40. public void shopping(){
  41. //this表示当前对象
  42. System.out.println(this.name + "在购物!");//null在购物
  43. //super表示的是当前对象的父类型特征。(super是this指向的那个对象中的一块空间)
  44. System.out.println(super.name + "在购物!");//张三在购物
  45. System.out.println(name + "在购物!");//null在购物
  46. }
  47. }
运行结果:

在这里插入图片描述

五:super使用后后面必须有个点

super 不是引用。super也不保存内存地址,super也不指向任何对象
super 只是代表当前对象内部的那一块父类型的特征。

示例代码06:

  1. public class SuperTest06{
  2. //实例方法
  3. public void doSome(){
  4. //输出“引用.”的时候,会自动调用引用的toString()方法
  5. System.out.println(this.toString());
  6. //SuperTest06@15db9742
  7. System.out.println(this);
  8. // System.out.println(super);//编译错误:需要“.”
  9. }
  10. //this和super不能使用在static静态方法中
  11. /* public Static void doOther(){
  12. System.out.println(this);
  13. System.out.println(super);
  14. }
  15. */
  16. //静态方法 主方法
  17. public static void main(String[] args){
  18. SuperTest06 st = new SuperTest06();
  19. st.doSome();
  20. }
  21. }
运行结果:

在这里插入图片描述

六:使用super调用父类方法

在父类和子类中有同名的属性,或者说有相同的方法, 如果此时想在子类中访问父类中的数据,必须使用“super.”加以区分。

①super.属性名 【访问父类中的属性】
②super.方法名(实参) 【访问父类的方法】
③super(实参) 【调用父类的构造方法】

示例代码07:

  1. public class SuperTest07{
  2. public static void main(String[] args){
  3. Cat c = new Cat();
  4. c.yiDong();
  5. }
  6. }
  7. class Animal{
  8. public void move(){
  9. System.out.println("Animal move");
  10. }
  11. }
  12. class Cat extends Animal{
  13. //对move进行重写
  14. public void move(){
  15. System.out.println("Cat move");
  16. }
  17. //单独编写一个子类特有的方法
  18. public void yiDong(){
  19. this.move();
  20. move();
  21. //super. 不仅可以访问属性,也可访问方法
  22. super.move();
  23. }
  24. }
运行结果:

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,107人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java super关键字

    super 关键字与[this][] 类似,this 用来表示当前类的实例,super 用来表示父类。 super 可以用在子类中,通过点号(.)来获取父类的成员变量

    相关 JAVA基础Super()详解

    什么是super? 是直接父类对象的引用。可以通过super来访问父类中被子类覆盖的方法或属性。 如果没有super存在的问题: 继承中子类可能会重写父类的方法,子类