Zero basic learning java-classes and objects

package

     What is encapsulation?

      private implementation encapsulation

        getter and setter methods

 Construction method

        basic grammar

         how to construct

this keyword

Recognize code blocks

       normal code block

       Building code blocks

       static code block

Supplementary Instructions 

         toString method

 anonymous object

 Summary of content highlights


 package

     What is encapsulation? In layman's terms, it is to encapsulate an object and prevent others from using it. What are the encapsulations in our java? Generally, I use private to implement encapsulation in java.

private implementation encapsulation

 The two keywords private/ public mean "Access Control"

A member variable or member method modified by public can be used directly by the caller of the class

A member variable or member method modified by private cannot be used by the caller of the class

use public directly

Results of the

Let's take a look at this kind of code. If we want to (name changed to myName), we need to modify our code on a large scale, and the maintenance cost is high and the energy is spent. In order to reduce this large-scale modification of our own code, we should How to do it?

use private

 

When we don't want the value in setName to be "bit", we only need to modify it in setName, without going to each modification. 

 Notice

1. Using private encapsulation, the properties are encapsulated! Can only be used within a class (current class)! Provide public get and set methods at this point!
2. The benefit of encapsulation is to make properties more secure.

getter and setter methods

When we use private encapsulation, the properties are encapsulated and can only be used in the class. Provide public get and set methods at this point!

 

 How to quickly use getter and setter methods!

 Construction method

Constructor: 1. The method name and the class name are the same, and the construction method is special and has no return value.

What is the constructor for? --"Generation of an object (instantiation of an object)

1. Allocate memory for the object 

2. Invoke the appropriate constructor. (Appropriate: means more than one constructor!)

 

 

*Notice
* 1. If no constructor is implemented, the compiler will generate a constructor without parameters for us by default
* That is to say, a class will have at least one constructor, even if you don't write it!
*2. If the current class has other constructors, then the compiler will not generate a constructor without parameters for us!
*3. Overloading can be formed between constructors!

this keyword

    this represents the current object reference (note that it is not the current object ). You can use this to access the fields and methods of the object  

*this3 uses*

1.this.data calls the properties of the current object 

2.this.func() calls the method of the current object 

3.this() calls other constructors of the current object* Note that it can only be stored in the constructor! ! ! !

 

 

Recognize code blocks

According to the position and keywords defined by the code block, it can be divided into the following four types:

normal code block

building block

static block

Synchronized code block

For example; instance code is code defined using {}, while static code is defined using the keyword static{}

 

 How is it called?

new an object in the main function main

 

 print result

We found that the order of printing is that the static code comes first, the instance code comes after, and finally our construction method

This shows that the code block execution is sequential, static code - "instance code -" - "construction method.

Supplementary Instructions

      toString method

      it can convert our object to character

     

 

 

Let's look at the print result. After adding the toString method, the result we print is automatically converted to a character for us.

How to use toString method quickly?

This is the same as we use the set and get methods in the private package.

 

  anonymous object

What is an anonymous object?

An object with no name and can only be used once!

 

 

Summary of content highlights

1. A class can generate countless objects, the class is the template, and the object is the concrete instance.

2. The attributes defined in the class are roughly divided into several categories: class attributes and object attributes. Among them, the data attributes modified by static are called class attributes, and the methods modified by static are called class methods. The characteristic is that it does not depend on the object, and we only need to use the class name to call its attributes or methods.

3. Static code blocks execute first instance code blocks, and instance code blocks execute constructors first.

4. This keyword represents a reference to the current object. is not the current object.

Xiaosheng is not talented, and can only write such rough articles. If you have any suggestions, please listen! ! !

Guess you like

Origin blog.csdn.net/Biteht/article/details/121518077