温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

什么是java super超类

发布时间:2026-10-26 22:18:23 来源:亿速云 阅读:107 作者:小樊 栏目:编程语言

在 Java 中,**“super 超类”**通常包含两层意思:

  1. 超类(Superclass):指被继承的类(父类)
  2. super 关键字:用于在子类中访问父类的成员或构造方法

下面分别说明。


一、什么是超类(Superclass)

超类也叫父类或基类,是被其他类继承的类。

class Animal {  // 超类
    void eat() {
        System.out.println("吃东西");
    }
}

class Dog extends Animal { // Dog 是子类
}
  • Animal 是 Dog 的超类
  • Dog 是 Animal 的子类

二、super 关键字的作用

super 用于在子类中调用父类的内容。

1. 访问父类的成员变量

class Parent {
    int x = 10;
}

class Child extends Parent {
    int x = 20;

    void show() {
        System.out.println(x);      // 20
        System.out.println(super.x); // 10
    }
}

2. 调用父类的方法

class Parent {
    void show() {
        System.out.println("父类方法");
    }
}

class Child extends Parent {
    void show() {
        super.show(); // 调用父类方法
        System.out.println("子类方法");
    }
}

3. 调用父类的构造方法(最常见)

class Parent {
    Parent(String name) {
        System.out.println("父类:" + name);
    }
}

class Child extends Parent {
    Child() {
        super("Tom"); // 必须第一行
        System.out.println("子类构造");
    }
}

⚠️ 注意:

  • super() 必须放在构造方法的第一行
  • 如果没写,Java 会默认调用 super()(父类无参构造)

三、super 和 this 的区别

关键字 含义
this 当前对象
super 父类对象

四、一句话总结

超类是父类,super 是在子类中访问父类的“桥梁”。

如果你愿意,我也可以用内存图或面试题角度再给你讲一遍。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI
助
手