Creation of classes and objects, detailed explanation of constructors and memory analysis of objects 2021-11-27

Posted by ale1981 on Sun, 28 Nov 2021 09:34:20 +0100

Day23

Creation of classes and objects

Relationship between class and object

Class is an abstract data type. It is an overall description / definition of a class of things, but it does not represent a specific thing.
Objects are concrete instances of abstract concepts.

Creating and initializing objects

  • When using the new keyword, in addition to allocating memory space, the created object will be initialized by default and the constructor in the class will be called.
  • Constructors in classes, also known as construction methods, must be called when creating objects, and constructors have the following two characteristics:
  1. Must be the same as the class name
  2. There must be no return value type, and void cannot be written
Constructors must master

Default initialization example:

package com.opp.demo02;
//Student class
public class Student {
    //Properties: Fields
    String name;//null
    int age;    //0

    //method
    public void study(){
        System.out.println(this.name+"I'm learning");
    } 
}
package com.opp.demo02;
//There should be only one main method in a project
public class Application {
    public static void main(String[] args) {
        //Class: abstract and needs to be instantiated
        //Class returns its own object after instantiation
        //A student object is a concrete instance of a student class
        Student xiaoming = new Student();
        Student xiaohong = new Student();

        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);
    }
}


Assignment example:

package com.opp.demo02;
//Student class
public class Student {
    //Properties: Fields
    String name;//null
    int age;    //0

    //method
    public void study(){
        System.out.println(this.name+"I'm learning");
    }
}
package com.opp.demo02;
//There should be only one main method in a project
public class Application {
    public static void main(String[] args) {
        //Class: abstract and needs to be instantiated
        //Class returns its own object after instantiation
        //A student object is a concrete instance of a student class
        Student xiaoming = new Student();
        Student xiaohong = new Student();

        xiaoming.name="Xiao Ming";
        xiaoming.age=20;

        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);

        xiaohong.name=("Xiao Hong");
        xiaohong.age=(23);
        
        System.out.println(xiaohong.name);
        System.out.println(xiaohong.age);

    }
}

Constructor details

Constructor action

  • Using the new keyword is essentially calling the constructor
  • Used to initialize values

Method of opening class file with idea
1.

2. Add the out root directory in modules and click OK

3. An out directory will be generated

4. First, a method will be added by default without return value type. Second, the method name must be the same as the class name

package com.oop.demo02;
//java-->class
public class Person {
    //Even if a class writes nothing, it will have a method
    //Display definition constructor
    String name;
    //Constructor function: instantiate initial value
    public Person(){

    }
}

package com.oop.demo02;
//There should be only one main method in a project
public class Application {
    public static void main(String[] args) {
        //new instantiates an object
        Person person = new Person();
        System.out.println(person.name);  //null
    }
}


Use this keyword to output directly

package com.oop.demo02;
//java-->class
public class Person {
    //Even if a class writes nothing, it will have a method
    //Display definition constructor
    String name;
    //Constructor function: instantiate initial value
    
    //1. Using the new keyword is essentially calling the constructor
//    public Person(){
//        this.name = "xiaoxiao";
//    }
    //Parametric Construction: once a parametric construction is defined,
    //If there is no parameter, the definition must be displayed, otherwise an error will be reported
    public Person(String name){
        this.name = name;
    }
}
package com.oop.demo02;
//There should be only one main method in a project
public class Application {
    public static void main(String[] args) {
        //new instantiates an object
        Person person = new Person();
        System.out.println(person.name);//xiaoxiao
    }
}


When a parameterless construct has no method body, it is null when called

Remember a shortcut key

alt+insert automatically generates structures with and without parameters
The specific method of generating parametric structure is as follows:

  1. Press alt+insert and enter

  2. Click OK to automatically generate a parameterized structure

The specific method of generating parameterless structure is as follows:

  1. Press alt+insert and enter
  2. Click OK to automatically generate a parameterless structure

Summary

Constructor:
1. Same as class name
2. No return value
effect:
1. The essence of new is to call the construction method
2. Initialize the value of the object
Note:
1. After defining a parametric structure, if you want to use a nonparametric structure,
The displayed definition is a parameterless construct

alt + insert
this. Represents that the value equal to after the current class is the value passed in by the parameter

Create object memory analysis

package com.oop.demo03;

public class Pet {
    public String name;
    public int age;
    //Nonparametric structure
    public void shout(){
        System.out.println("Let out a cry");
    }
}
package com.oop.demo02;
import com.oop.demo03.Pet;
//There should be only one main method in a project
public class Application {
    public static void main(String[] args) {
        Pet dog = new Pet();
        dog.name = ("peas");
        dog.age = 3;
        dog.shout();

        System.out.println(dog.name);
        System.out.println(dog.age);
    }
}


Memory analysis diagram

Topics: Java Algorithm