super with this usage

super Precautions:  

1, when the super call the constructor of the superclass must be configured in a first method;

2, super must only occur in the constructor method or subclass;

3, super, and at the same time this method can not call the constructor;

4, super callable properties and methods of the parent class, private private attributes and methods do not include

 

With this difference:

1, representing different objects. this: the caller to call itself the object (who is who calls); super: the application on behalf of the parent object.

2, this is not inherited can be used; super can only be used under conditions of inheritance

3, this (): this configuration is called by default class; Super () call is the configuration of the parent class.

 

Examples;

public class Person{

  protected String name="BBQ";

  public Person () {// constructor with no arguments

    Syste.out.println("person");

  }

 public Person (String name) {// constructor parameters have

   this.name=name;

  }

}

 

public class Student extend Person{

  public Student () {// no reference in the subclass, the default parameters without calling the parent class

         // Here omits one: super () ;, will first run a parent, no arguments and then perform the following content

    System.out.println("student");

  }

   public void print(){

    System.out.println (super.name); // output BBQ, call the parent class attributes

  }

}

 

Main categories:

public class Appliaction{

  public static voiid main(String[] args){

    Student student =new Student();

  }

}

 

operation result:

 person

 student

BBQ

Guess you like

Origin www.cnblogs.com/bbq668/p/12002740.html