Getting to Know Java (Java Classes and Objects - Objects)

1. Object

    Java is an object-oriented programming language. Objects are abstracted from classes, and all problems are handled by objects. Objects can manipulate the properties and methods of classes to solve corresponding problems, so understand the generation, operation and death of objects. necessary.

    1.1 Object Creation

    An object can be considered as a special case that appears in a class of things, and the problems that arise in this type of thing can be handled through this special case. Objects are created in the Java language through the new operator. Each time an object is instantiated, the constructor will be called automatically. In essence, this process is the process of creating an object. In the Java language, the new operator is used to call the constructor to create an object.

    grammar:

Test test = new Test();
Test test = new Test("a");
Parameter Description in Create Object Syntax
Settings describe
Test class name
test Create Test class object
new create object operator
" a " Constructor parameters

    When the test object is created, it is a reference to an object, which allocates storage space for the object in memory, initializes member variables in the constructor, and automatically calls the constructor when the object is created, that is, in the Java language. Bundled with initialization and creation.

    Each object is independent of each other, occupies an independent memory address in memory, and each object has its own life cycle. When the life cycle of an object ends, the object becomes garbage, which is brought by the Java virtual machine. processed by the garbage collection mechanism and can no longer be used.

    Objects and instances in the Java language can actually be used interchangeably.

    eg : create class, create object in class and create object in main method.

public class CreateObject{
    public CreateObject(){ //Construction method
        System.out.println("Create object");
    }
    public static void main(String[] args){ //Main method
        //Use the new operator to create an object in the main method. When the object is created, the code in the constructor will be automatically called
        new CreateObject(); //Create object
    }
}

    The result of the operation is: Create the object

    1.2 Accessing object properties and behavior

    After the user has created an object with the new operator, he can use "object.classmember" to get the properties and behavior of the object. The properties and behaviors of objects are represented in the class by the form of class member variables and member methods, so when an object acquires a class member, it also acquires the properties and behaviors of the object accordingly.

    eg : Create a class and describe how the class members are called when the object is described in the class.

public class TransferProperty{
    int i = 47 ; //define member variable
    public void call(){ //Define member method
        System.out.println("Call the call() method");
        for(i = 0 ; i < 3 ; i++){
            System.out.print(i + "");
            if(i == 2){
                System.out.println(" \n");
            }
        }
    }
    public TransferPropety(){ //Define the constructor
    }
    public static void main(String[] args){ //Main method
        TransferProperty t1 = new TransferProperty(); //Create an object
        TransferProperty t2 = new TransferProperty(); //Create another object
        t2.i = 60; //Assign the class member variable to 60
        // use the first object to call the class member variable
        System.out.println("The result of the first instance object calling the variable i: "+t1.i++);
        t1.call();//Use the first object to call the class member method
        // use the second object to call the class member variable
        System.out.println("The result of the second instance object calling variable i: "+t2.i);
        t2.call(); //Use the second object to call the class member method
    }
}
    The running result is:

Result of calling variable i for the first instance object: 47
call the call() method
0 1 2
The result of the second instance object calling the variable i: 60
call the call() method
0 1 2

    In the main method of the above code, first instantiate an object, and then use the " . " operator to call the member variables and member methods of the class. But it is different in the result, because the value of this member variable is reassigned to 60 before printing the value of this member variable, but the second object t2 is used to call the member variable when assigning, so the first object t1 calls the member variable to print This value is also the initial value of the variable.

    If you want member variables not to be changed by any of the objects, you can use the static keyword.

    eg : Create a class that exemplifies objects calling static member variables.

public class AccessProperty{
    static int i = 47 ; //define member variable
    public void call(){ //Define member method
        System.out.println("Call the call() method");
        for(i = 0 ; i < 3 ; i++){
            System.out.print(i + "");
            if(i == 2){
                System.out.println(" \n");
            }
        }
    }
    public AccessProperty(){ //Define the constructor
    }
    public static void main(String[] args){ //Main method
        AccessProperty t1 = new AccessProperty(); //Create an object
        AccessProperty t2 = new AccessProperty(); //Create another object
        t2.i = 60; //Assign the class member variable to 60
        // use the first object to call the class member variable
        System.out.println("The result of the first instance object calling the variable i: "+t1.i++);
        ta.call(); //Use the first object to call the class member method
        // use the second object to call the class member variable
        System.out.println("The result of the second instance object calling variable i: "+t2.i);
        t2.call(); //Use the second object to call the class member method
    }
}

    The running result is:

Result of calling variable i for the first instance object: 60
call the call() method
0 1 2
Result of the second instance object calling variable i: 3
call the call() method
0 1 2

    As can be seen from the above running results, since the t2.i = 60 statement has changed the value of the static member forced you, the value of the member variable called using the object t1 is also 60, which is exactly the value of i is defined as a static member variable Even if two objects are used to operate on the same static member variable, the value of the static member variable can still be changed, because the two implementations in memory point to the same memory area at the same time. After the t1.i++ statement is executed, the value of i becomes 3. When the call() method is called again, it is reassigned to 0, and the circular printing operation is performed.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325631799&siteId=291194637