编程知识 cdmana.com

Java super keyword

super The key words and this similar ,this Used to represent an instance of the current class ,super Used to represent a parent class .

super Can be used in subclasses , By the dot (.) To get the member variables and methods of the parent class .super It can also be used in subclasses of subclasses ,Java It can automatically trace back to the upper class .

Parent class behavior called , As if the behavior is the behavior of this kind , And the calling behavior doesn't have to happen in the parent class , It can automatically trace back to the upper class .

super Keyword function : Call the parent class declared as private The variable of . Click the method that has been covered . As a method name, denotes a parent class construction method . Call hidden variables and overridden methods public class Demo{

public static void main(String[] args) {
    Dog obj = new Dog();
    obj.move();
}

} class Animal{

private String desc = "Animals are human's good friends";
//  It has to be stated that  getter  Method 
public String getDesc() { return desc; }

public void move(){
    System.out.println("Animals can move");
}

} class Dog extends Animal{

public void move(){
    super.move();  //  Calls a method of the parent class 
    System.out.println("Dogs can walk and run");
    //  adopt  getter  Method calls the parent class to hide variables 
    System.out.println("Please remember: " + super.getDesc());
}

} Running results : Animals can move Dogs can walk and run Please remember: Animals are human's good friends

move() Methods can also be defined in some ancestor classes , For example, the parent of the parent class ,Java Traceability , Will always look up , Until the method is found .

adopt super Call the hidden variable of the parent class , Must be declared in the parent class getter Method , Because declared as private Data members of are invisible to subclasses . Call the constructor of the parent class in many instances , Use the default constructor to initialize the parent object . You can also use it super To display the constructor of the calling parent class . public class Demo{

public static void main(String[] args) {
    Dog obj = new Dog(" tearful ", 3);
    obj.say();
}

} class Animal{

String name;
public Animal(String name){
    this.name = name;
}

} class Dog extends Animal{

int age;
public Dog(String name, int age){
    super(name);
    this.age = age;
}

public void say(){
    System.out.println(" I am a lovely little dog , My name is " + name + ", I " + age + " Year old ");
}

} Running results : I am a lovely little dog , My name is Hua Hua , I 3 Year old

Be careful : Whether it's super() still this(), Must be placed on the first line of the constructor .

It is worth noting that : Another construction method is called in the construction method , The calling action must be placed in the most initial position . Constructor cannot be called inside any method other than constructor . Only one constructor can be called within a constructor .

If you write a constructor , Neither call super() No call this(), The compiler will automatically insert a call into the parent class constructor , And without parameters .

Last note super And this The difference between :super Not a reference to an object , Can't be super Assign to another object variable , It's just a special keyword that instructs the compiler to call the parent method .super The key words and this similar ,this Used to represent an instance of the current class ,super Used to represent a parent class .

super Can be used in subclasses , By the dot (.) To get the member variables and methods of the parent class .super It can also be used in subclasses of subclasses ,Java It can automatically trace back to the upper class .

Parent class behavior called , As if the behavior is the behavior of this kind , And the calling behavior doesn't have to happen in the parent class , It can automatically trace back to the upper class .

super Keyword function : Call the parent class declared as private The variable of . Click the method that has been covered . As a method name, denotes a parent class construction method . Call hidden variables and overridden methods public class Demo{

public static void main(String[] args) {
    Dog obj = new Dog();
    obj.move();
}

} class Animal{

private String desc = "Animals are human's good friends";
//  It has to be stated that  getter  Method 
public String getDesc() { return desc; }

public void move(){
    System.out.println("Animals can move");
}

} class Dog extends Animal{

public void move(){
    super.move();  //  Calls a method of the parent class 
    System.out.println("Dogs can walk and run");
    //  adopt  getter  Method calls the parent class to hide variables 
    System.out.println("Please remember: " + super.getDesc());
}

} Running results : Animals can move Dogs can walk and run Please remember: Animals are human's good friends

move() Methods can also be defined in some ancestor classes , For example, the parent of the parent class ,Java Traceability , Will always look up , Until the method is found .

adopt super Call the hidden variable of the parent class , Must be declared in the parent class getter Method , Because declared as private Data members of are invisible to subclasses . Call the constructor of the parent class in many instances , Use the default constructor to initialize the parent object . You can also use it super To display the constructor of the calling parent class . public class Demo{

public static void main(String[] args) {
    Dog obj = new Dog(" tearful ", 3);
    obj.say();
}

} class Animal{

String name;
public Animal(String name){
    this.name = name;
}

} class Dog extends Animal{

int age;
public Dog(String name, int age){
    super(name);
    this.age = age;
}

public void say(){
    System.out.println(" I am a lovely little dog , My name is " + name + ", I " + age + " Year old ");
}

} Running results : I am a lovely little dog , My name is Hua Hua , I 3 Year old

Be careful : Whether it's super() still this(), Must be placed on the first line of the constructor .

It is worth noting that : Another construction method is called in the construction method , The calling action must be placed in the most initial position . Constructor cannot be called inside any method other than constructor . Only one constructor can be called within a constructor .

If you write a constructor , Neither call super() No call this(), The compiler will automatically insert a call into the parent class constructor , And without parameters .

Last note super And this The difference between :super Not a reference to an object , Can't be super Assign to another object variable , It's just a special keyword that instructs the compiler to call the parent method .

This article altogether 1128 Number of words , Average reading time ≈ 3 minute

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[await]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/09/20210909125824183l.html

Scroll to Top