zoukankan      html  css  js  c++  java
  • super与this用法

    super注意点:  

    1、当super调用父类的构造方法,必须在构造方法的第一个;

    2、super必须只能出现在子类的方法或者构造方法中;

    3、super和this不能同时调用构造方法;

    4、super可调用父类的属性和方法,不包括private私有属性和方法

    与this的区别:

    1、代表对象不同。this:本身调用者调用这个对象(谁调用就是谁);super:代表父类对象的应用。

    2、this没有继承也能使用;super只能在继承条件下使用

    3、this() :默认调用的是本类的构造; super()调用的是父类的构造。

    实例;

    public class Person{

      protected String name="BBQ";

      public  Person(){                           //无参构造方法

        Syste.out.println("person");

      }

     public  Person(String name){                 //有参构造方法

       this.name=name;

      }

    }

    public class Student extend Person{

      public Student(){         //在子类无参,默认调用父类的无参

             // 这里会省略一句:super();,会先执行父类的无参后再执行下面内容

        System.out.println("student");

      }

       public void print(){

        System.out.println(super.name);    //输出BBQ,调用父类的属性

      }

    }

    主类:

    public class Appliaction{

      public static voiid main(String[] args){

        Student student =new Student();

      }

    }

    运行结果:

     person

     student

    BBQ

  • 相关阅读:
    predis操作
    mysql 笔记(转载)
    mysql 汉字根据首字母排序
    sql 优化
    update多条不同数据
    解决network is unreachable问题
    开启服务器端口
    数据库开启远程访问
    激活2021.2.1idea
    python向excel追加数据
  • 原文地址:https://www.cnblogs.com/bbq668/p/12002740.html
Copyright © 2011-2022 走看看