This () and super() in Java

1. This keyword

package com.cj.study.thissuper;
 
public class Student{
	
	public String name;
 
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName(a) {
		returnname; }}Copy the code

The above code is a very common Javabean definition, often containing the this keyword

Have you thought about why this keyword is used here?

To explain the problem more simply, change the above code

Name =name =name =name =name =name =name =name =name =name =name =name =name =name Because the name is the same cause can not distinguish out

So you have to use this to distinguish; Of course, if the two names are not the same, name= NNN without this is ok, because it looks for a local variable first, if it finds a local variable, it looks for a global variable if it doesn’t find a global variable.

So what is this?

This is a reference to the current object, which means that this represents whoever the constructor is currently building. It is a reference

Another question, what if a constructor wants to call another constructor?

This is where this() comes in

Note: If another overloaded constructor is called from within a constructor, the called constructor must be on the first line of the code

Here’s an example:

The correct

2, super keyword

ShowNum =4 showNum=4 showNum=4

If the subclass defines num as 5, the printable value will be 5, because this is omitted by default when calling members (variables and methods)

But what if I want to use superclass instead of subclass

Again, you can’t tell the difference, so use super to indicate that you want to call the parent class, not the child class

If only the parent class has it, but the child class does not, the parent class calls it. (Because the child class inherits from the parent class, the child class owns everything but the parent’s private property.)

When both parent and subclass are present, the default call is for the subclass, but super indicates that future calls are for the parent class

As shown above, references to this and super refer to the object’s member variables and methods like this:

This refers not only to the member variables and methods of a subclass, but also to variables and methods that the subclass inherits from its parent class

Super simply refers to the member variables and methods that a subclass inherits from its parent.

What if the subclass constructor wants to call the parent class constructor?

That’s where super() comes in; the

***** Note: all subclass constructors must call the superclass constructor. The first line of each subclass has an implicit super();

The constructor of a subclass will report an error if the parent class does not define a constructor with no arguments. By default, the constructor of a subclass has an implicit super().

Your constructors have an implicit super() by default; If your constructor doesn’t want to call the default parent constructor, but instead wants to call another parent constructor, you can say that explicitly, and if you do, the supper(XXX) of the parent must be on the first line

So that’s a summary of this and super, maybe a little messy, but hopefully helpful

Tiezi, if you find the article helpful, you can click follow, click like

You can also pay attention to the public number: WX search: “chat 50 cents of Java”, welcome to learn to exchange, pay attention to the public number can receive the blogger’s Java learning video + material, ensure that are dry goods