Java 笔记分享 —— super 关键字

目录

1.1 前言

1.2 作为实例父类直接调用父类中的实例变量和方法

1.3 特别的 super()

1.4 总结


1.1 前言

super 关键字与其说是超级关键字,更应该说是超类关键字,它的用法和超类(也就是父类)息息相关。

我们在创建子类的实例的同时,也会隐式地创建父类的实例,所以 super 可以作为被实例化的父类直接引用父类对象。但要注意的是,super 关键字是不能在 static 方法中使用的

1.2 作为实例父类直接调用父类中的实例变量和方法

class Parent {
    
    String str = "the parent class's string";  // 父类实例变量

    void method() {  // 父类方法

        System.out.println("the parent class's method");

    }

}

class test1 extends Parent {

    String str = "the child class's string";  // 子类实例变量

    void method() {  // 子类方法

        System.out.println("the child class's method");

    }

    void display() {  // 显示父类和子类中的以上对象

        // 实例变量
        System.out.println(super.str);  // 打印父类中的实例变量,用 super 可以直接引用
        System.out.println(str);  // 打印子类中的实例变量

        // 方法
        super.method();  // 用 super 直接调用父类方法
        method();  // 调用子类方法

    }

    public static void main( String args[] ) {  // 主方法是静态方法,super 关键字无法在静态方法中使用

       
        test1 t = new test1();  // 实例化子类

        t.display();

    }

}
运行结果:
    the parent class's string
    the child class's string
    the parent class's method
    the child class's method

1.3 特别的 super()

 super() 可用于直接调用父类构造函数,但只能在子类构造函数中使用,且必须作为第一条语句

class Parent {

    Parent() {  // 父类默认构造函数

        System.out.println("the default constructor in parent class");

    }

}

class test1 extends Parent {

    test1() {  // 子类默认构造函数

        super();
        System.out.println("the default constructor in child class");

    }

    public static void main( String args[] ) {  // 主方法是静态方法,super 关键字无法在静态方法中使用

        // 实例化子类并调用子类默认构造函数
        test1 t = new test1();

    }

}
运行结果:
    the default constructor in parent class
    the default constructor in child class

调用父类有参构造函数实例

class Parent {

    Parent() {  // 默认无参构造函数

        System.out.println("the default constructor in parent class");

    }

    Parent ( String str ) {  // 有参构造函数

        System.out.println(str);

    }

}

class test1 extends Parent {

    test1() {
        
        super("the constructor with parameter in parent class");  // 调用父类中的参数构造函数
        System.out.println("the default constructor in child class");

    }

    public static void main( String args[] ) {  // 主方法是静态方法,super 关键字无法在静态方法中使用

        // 实例化子类并调用子类默认构造函数
        test1 t = new test1();

    }

}
运行结果:
    the constructor with parameter in parent class
    the default constructor in child class

参数是可以传递的,也就是可以通过子类构造函数传递给父类构造函数 。

class Parent {

    Parent ( String str ) {

        System.out.println(str);

    }

}

class test1 extends Parent {

    test1( String str ) {

        super( str );  // 调用父类中有参构造函数
     
        System.out.println("the default constructor in child class");

    }

    public static void main( String args[] ) {  // 主方法是静态方法,super 关键字无法在静态方法中使用

        // 实例化子类并调用子类默认构造函数
        test1 t = new test1( "the string in main method" );

    }

}
运行结果:
    the string in main method
    the default constructor in child class

那可不可以用 super() 同时调用无参构造函数和有参构造函数呢?答案是不能的。

class Parent {

    Parent() {

        System.out.println("the default constructor in parent class");

    }

    Parent ( String str ) {

        System.out.println(str);

    }

}

class test1 extends Parent {

    test1() {

        super();  // 调用父类中无参构造函数
        super("the constructor with parameter in parent class");  // 调用父类中有参构造函数
        System.out.println("the default constructor in child class");

    }

    public static void main( String args[] ) {  // 主方法是静态方法,super 关键字无法在静态方法中使用

        // 实例化子类并调用子类默认构造函数
        test1 t = new test1();

    }

}
运行结果:(报错)
    对 super 的调用必须是构造器中的第一个语句

由于 super() 必须是构造函数中的第一个语句,用两个 super() 就会报错。

1.4 总结

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值