super (Java关键字)

1.为什么要使用super关键字?

1)子类中要访问父类方法或变量。
2) 子类中调用父类的构造器
2.使用注意事项:

a. 只能在构造方法或实例方法内使用super关键字,在静态方法和静态代码块内不能使用super关键字。
b. 在子类构造方法中如没有使用this关键字,会隐式调用父类的无参构造方法;

          class Father() { 
            public Father() {
               //这里第一行隐含一句代码: super();
                       System.out.println("In Father()");
                }
          }

          class Son extends Father {
          }

          public class Test {
                 public static void main(String[] args) {
                        new Son(); //打印输出 In Father()
                 }
          }

          -----------------------------------------------------------------------

          class Father() { 
            public Father() {
                       System.out.println("In Father()");
                }
          }

          class Son extends Father {
                public Son() {
                       System.out.println("In Son()");  
                }   
          }

          public class Test {
                 public static void main(String[] args) {
                        new Son(); 
                        //打印输出 In Father()
                        //         In Son()
                 }
          }

          -----------------------------------------------------------------------

          class Father() { 
            public Father(String name) {
                       System.out.println("In Father() " + name);
                }
          }

          class Son extends Father {
                public Son() {
                       System.out.println("In Son()");  
                }   
          }

          public class Test {
                 public static void main(String[] args) {
                        new Son();  //编译出错, 子类会隐式调用父类中无参构造方法,而此时父类中不存在无参构造方法。
                 }
          }

          -----------------------------------------------------------------------

          class Father() { 
            public Father(String name) {
                       System.out.println("In Father() " + name);
                }
          }

          class Son extends Father {
                public Son() {
                       super("zs");
                       System.out.println("In Son()");  
                }   
          }

          public class Test {
                 public static void main(String[] args) {
                        new Son();  
                        //打印输出:
                        //In Father() zs
                        //In Son()
                 }
          }

          -----------------------------------------------------------------------

          class Father() { 
            public Father() {
                       System.out.println("In Father()");
                }

            public Father(String name) {
                       System.out.println("In Father(String name)");
                }
          }

          class Son extends Father {
                public Son() {
                       this("zs");
                       System.out.println("In Son()");  
                }

                public Son(String name) {
                       System.out.println("In Son(String name)");   
                }   
          }

          public class Test {
                 public static void main(String[] args) {
                        new Son();  
                        //打印输出:
                        //In Father()
                        //In Son(String name)
                        //In Son()
                 }
          }

c. 构造方法中this(…)和super(…)不能同时出现;

※ 注:this关键字与super关键字的区别,this表示当前的,super则表示父类的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值