Object-oriented Creation and Use of Classes and Objects

Creation and use of classes and objects

Object-oriented - About the creation and use of classes and objects

Preface

Class and Object are core object-oriented concepts.
Classes are descriptions of a class of things and are abstract and conceptual definitions.
An object is each individual of this kind of thing that actually exists and is therefore called an instance.
Object-oriented programming focuses on class design. A design class is a member of a design class.
Here is a simple example for a better understanding of classes and objects:
Class = people with abstract concepts; Object = Real Someone

1. Two basic members of a class - attributes and methods

As mentioned earlier, design classes are members of design classes. Here we give you a brief introduction to the two most basic members of a class: attributes and methods.
In the Java language, the properties of a class (field) can also be called member variables, or fields or domains. Class methods, also known as member methods, are also called functions in other programming languages, such as C and JavaScript.
Next, I'll give you a brief introduction to the format of their declarations.
1) Attribute declaration format:
Data type variable name = variable value;
(Personally, attributes are a representation of variables)
2) Method declaration format:
Permission modifier returns value type method name (parameter){
Method body;
}
Note: The method's permission modifiers are classified as private, public, default, and protected. The details are not expanded here. The explanation and use of this content will be updated in succession.

2. Creation of Classes and Objects

1. Class creation

Creating a class is really simple. We can create a class by using the class keyword in the following format:
Class class class name {

}
Combining with the explanations of the above-mentioned class design, show you a code for better understanding

The code is as follows:

//1. Create classes, design class members
class Person{
	//attribute
	String name;
	int age = 1;
	boolean isMale;
	
	//Method
	public void eat() {
		System.out.println("Eat something");
	}
	
	public void sleep() {
		System.out.println("Sleep");
	}
	
	public void talk(String language) {
		System.out.println("Human Use" + language + "Communication");
	}
}

2. Objects that create classes

In the Java language, objects that create classes = instantiations of classes = instantiations of classes.
For the creation of objects of a class, we can understand literally that the relationship between the object and the class is an inclusive relationship, that is, the class needs to be created first, and then the class can be created before it is declared.
The object is created in the following format:
Variable type variable name= new constructor ();
Note: Here's a key point to explain that the real object is the new constructor () to the right of the equal sign, not "variable type variable name= new constructor ();" This whole, and "variable type variable name= new constructor ();" The expression means that the created object is assigned to a variable in the form of an address value.

The code is as follows:
The real object here is new Person(), Person p1 = new Person(); Is to assign the address value of the new Person() object to a variable of type Person with the variable name p1.

// 2. Create Objects of Person Class
Person p1 = new Person();

3. Calling the structure of an object through Object-Attribute or Object-Method

Once the class and object are created and the properties and methods in the class have been declared, we can invoke the declared properties and methods in the corresponding class through the objects that have been created.

The code is as follows:

// 3. Call object structure: properties, methods
// Call property:'object. property'
p1.name = "Tom";
p1.isMale = true;
System.out.println(p1.name);

// Call Method: Object. Method
p1.eat();
p1.sleep();
p1.talk("Chinese");

An example of integrating all the above code is as follows:

The code is as follows:

public class test {
	public static void main(String[] args) {
		// 2. Create Objects of Person Class
		Person p1 = new Person();

		// 3. Call object structure: properties, methods
		// Call property:'object. property'
		p1.name = "Tom";
		p1.isMale = true;
		System.out.println(p1.name);

		// Call Method: Object. Method
		p1.eat();
		p1.sleep();
		p1.talk("Chinese");
	}
}

//1. Create classes, design class members
class Person {
	// attribute
	String name;
	int age = 1;
	boolean isMale;

	// Method
	public void eat() {
		System.out.println("Eat something");
	}

	public void sleep() {
		System.out.println("Sleep");
	}

	public void talk(String language) {
		System.out.println("Human Use" + language + "Communication");
	}
}

The output is:

Tom
 Eat something
 Sleep
 Human Use Chinese Communication

4. Experience the relationship between multiple objects of a class

If multiple objects of a class are created, each object has its own set of properties. Meaning: if we modify the property a of one object, it will not affect the value of another object property a. (provided the properties of the class are not static)
Note: If an object's attributes are not assigned, the output value is the default value for its data type.

The code is as follows:

Person p2 = new Person();
System.out.println(p2.name);// null
System.out.println(p2.isMale);//false
//Assigning the object address value saved by the p1 variable to p3 causes p1 and p3 to point to the same object entity in heap space
Person p3 = p1;
System.out.println(p3.name);// Tom
p3.age = 10;
System.out.println(p1.age);// 10

The output is:

null
false
Tom
10

Tags: Java OOP

Posted by jam123 on Mon, 02 May 2022 06:59:08 +0300