Chapter 5 of the JAVA cultivation script sleeping on firewood and tasting gall

Previous period:
JAVA cultivation script Chapter 1: Painful torture
JAVA cultivation script Chapter 2: Gradual demonization
JAVA cultivation script Chapter 3: Jedi counterattack
JAVA cultivation script Chapter 4: "Closed door cultivation"

1, Classes and objects

Starting from the important knowledge point, JAVA language is object-oriented, so what is object-oriented? The picture below is the first picture in my mind. Please enjoy it

Hahaha, isn't it amazing, but is object-oriented really so sweet? Let's correctly understand what object-oriented is.

Note: object orientation is just a way of thinking, which can simplify a complex problem. As long as it faces an object, class is the general name of a class, and object is the concrete example of this class.

1. Class instantiation

Understand the feeling through examples. For example, there is a large flower bed, which can grow many flowers. Then the flower bed is a class, and many flowers (objects) can be planted (generated) by the flower bed (class).
A class can be instantiated to produce many objects.
Next, let's look at how to generate a class in the code:

(1)class Person{                                 
(2)    public int age;                           
(3)    public String name;
(4)    public String sex;
(5)    public void study(){
	         System.out.println("study");
	     }
(6)    public void up(){
 	        System.out.println("Up");
	     }
	 }
 	public class Prog {
	     public static void main(String[] args) {
(7)        Person person=new Person();
(8)        person.study();
(9)        person.up();
(10)        System.out.println(person.age);
(11)        Person person2=new Person();
(12)        Person person3=new Person();
   	 	  }
   	 }

Note part (note according to the corresponding number);
1: The keyword for creating a class, followed by Person, is the class name
2: Member properties, instance variables.
3: Same as 2
4: Same as 2
5: Member method
6: Same as 5
7: Create an object of the Person class through which non static properties and methods in the class can be referenced or called. (non static and static are understood later).
8: Object name plus You can access the properties and methods in the class.
9: Same as 8.
10: Same as 8.
11: As we mentioned earlier, a class can create multiple objects. After creating Person, we can create many more later.
12: Same as 11.

matters needing attention:
1: Instantiating a member requires allocating memory for the object.
2: Call the appropriate constructor.

2. Properties

If the property is not initialized, what is the default value? When creating attributes, try not to assign initial values unless necessary.

The reference type defaults to  null. 
The simple type defaults to 0.
Boolean Default to  false. 
char Default to     '\u0000';

3. Method

There is also a method called "construction method", which will be automatically called when instantiating an object. The method name is the same as the class name, which is used for object initialization
Although we have been able to initialize properties locally, sometimes more complex initialization logic may be required, so we can use the construction method
We will introduce the syntax of construction method in detail later

2, static keyword

static keyword can modify attributes, methods, code blocks and classes.

1. Modification attributes

After you modify the attribute, the static attribute is related to the class rather than the instance object. Look at a piece of code. We can understand it through the picture:

class TestDemo{
    public int a;
    public static int count; }
public class Main{
    
 public static void main(String[] args) {
        TestDemo t1 = new TestDemo();
        t1.a++;
        TestDemo.count++;
        System.out.println(t1.a);
        System.out.println(TestDemo.count);
        System.out.println("============");
        TestDemo t2 = new TestDemo();
        t2.a++;
        TestDemo.count++;
        System.out.println(t2.a);
        System.out.println(TestDemo.count);
   }
}

Notes:
1: When the attribute count is modified by static, all classes can be shared and do not belong to an object.
2: The access method is also changed. After being modified by static, the access form is changed to:
Class name (TestDemo). Attribute (count)

2. Modification method

The modified method is called static method,
Static methods belong to classes, not objects that belong to classes.
Static methods can be called directly without creating an instance of the class.
Static methods can access static data members, can not access non static data members, and can change the value of static data members.

class TestDemo{
    public int a;
    public static int count;
    public static void change() {
        count = 100;
        //a = 10;  Non static data members cannot be accessed
   }
}
public class Prog{
 public static void main(String[] args) {
        TestDemo.change();//Can be called without creating an instance object
        System.out.println(TestDemo.count);   
   }
}

Notes:
1. Static methods cannot directly use non static data members or call non static methods (non static data members and methods are related to instances)
2.this and super keywords cannot be used in static code blocks (this is the reference of the current instance, super is the reference of the parent instance of the current instance, and is also related to the current instance)

3, Encapsulation

Before talking about encapsulation, first understand what the following three access modifier qualifiers are used for.

public->Public.
private->Private.
protected->Protected.
If you add nothing when creating, the default is——>Package access.

effect:
Encapsulation is to use private to modify attributes or methods. The modified attributes or methods can only be used in this class.

After modification, the security is greatly improved and the access methods are different. The code is as follows:

class Person { 
	private String name;
 	private int age; 
	public void setName(String name){ 
		this.name = name;
	} 
	public String getName(){ 
		return name; 
	} 
	public void show(){ 
		System.out.println("name: "+name+" age: "+age); 
	} 
} 
public static void main(String[] args) { 
	Person person = new Person(); 
	person.setName("caocao"); 
	String name = person.getName(); 
	System.out.println(name); 
 	person.show(); 
}

Notes:

1. After modification, we find that the instance object created by the class cannot be accessed directly, but through the methods in the class,
2. It can be seen that the security level of the code will not lead to a lot of trouble in subsequent codes with the modification of attribute member names in the class.
3.get is to get, set is to modify, and Alt+insert (f12) can quickly create these two methods.

4, Construction method

effect:
Construction method is a special method. When a new object is instantiated with the keyword new, it will be automatically called to complete the initialization operation.
After we create an instance object through new, if you do not create a constructor, the compiler will automatically create a default parameterless constructor for initialization. If you create it yourself, the compiler will not generate it automatically.
The construction method supports overloading, and the method is the same as that in previous content.
Here's the code:

class Person {
    private String name;
    public Person1() {
        this.name = "zhang";
    }
    public Person2(String name) {
        this.name = name;
    }
}
public class Prog{
    public static void main(String[] args) {
        Person person = new Person1();
        Person person2 = new Person("zhang");
    }
}

Notes:
Person1 is the construction method generated by the compiler by default. If the instance is given, it will not be displayed normally.
Constructors created by Person2 for itself
matters needing attention:
Constructor name must be the same as class name.
Constructor has no return value type.
Each class will have a construction type. If you don't create it, the compiler will create it by default.

5, Code block

What is a code block? I don't know how to express it for you. I'd better draw it.

Each color corresponds to a code block.
Code blocks are divided into the following four types:
1: Native code blocks - > not commonly used.
2: Instance code block - > is defined outside the method and inside the class.
3: Static code block - > is defined outside the method and inside the class.
4: Sync code block - > neither can I, hee hee.
Let's know 2 and 3 first, and then 1 is not often used.

1. Example code block

Construction code blocks are generally used to initialize instance member variables, and only instance members can be initialized.
The code is as follows:

class Person{
    public String name; 

    {
		this.name = "zhang"; 
	}
}
public class Prog {
    public static void main(String[] args) {
    	System.out.println(person.name);
    }
}

2. Static code block

Code blocks defined using static. Generally used to initialize static member properties. And only static members can be initialized.
The code is as follows:

class Person{
    public static String name; 

    {
		this.name = "zhang"; 
	}
}
public class Prog {
    public static void main(String[] args) {
    	System.out.println(Person.name);
    }
}

matters needing attention:
1. How many static code blocks are generated, only one will be executed.
2. Static code blocks are executed first when the program runs.

The running sequence is:
Static code block - > instance code block - > constructor.

At the end of this chapter, thank you for your sightseeing. See you in the next chapter.

Keywords: Java Class OOP

Added by peerData on Mon, 03 Jan 2022 01:16:31 +0200