Object Orientation: The relationship between classes and objects

class-object relationship

class-object relationship

A class is an abstract data type that describes/defines a certain class of things as a whole, but does not represent a specific thing

  • animals, plants, mobile phones, computers
  • Person class, Pet class, Car are all used to describe and define the characteristics and behaviors that a certain class of specific things should have

Objects are concrete instances of abstract concepts

  • Zhang San is a concrete example of a person, and a wealthy person in Zhang San’s family is an example of a dog
  • It can reflect the characteristics and show the function is a concrete instance, not an abstract concept

Created on initialization object

Class-to-Object Creation

​ Use the new keyword to create an object

  • When creating with the new keyword, in addition to allocating memory space,

    It will also default-initialize the created object and call the constructor in the class

//student class
public class Student {

    //Attribute: Field
    String name; //Name
    int age;     //age

    //method
    public void study() {
        System.out.println("student studying");

        //But there is more than one student, and there will be changes. So we use the following method:
        System.out.println(this.name + "studying");//this represents the current class
    }
}

//A project should have only one main method
public class Application {
    public static void main(String[] args) {

        //class: abstract, instantiated
        Student student = new Student();

        /*When instantiated, it will return an own object
        student An object is a concrete instance of the Student class!*/

        //Don't understand?  look again
        Student xiaoming = new Student();
        Student xiaohong = new Student();
        //These are two different objects (Xiao Ming, Xiaohong), but both have the properties of name and age and the study method, because they are both Student classes (the same class can generate different objects)

        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);
        
        //The output is:
        null
        0
		//Because name is not assigned, the default initialization of String is null, and the default initialization of int is 0
            
    }
}

//look again
public class Application {
    public static void main(String[] args) {

        //Create two student objects
        Student xiaoming = new Student();
        Student xiaohong = new Student();

        //Assign value to the student Xiaoming
        xiaoming.name = "Xiao Ming";
        xiaoming.age = 8;

        //Output Xiaoming's information
        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);

        //Assign value to the student Xiaohong
        xiaohong.name="little red";
        xiaohong.age=10;
        
 		//Output Xiaohong's information
        System.out.println(xiaohong.name);
        System.out.println(xiaohong.age);
       
        //The output is
        Xiao Ming
		8
		little red
		10
    }
}

//The above code needs to be written again with this, and practice will bring true knowledge

Constructor details

//Open the output directory (see screenshot below)
public class Person {

}

Open output directory

Add root directory to Modules

Now you can see the out directory, which is the output directory, which will generate Class

Check the comparison: It can be seen that a method is added by default in the Class file on the right, and this method has no return value. The method name is the same as the class name. In fact, this is a constructor

From this a conclusion can be drawn:

"Even if a class does not write anything, it will have a method, and this method is the constructor"

  • The constructor in the class also becomes the constructor method, which must be called when creating an object.

    And the constructor has the following two characteristics:

    • Must be the same as the class name
    • There must be no return value, and void cannot be written
public class Person {

    //show a constructor
    public  Person(){

    }
}
  • This is a constructor, no-argument constructor

  • What can a parameterless construct do?

    • can give us some information to initialize
    public class Application {
        public static void main(String[] args) {
            
    		//Instantiate an object using the new keyword
    	    Person person = new Person();
    
            System.out.println(person.name);//In the case of no value, the default value is null (this is where the constructor can come in handy)
         }
    }
    
    public class Person {
    
         String name;
    
        //The first role of the constructor is to instantiate some objects
        
        //instantiate initial value
        //No-argument constructor
        public Person() {
            this.name = "Meimei";
        }
        
    }
    
    //The output is
     Meimei
    
    	//Constructor with parameters: Once a constructor with parameters is defined, no parameters must be explicitly defined (otherwise it is invalid)
    	public Person(String name) {
            	this.name = "Meimei";
        }
    

The core of the constructor has two functions

  • 1. When using the new keyword, the essence is to call the constructor
  • 2. Constructors are generally used to initialize values

Constructors must master

Tags: Java programming language

Posted by kigroy on Tue, 31 May 2022 19:40:14 +0530