JAVA object-oriented 01 -- Understanding classes and objects

Posted by nawal on Tue, 08 Mar 2022 21:42:04 +0100

OOP

Key content in Java!

What is OOP

OOP: Object Oriented Programming
Java is object-oriented. OOP is a process of using java language to describe objectively existing things and things, and using the logic of the program to connect these things and things and interact for a certain purpose. Therefore, you must be familiar with OOP in order to use Java well.

Program composition of OOP

Object oriented programs are composed of objects, and each object contains public and hidden parts for users.

Cognitive class and object

What is a class

A class is a template that describes a class of objects, behaviors and states. Attributes and behaviors (Methods) are defined in a class.

What is an object

An object is an instance of a class, which represents an independent individual. Each object has its own attributes. Different objects are distinguished by different attributes.

Relationship between class and object

A class is a template for an object, and an object is an instance of a class. Classes can only be used through objects. In development, classes should be generated first, and then objects. Classes cannot be used directly, but objects can be used directly.

Definition and use of classes and objects

definition

{
class Class name{

	variable(attribute);
	
	method(behavior);
}

Example: define a person class, which has two attributes: name and age, and an introduction behavior.

public class Person {
    String name;
    int age;

    public void introduce(){
        System.out.println("My name is:"+name+",Age:"+age);
    }
}

use

Instantiate object

1. Declaration and instantiation are separated.

/*
	Class name object name = null;
	Object name = new class name ();
*/
Person person = null;
person = new Person();

2. Declare and instantiate objects

//Class name object name = new class name ();
Person person = new Person();

Using Object Manipulation Classes

//Object name Attribute name: indicates the attribute in the calling class;
//Object name Method name (): refers to the method in the calling class.

    public static void main(String[] args) {
    	//Declare object person1
        Person person1 = null;
        //Instantiate object
        person1 = new Person();

		//Assign values to the name and age attributes of person1
        person1.name = "WANWAN";
        person1.age = 21;

		//Introduction to calling person1
        person1.introduce();
		
		Person person2 = new Person();
        person2.name = "MOMO";
        person2.age = 22;
        person2.introduce();
    }

Operation results:

My name is: WANWAN,Age: 21
 My name is: MOMO,Age: 22

The difference between the two methods of instantiating objects

Analyze the instantiation process from the perspective of memory;
First, let's learn what two kinds of memory space are used for:
1. Stack memory: it stores the address of heap memory (for easy understanding, it is like the name of the saved object).
2. Heap memory: it stores the attribute content of the object. The heap memory needs to allocate space with the new keyword.

The first method:




The second method:




It can be seen that the second method is to combine the first and second steps of the first method

Object reference passing

Let's look at the code first, or the two objects just now. Let's let person2 = person1 to see the results.

  public static void main(String[] args) {
        Person person1 = null;
        person1 = new Person();
        person1.name = "WANWAN";
        person1.age = 21;
        person1.introduce();

        Person person2 = new Person();
        person2.name = "MOMO";
        person2.age = 22;
        person2.introduce();

        person2 = person1; //Reference passing
        person2.name = "FANFAN";
        person1.introduce();
    }

Operation results:

My name is: WANWAN,Age: 21
 My name is: MOMO,Age: 22
 My name is: FANFAN,Age: 21

You can see through person2 Name modified the value of the name attribute, and the value of the name attribute of person1 also changed.

Let's look at the process in memory:


Construction method of class

In Java, every class has a constructor. If you do not explicitly define a constructor for a class, the java compiler will provide a default constructor for that class.
When creating an object, at least one constructor must be called. The name of the constructor must have the same name as the class. A class can have multiple constructors.

Person class:

public class Person {
    String name;
    int age;
    //Nonparametric structure
    public Person() {
   		System.out.println("A parameterless construct was called");
    }
	//Parametric structure (1 parameter)
    public Person(String name){
    	System.out.println("Call with name Parametric structure");
        this.name = name;
    }
	//Parametric structure (all parameters)
    public Person(String name, int age) {
    	System.out.println("All parameter construction called");
        this.name = name;
        this.age = age;
    }
}

Let's take a look at some tests to find out the call without participation and parameters.

public class Test {
    public static void main(String[] args) {
       Person person1 = new Person();//No parameters
    }
}
//Operation results:
//A parameterless construct was called


 public class Test {
    public static void main(String[] args) {
       Person person1 = new Person("WAN",21);//Full reference
    }
} 
//Operation results:
//All parameter construction called


 public class Test {
    public static void main(String[] args) {
       Person person1 = new Person("WAN");//With name parameter
    }
} 
//Operation results:
//A parameterized construct with name was called

public class Test {
    public static void main(String[] args) {
       Person person1 = new Person(22);//With age parameter
    }
}
//Operation results:
//Error: incompatible type: int cannot be converted to Java lang.String

Through these tests, it can be seen that the written construction method will be selected according to the parameters carried by the new object. If there is no corresponding construction method, an error will be reported

Member method of class

Class member methods are actions owned by objects. For example, self introduction is an action of people, which can be called a method of people. Member methods are written by themselves, which is the same as writing code in the entry method.

Format:

 Access modifier  [Special modifier static/final/synchronized] Return type method name([parameter list ]){
       ...
   }

Example:

public class Person {
    String name;
    int age;

	//This is the member method
    public void introduce(){
        System.out.println("My name is:"+name+",Age:"+age);
    }
}

Topics: Java