Understanding of face-to-object (Java)

1, facing the object (oop)

Object-oriented: Everything is an object, pay more attention to things, do your own things by yourself.

For example: What should I do if I want to put the elephant in the refrigerator? Many people would think of opening the refrigerator door first, then putting the elephant in, and closing the door finally. This put the elephant in. From the above description, we can conclude that opening the refrigerator door, then putting the elephant in, and finally closing the door. This is actually a process, and the final result is to put the elephant in the refrigerator. In this process, we can treat the refrigerator as an object and open the refrigerator door, put the elephant in, and then close the refrigerator door. We can all regard this series of actions as the method of the refrigerator object, and we have to take Put the elephant in the refrigerator. We only need to call these methods of the refrigerator. As for how the refrigerator implements these methods, you don’t need to know. All you need to know is the result: the elephant is placed in the refrigerator. This is object-oriented.

Advantages: Low code coupling, high code reusability, easy to extend and maintain. Low coupling, high cohesion.

The object-oriented language has high reusability and high development efficiency.

Disadvantages: Code operation efficiency is low.

Abstraction: Abstract a concrete thing into a class (file).

Properties (static): Variables defined in the class.

Behavior (dynamic): methods defined in the class.

Code understanding:

package com;

/*
* Class:
* Abstract a thing into a class (abstraction: in the process of abstraction, abstract whatever is used)
*-When defining a class, in the class Can only contain attributes and behaviors
* -Attributes (static): variables defined in the class
* -When attributes are defined, the system will initialize them by default
* -Value: 0
* -char :’’
* -boolean:false
* -Reference data type: null
* -Behavior (procedure): method defined in class
* -Why do we need to define class?
* -In order to save data that cannot be stored in basic types
*
* Object:
* */
public class Student {//Create a student class
//Properties
int id;
String name;
char sex;
int age;

//行为
void eat() {
System.out.println(name + “Eat!”);
}
}

package com;

public class Test {//Create a test class

p>

public static void main(String[] args) {
// TODO Auto-generated method stub
// Need to save a student’s information
int a = 10;
/ *
* Instance, object (a specific instance of the class)
* Create object: class name object name = new class name();
*-a space stu is allocated in the stack space br> *-new: allocate a piece of memory in the heap space for more classes
*-let the stu allocated on the stack execute the memory address allocated by the heap space
* */
Student stu = new Student() ;
/*
* Object access:
* Object name. Attribute name: Read and write attributes
* Object name. Behavior: call behavior
* */
stu.name = “诸葛亮”;
stu.age = 22;

stu.eat();

Student stu1 = new Student();
stu1.name = “貂蝉” ;
stu1.eat();

Student stu2 = null;
stu2.name = “刘备”;
}

}

The print result is:

Zhuge Liang has a meal! Diao Chan eat!

Leave a Comment

Your email address will not be published.