Beginners Section 23 this, overloading

announcement notice

This official account is purely a personal public welfare official account! I just want to help all java beginners. The official account has a series of free resources for the majority of java beginners to learn by themselves! There is also a WeChat exchange group for everyone to learn and discuss! ! ! You can pay more attention! ! ! You can also leave a lot of comments on our articles and leave your valuable comments! ! !


one

this keyword

In a normal method, this always points to the object on which the method is called.

In a constructor, this always points to the object being initialized.

 

To sum up, in fact, this is mainly used in three ways:

1. Represents a reference to the current object!

2. It means that the member variables of the class are used instead of function parameters. Note that the function parameters and member variables with the same name are distinguished! In fact, this is a special case of the first usage, which is more commonly used, so I will emphasize it.

3. It is used to refer to the constructor (in fact, the constructor) that satisfies the specified parameter type in the constructor. But great care must be taken here: only one constructor can be referenced and it must be at the beginning! There is also a note: this cannot be used in static methods! So even someone's definition of static method is: a method without this! Although exaggerated, it fully shows that this cannot be used in static methods!


two

Overload

Overloading (overloading) is inside a class, the method name is the same, but the parameters are different. The return types can be the same or different. Each overloaded method (or constructor) must have a unique parameter type list. The most common place is the overloading of constructors.

Overloading rules

The overloaded method must change the parameter list (the number or type or order of parameters is different);

Overloaded methods can change the return type;

Overridden methods can change access modifiers;

Overridden methods can declare new or broader checked exceptions;

Methods can be overloaded in the same class or in a subclass.

The return value type cannot be used as a distinguishing criterion for overloaded functions.

 

Since the overloading of the constructor has been demonstrated in the previous section, only the overloading of the method will be demonstrated here.

As shown

[Exercise] Overloading of constructors:

Define a base class named Vehicles (vehicles), which should contain a member property color (color) of type String, and a member method run (driving, showing "the car has started" in the console), and write 2 constructors

No-argument construction: call the run method

Construct with parameters: call the construction without parameters, and have a color parameter of type string, and assign the parameters to member variables, the last code of the construction without parameters is System.out.println("The car is " + this.color + " colour");

Output in the following format (please use the this keyword to call class members). 

The answer is in the official account "Constructor overloading"


   Scan the code to follow ∣ there are troublemakers to detour


Long press, identify the QR code, and follow

Note: This official account is purely a personal public welfare official account!

There is no recruitment information for any training institutions! !


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324537308&siteId=291194637