Java object oriented, constructor, static and block

Posted by Tatara on Sat, 22 Jan 2022 18:43:24 +0100

Java object orientation (2), constructors, static and blocks


First of all, let's understand what object-oriented is. Object-oriented is actually a kind of thinking to deal with problems,

It's like going to pick up a cup of tea;


There are two kinds of thinking, one is object-oriented, the other is process oriented;

Process oriented: what is the process between receiving a cup of tea and how to do it? Should we first pick up the water cup, soak tea, then go to the water dispenser, and then receive a cup of hot tea; It is aimed at one thing and focuses on the implementation process.

Object oriented: here, we don't care what steps there are to take a cup of tea, but just take a cup of tea. Regardless of the process, we can ask Xiao Wang to help take a cup of tea. We just need to face Xiao Wang to specify it, focusing on the result.

In object-oriented, the basic unit of the whole program is class, and the method is subordinate to class; The relationship between class and object is from special to general, from concrete to abstract


Class: we call it class

Object: we call it object, instance

Relationship: the object of a class and the instance of a class

Go on to the previous chapter

1. Basic composition of a class

(1) Properties:

Member variable

(2) method

----------------------- previous chapter

Key points: (3) Constructor (construction method): Constructor is actually a special method

Definition: initializes object information, not used to create objects

Use: use with new

Structure: modifier class name (parameter list){

Method body;

​ }

Features: 1 Constructor is also a special method. Multiple overloaded constructors are defined. The names of constructors are consistent with the class names. Overloaded constructors can be formed by different parameter lists

​ 2. If there is no definition constructor shown, the compiler will automatically provide an empty constructor for the class

​ 3. If there is a constructor for the display definition, the compiler does not provide any constructor

	No return value type is required,either void
    It can be defined as needed return keyword,Action early termination method
    The constructor name is consistent with the class name
 be careful:
        1.Constructors can implement overloading,Because it's a special method
        2.No constructor is displayed,The compiler provides an empty construct by default
        3.If there is a display definition constructor,No constructor will be provided,Include empty constructs
        4.Provide at least one empty construct...Provide with parameters on demand,Not absolute,According to demand
public class Class001_Constructor {
    public static void main(String[] args) {
        //First member object
        Dog dog = new Dog();
        //new Dog();
        //Assign values to the members of the object
        //dog.name = "";
        //dog.type = "";

        dog.lookDoor();

        //Assignment while creating objects
        new Dog("Steamed buns").lookDoor();

        new Dog("Fried Glutinous Rice Balls with Sesame","Chinese garden dog","speckle").lookDoor();
    }
}


class Dog{
    public String name;
    public String type;
    public String color;

    //Null constructor: constructor without parameters
    public Dog(){
        System.out.println("Empty structure");
    }

    public Dog(String dogName){
        System.out.println("One parameter construction");
        //Assign the data received by the argument to the member variable name of the current object
        name = dogName;
    }

    public Dog(String dogName,String dogType,String dogColor){
        System.out.println("Three parameter construction");
        //Assign the data received by the argument to the member variable name of the current object
        name = dogName;
        type = dogType;
        color = dogColor;
    }

    public void lookDoor(){
        System.out.println(name+"I'm looking after the house....");
    }
}

(4) code block

Local code block: {} definition is executed in the method following the call of the method

1) scope

2) semantic

Building block: {} definition is used with new outside the method in the class

1) the initial information of the member content is executed earlier than the code in the constructor

2) initialize some contents of the object

Static block: static modifier {}, which defines the method in the class and executes after the external class is loaded for the first time

1) initialize the static content

2) configuration of information and loading of resources

Synchronization block (not much)

public class Class001_Block {
    static int i ;
    int j = 1;

    public Class001_Block(){
        System.out.println("constructor ");
    }

    //Tectonic block
    {
        System.out.println("Building block 1");
        System.out.println(i);
        System.out.println(this.j);  //Current new object
    }
    {
        System.out.println("Building block 2");
    }
    {
        System.out.println("Building block 3");
    }

    //Static block
    static{
        System.out.println("Static block 1");
        System.out.println(i);
        //Non static member variables j cannot be used directly in static content and need to follow the object
        //System.out.println(j);
    }
    static{
        System.out.println("Static block 2");
        i = 1000;
    }
    static{
        System.out.println("Static block 3");
    }


    public static void main(String[] args) {
        System.out.println("main");
        //local
        //int i =1;

        //Local code block
        {
            int i = 10;
            System.out.println(i);
        }
        System.out.println(i);

        new Class001_Block();
    }
}

(5) internal class (don't go deep first)

2.this keyword (key)

Refers to the current object (new object) – > stores the addresses of all objects pointed to

The first line of the constructor calls other constructors in this class
This (parameter list)

    Distinguishing between members with the same name and local problems
        Default proximity principle
        If there is a problem with the same name,You want to refer to the calling member,adopt this.member-->because this Reference object,Calling members through objects
        If there is no problem with the same name,Can be omitted this.Call member
        Use in constructor this,Default refers to the current new Object of,Stored is this The address of the object
        Used in member methods of this,this In the member method, the default refers to the object of the current calling member method.

    be careful:
        1.Multiple constructors cannot call each other,Otherwise, the call cannot be stopped
        2.stay static Cannot be used in decorated content this
public class Class001_This {
    public static void main(String[] args) {
        Dog dog = new Dog("Speckle ratio","Samoye","white");
        dog.show();

        Dog dog2 = new Dog("Xiaobai");
        dog2.show();
        System.out.println("dog2 Address of"+dog2);
    }
}

class Dog{
    public String name;
    public String type;
    public String color;

    //constructor 
    public Dog(){
        System.out.println("Empty structure");
    }

    public Dog(String name){
        System.out.println("One parameter construction");
        this.name = name;
        System.out.println("this address" + this);
    }

    public Dog(String name,String type){
        System.out.println("Two parameter construction");
        this.name = name;
        this.type = type;
    }

    public Dog(String dogName,String dogType,String dogColor){
        this(dogName,dogType);
        color = dogColor;
        System.out.println("Three parameter construction");
    }

    public void lookDoor(){
        System.out.println(name+"I'm looking after the house....");
    }
    public void show(){
        String name = "Local name value";
        //this refers in the member method to the object of the current calling member method by default.
        System.out.println(this.name+"-->"+type+"-->"+color);
    }
}

3.static keyword (secondary focus)

​ 1. In static content, you can directly use static content, and you need to follow the object to use members
​ 2. In member content, you can directly use static content or member content

this cannot be used in a static environment. A static method can directly follow a class call, and there can be no object at all. this of a static method cannot refer to an object

public class Class002_Static {
    //Member location: outside method in class
    //Instance variable
    int i = 1;
    //Static variable
    static int j = 2;

    public static void main(String[] args) {
        System.out.println(j);
        Class002_Static cs  = new Class002_Static();
        System.out.println(cs.i);

        cs.test();
        testStatic();
    }

    //In the member method, this by default refers to the object that calls the member method
    public void test(){
        System.out.println("Member method");
        System.out.println(j);
        System.out.println(this.i);
    }

    public static void testStatic(){
        System.out.println("Static method");
    }
}

Classic mixed examples

package com.yjx.block03;
//Find print order + result
public class Class002_BlockTest {
	public static int a = 0;

	static {
		a = 6;
		System.out.println(" ,Static code block execution a=" + a); //
	}
	public Class002_BlockTest() {
		this(a); //
		System.out.println(" ,"+a);  //
		System.out.println(" ,Implementation of parameterless construction method a=" + a); //
	}
	public Class002_BlockTest(int n) { //n=
		{
			a = 10;
			System.out.println(" ,Non static code block execution a=" + a); //
		}
		System.out.println(" ,"+n); //
		System.out.println(" ,"+a);//
	}	
	public static void main(String[] args) {
		System.out.println(" ,main"); //
		Class002_BlockTest tsc = new Class002_BlockTest();
	}
}

4.debug code debugging tool (key)

Code debugging tools, not much here, you can search Baidu

Topics: Java Back-end Class OOP