"Crazy JAVA Lectures" 3

table of Contents

The use of this object

1. The this keyword always points to the object calling the method.

Practice one: use this

Method 2: Do not use this

2. Special:

Detailed method

Member variables and local variables

Hide and encapsulate


The use of this object

1. The  this keyword always points to the object calling the method.

Depending on where this appears, this is the default reference of an object in two situations:

(1) the constructor of the reference object constructor is being initialized;

(2) the method the object of the method call.

In fact, the biggest role of this is to allow a method in a class to access another method or instance variable in the same class.

Give a chestnut: define a Dog class with two methods, run and jump, and want to call the run method in jump.

Practice one: use this

image

The results are as follows:

image

Method 2: Do not use this

 

image

The result is the same.

Comparison of the two: when not using this, you need to redefine an object in the jump method, otherwise you cannot reference the run method. In comparison, using this is more concise.

    If you debug the program yourself, you can get the same result without this, but this is an illusion. In fact, this still exists.

2. Special:

    For static-modified methods, you cannot use this. As I said before, static declares class methods and class variables. You cannot call methods or variables that are not modified with static. You must create an instance. If you can use this, it means that all methods and variables in the class can be called through this, which is obviously contradictory. Therefore, Java stipulates that static members cannot access non-static members.

Here is a chestnut to tell you the error that occurs when a static method refers to a non-static method:

image

Look at the reason:

image

This is obvious.

Personally, I think this can be understood here.

Detailed method

1.  Methods in java cannot exist alone, all methods must be defined in the class. Methods logically belong to either a class or an object. Therefore, methods cannot be executed independently like functions, and classes or methods must be used as callers. Methods in the same class call each other to give the illusion that methods can be executed independently. As mentioned above, the calls are made through this.

2. Method parameter transfer mechanism: value transfer

    The so-called value transfer is to pass a copy (duplicate) of the parameter to the method, and the parameter itself will not be affected.

The following illustrates the value transfer by exchanging two numbers:

image

The result is:

image

As you can see, there is no effect we expected. Because when the system starts to execute the method, it assigns the actual parameter variable to the formal parameter variable instead of directly operating the actual parameter, so of course it will not change. This is value passing. Here we need to pass by reference type parameters.

The correct approach is as follows:

image

The results are as follows:

image

3. Method overloading

    Define multiple methods with the same name in the same class, but with different parameter lists.

    There are three elements to determine a method in a java program:

    (1) Caller

    (2) Method name

    (3) Form attendance table

    Method overloading only needs to have different formal parameter lists and the same method name. As for other parts, such as method modifiers, return value types, etc., they have nothing to do with method overloading.

Give a chestnut:

image

The results are as follows:

image

Member variables and local variables

image

   (1) It can be seen that member variables are variables defined in classes, and local variables are variables defined in methods. The class variable is until the class is destroyed, otherwise it will always exist, but the local variable disappears with the disappearance of the method.

    (2) Class variables can be accessed and modified through classes or instances. Therefore, when using class variables in an instance, pay attention, once modified, other instances use the modified value, so be cautious.

    (3) Different from member variables, local variables must display initialization except for formal parameters. In other words, if no initial values ​​are assigned, they cannot be accessed.

Give a chestnut:

image

The error is as follows:

image

Hide and encapsulate

It is mainly controlled by four access control characters:

(1) private (current class access authority)

(2) default (package access authority)

(3) protected (subclass access rights)

(4) public (public access authority)

Introduce private:

    Most of the member variables in the class should be decorated with private, and only some member variables with static modification, similar to global variables, should be considered with public modification. This is to make the module highly cohesive (as far as possible to complete the internal data of the module independently within the module, no direct external intervention is allowed), low coupling (only expose a small number of methods for external use).

Introduce default:

    If a member of the class or an external class is not decorated with any access control characters, it is the package access authority. Members or external classes under default access control can be accessed by other classes in the same package.

Introduce protected:

    Under normal circumstances, use protected to modify a method, usually hope that other subclasses override this method.

note:

    If all classes defined in a java file are not decorated with public, the file name of the java source file can be any legal name, but if a public modified class is defined in a java file, the file name of the source file Must be the same as the class name of the public modified class.

/********************************/

Finally, I will talk about an easily overlooked issue:

The import statement is placed after the package statement and before the class definition.

 

END

image

    What I am happier today is to use eclipse, I must learn to use it proficiently, the flag can not be down! I have gained a lot in the process of earnestly re-passing the knowledge points. I hope to make progress slowly. Don't be impatient and give yourself some confidence! I got a code problem these two days and made my head big. I dreamed of it last night. Alas, I decided to put it aside for a while, change my mind and start again.

    In the morning, I explained it like this. In the afternoon, I plan to get Android after class, competition or something, I still have to study hard, the gap is too big. The experiment yesterday morning broke me very much. I feel so useless. My hands-on skills are really bad. Come on, there is no other way!

    Be careful on rainy days in Beijing~

Guess you like

Origin blog.csdn.net/allein_STR/article/details/113985132