[Turn] java object - understanding of new object

I have been learning java for a long time and have not been clear about the objects in java. Today, the landlord has sorted out the objects in java, hoping to help everyone.

To understand and use objects in java, we first understand the construction method and the creation of objects. Classes are the most important kind of data in object-oriented languages, and you can use classes to declare variables. In object-oriented languages, variables declared with classes are called objects.

Different from basic data types, after declaring an object, you must also create an object, that is, assign the variables it owns to the declared object (determine the attributes the object has). When you use a class to create an object, it is also called given. one of this class

instance. In layman's terms, a class is a template for creating objects.


1. Constructor

Constructor is a special method in a class. When creating an object with a class, you need to use its constructor. The constructor in a class must have the same name as the class in which it is located, and has no type.
 
class Person   
{  
    int age;  
    String name;  
  
    Person(){  
          
    }  
}

 

Person() is a constructor in the Person class. In fact, this method is hidden by java, that is to say, if there is no constructor written in the class, the system will default that the class has a parameterless structure, and there is no statement in the method body. function. Such as

Person(){ } in the Person class, if one or more constructors are defined in the class, then java does not provide a default constructor. as follows:

class Person   
{  
    int age;  
    String name;  
  
    Person(){  
        age=22;  
        name = "Andy Lau" ;  
    }  
  
    Person(int age,Stirng name){  
        System.out.println("the man age is"+age+"name: "+name);  
    }  
}

2 Create objects

(1) Object declaration
The general format is:
 
the name of the class the name of the object;
Such as:
Person person;
 
(2) Assign variables to declared objects
1. Use the new operator and the default constructor of the class to assign a variable to the declared object, i.e. create the object.
class Person   
{  
    int age;  
    String name;  
    void speak(String name){  
        System.out.println(name);  
    }  
}  
public  class OuXiang  
{  
    public static void main(String[] args) {  
        Person liudehua;             // declare object   
        liudehua= new Person();   // assign variables to variables (using new and default constructor)   
    }  
}

2. Use the new operator and the constructor of the class to assign variables to the declared object, i.e. create the object.

class Person   
{  
    int age;  
    String name;  
  
    Person(int age,Stirng name){  
        System.out.println("the man age is"+age+"name: "+name);  
    }  
  
    void speak(String name){  
        System.out.println(name);  
    }  
}  
public  class OuXiang  
{  
    public static void main(String[] args) {  
        Person liudehua;             // declare the object   
        liudehua= new Person(53,"Andy Lau");   // Assign variables to variables (using new and the constructor in the class)   
    }  
}

3 Object memory model

When the variable liudehua is declared with the Person class, that is, the object liudehua, Person liudehua; the memory model is as follows
 
liudehua
null
After declaring the variable, there is still no data in the memory of liudehua. At this time, the liudehua is still an empty object, and the object cannot be used, because it has no entity yet, and a variable must be allocated to it, that is, an entity is allocated to the object.
 
liudehua=new Person(53,"Andy Lau"); At this point, a variable is assigned to the object.
After the new operator allocates memory for the variables age and name, it will return a reference to the object variable liudehua, that is, return 0xAC12 (equivalent to an identity id) in the above figure. The reference contains the memory location and related important information representing the member variable.
 
interest.

Guess you like

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