Basic knowledge of JAVA (7)

5.11 Code block

The so-called code block refers to a piece of code enclosed in "{}". Depending on the location, the code block can be divided into four types: ordinary code block, construction block, static code block, and synchronous code block.

5.11.1 Ordinary code block

The ordinary code block refers to the code block defined directly in the method or statement.

5.11.2 Building blocks

The construction code block is the code block written directly in the class.
The construction block takes precedence over the construction method, and the code in the construction block will be executed every time an object is instantiated, and it will be executed multiple times.

5.11.3 Static code block

A static code block is a code block declared using the static keyword.
The static code block is executed before the main method, and the static code block defined in the class is executed before the building block, and no matter how many objects are generated, the static code block is executed only once.

5.12 Privatization of construction methods

In the design pattern, this design is called the singleton design pattern, that is, no matter how the program runs, there will always be only one instantiated object of the class.

5.13 Object array

The so-called object array refers to an array containing a group of related objects. When using an object array, the array must first open up memory space, because it is a reference data type, so each object in the array is a null value, and each object in the array must be instantiated separately when in use.

Declaration of format object array

Class object array name [] = new class [array length];

String args[] in the main method is an array of objects.
In the main method, you can use String args[] to receive initialization parameters. In fact, the String itself is a class, so the parameters in the main method appear in the form of an array of objects.

5.14 Internal category

5.14.1 Basic definition of inner class

Member variables and methods can be defined inside a class, and another class can be defined inside a class. If a class Inner is defined inside the class Outer, then the class Inner is called the inner class, and the class Outer is called the outer class.
Inner classes can be declared as public or private. When the inner class is declared as public or private, the restrictions on its access are exactly the same as for member variables and member methods.

Declaration format of inner class

标识符 class 外部类的名称{
    
    
   //外部类的成员
   标识符 class 内部类的名称{
    
    
        //内部类的成员 
   }
}

The only advantage of the inner class is that you can easily access the private properties in the outer class. At the same time, the external class can also easily access the private attributes in the internal class, so that it can avoid the complicated operation structure caused by encapsulation.

5.14.2 Use static to define internal classes

Use static to declare properties or methods, and use static to declare internal classes. Internal classes declared with static become external classes, but internal classes declared with static cannot access non-static external classes.

5.14.3 Accessing internal classes externally

In addition to being accessed through an external class, an internal class can also be called directly in other classes. The basic format of the call is:

Outer class. Inner class Inner class object = Outer class instance. New inner class();

In the above operation format, the instantiation object of the external class must be found first, and then the object of the internal class can be instantiated through the instantiation object of the external class.

Observe the class file of the inner class.
After the internal class is defined, the generated class file is based on "

Outer$Inner.class

If it exists in the form of ", as long as you see "$" in the file in Java, use it in the program to replace it with ".".

5.14.4 Define the inner class in the method

In theory, internal classes can be defined anywhere in the program, such as: code blocks, methods, but internal classes defined in methods cannot directly access the parameters in the method. If the parameters in the method want to be accessed by the internal class, then The final keyword must be added before the parameter.

5.15.2 Implementation of a singly linked list (1)

Linked list is a form often seen in data structure. The so-called linked list is like a train car. Starting from the locomotive, each car is connected to one car. Each carriage is equivalent to a node. In addition to storing its own content, each node also stores a reference to the next node.

Node class

class Node{
    
    
    private String data;//保存节点内容
    private Node next;//保存下一个节点
    public Node(String data){
    
    //构造方法设置节点内容
	this.data=data;
    }
    public String getData(){
    
    //得到节点内容
	return this.data;
    }
    public void setNext(Node next){
    
    //设置下一节点
	this.next=next;
    }
    public Node getNext(){
    
    //取得下一节点
	return this.next;
    }
};

Determine whether there are subsequent nodes after a node. If there are subsequent nodes, then output; if there are no subsequent nodes, no output. The output can be completed using the recursive call of the method (recursive call is to call yourself).

Guess you like

Origin blog.csdn.net/qq_43480434/article/details/112852850