Important thing to say three times
We believe that object-oriented programming is a simulation of the real world, and that each of the classes in the program makes up our program, like the real world of human beings, cars, buildings , animals and so on these classes constitute the world.
We believe that object-oriented programming is a simulation of the real world, and that each of the classes in the program makes up our program, like the real world of human beings, cars, buildings, animals and so on these classes constitute the world.
We believe that object-oriented programming is a simulation of the real world, and that each of the classes in the program makes up our program, like the real world of human beings, cars, buildings, animals and so on these classes constitute the world.
One, create a class in Java
Syntax class Name {
Properties
Method
}
In this sentence, the class name is our abstraction of a series of things that have a common attribute, such as our class name "person" is to abstract people into a class, people have height, weight, name and so on, these are the attributes of the person class, define the name of this attribute we can write as
String name;
People have activities, such as talking, talking this behavior is the method in the above statement, the definition of a conversation this method can be written as
void Talk () {method Body}
The method body is the specific execution process that describes the behavior of the conversation.
Ii. Creation of objects
The person class is created so that we can use it to create an object that is specific to someone, such as
Person Itliu = new person ();
So we have itliu this person created, in this statement Itliu is the object name of the object we created, also known as a reference, it is like a person's name. The equal sign of "=" is to assign the newly created object to the Itliu reference. The reference is stored in the stack memory, the object is in the heap memory, and the reference in the stack memory points to the corresponding object in the heap memory.
An object has properties that we call a member variable
The behavior of an object we call it a member function
For example:
Let's say we've defined the person class.
Person Itliu = new person ();
Itliu.name; Age attribute of Itliu
Itliu.jump (); Tiliu's Jump behavior
Creation and use of anonymous objects
New person (). Jump;
This sentence we can understand, I saw a beautiful woman jumping on the street, but I didn't know her name.
Note: The anonymous object is usually one-time, which is like you see the beautiful woman jump, but you do not know her name so you and she just one side no longer see.
Class and object of Java object-oriented programming