In the real world, finding a mate is an art, and finding a mate is not a matter of quantity, but of quantity

In the computer world, the key of object-oriented programming lies in the flexible use of classes, and how to design an object that meets the needs is also worth learning and thinking.

So what exactly is object-oriented programming?

In object-oriented programming, the concepts of class and object are definitely involved. What is a class? What is the object? What is the relationship between the two?

Let’s answer these questions one by one

Classes and objects

  • Class is a group of things with the same properties. A class is a template that describes the state and behavior of a class of objects
  • Objects are individuals that actually exist in a class, so they are also called instances.
  • The abstraction of an object is a class, the materialization of a class is an object, that is, an instance of a class is an object.

In C, a structure is a collection of data that binds the data together so that we can think of the data as a whole. The functions that operate on the data in the structure are written outside the structure.

In object-oriented programming, the function that represents the behavior of things is also put into this whole, forming the concept of object. So that this whole thing describes both properties and behaviors.

Therefore, object-oriented programming is a programming method that focuses on the Object itself. The elements of the Object include the behavior and operation of the Object, based on which programming is conducted.

This approach makes the program easy to reuse. OOP mainly uses three programming techniques: inheritance, encapsulation and polymorphism.

Said so much, is not dizzy, it doesn’t matter, continue to look.

Abstractions in the real world

  • class

In real life, people can be considered as a class, and this class is called human (abstract class)

If a guy is looking for a girlfriend, then all girls are the boy’s range. All girls are one type.

  • object

If this time the boy has found the object of love, his girlfriend name is [Lin Yuner]. Then assume that the name is unique, the name 】 【 Lin Yun son of the girl is an object (bb in a low voice, in fact {she is my wife | h ē h ē h ē h ē} [head])

Next through the specific code to explain

// FileName: Person.java
public abstract class Person {

	protected int age;
	protected String name;
	
	public Person(int age,String name) {
		
		this.age = age;
		this.name = name;
		
	}

	public abstract void speak(a);

	public abstract void sayInfo(Person person);

}

Copy the code

Here, we define an abstract class — people. In this abstract class of people, it contains some attributes and behaviors of people and represents the common attributes of people

We then define subclasses Man and Woman to inherit from Person, including the shared properties, and add sex, and rewrite the method

// FileName: Man.java
public class Man extends Person {
	private String sex = "man";
	public Man(int age,String name) {
		super(age,name);
	}
  
	@Override
  
	public void speak(a) {
		System.out.println("My name is:" + super.name + "\n" + "My year" + super.age + "Old.");
	};

	@Override	
	
	public void sayInfo(Person person) {
		System.out.println("My girlfriend is :" + person.name + "\n" + "This is her year." + person.age + "Old."); }}Copy the code

// FileName: WoMan.java
public class WoMan extends Person {
	private String sex = "woman";
	public WoMan(int age,String name) {
		super(age,name);
	}
  
	@Override
  
	public void speak(a) {
		System.out.println("My name is:" + super.name + "\n" + "My year" + super.age + "Old.");
	};

	@Override	
	
	public void sayInfo(Person person) {
		System.out.println("My boyfriend is :" + person.name + "\n" + "This is her year." + person.age + "Old."); }}Copy the code

So, their relationship looks like this:

So, the basic composition of a class looks like this:

So, let’s write the test code again:

// FileName: TestPerson.java
public class TestPerson {
	/ * * *@param args
	 */
	public static void main(String[] args) {
		Person codevald = new Man(21."codevald");
		Person lye = new Woman(20."linyuer"); codevald.speak(); codevald.sayInfo(lye); lye.speak(); lye.sayInfo(codevald); }}Copy the code

Running results:

Now, the most interesting part, let’s look at what happens when new happens in the code.

New operation process

When we create a new object, the JVM will first find the corresponding class meta information. Failure to find it means that the class information has not been loaded, so it may trigger the class loading operation when the object is created. After the class meta information is loaded, we can use the class meta information to determine the object information and the required memory size.

The flow of object creation

1. Build objects

First, the main thread claims a stack space of its own. Then, when we call the main method, we generate a stack frame for the main method and execute new Man(), which determines the size of the object based on the meta information of the Man class. Then, we apply a memory area in the JVM heap and build the object. At the same time, the Man object member variable information and give the default value (here will involve polymorphism, in memory allocation situation will have a chance to explain).

2. Initialize the object

This step will execute the internal init method of the object to initialize the value of the member variable, that is, execute the constructor of the object (here the constructor of the parent class is called for assignment). When the constructor is finished, age = 21,name = “codeVald”.

3. Reference objects

After the object is instantiated, the reference address of the Person object on the stack points to the address of the Man object in the heap

4. Continue to construct, initialize, and reference object objects

This step is the same as the previous three steps, which are not explained in detail

5. Invoke methods

Call the speak() method, which first finds the reference address in the Person object (codeVald), then finds the actual Man object in the heap. Execute the Speak () method, which calls the member variables in the parent class, so it finds the member variables (name and age) of the parent object in the heap. Load it in, print it out

My name is codeVald and I am 21 years old.

When you call sayInfo (), you will find the codeVald of the Man object in the heap. When you execute sayInfo (), you will find the address of the Person object (Linyuner), which refers to the Woman object in the heap. Find the member variables (name and age) in the parent class and print them

My girlfriend is Linyuer. She is 20 years old.

The following code execution process is the same, it is left to the partners to analyze ~

Attached is the TestPerson bytecode file for interested parties to view and analyze by themselves

Compiled from "TestPerson.java" public class person.TestPerson { public person.TestPerson(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: new #7 // class person/Man 3: dup 4: bipush 21 6: ldc #9 // String codevald 8: invokespecial #11 // Method person/Man."<init>":(ILjava/lang/String;) V 11: astore_1 12: new #14 // class person/Woman 15: dup 16: bipush 20 18: ldc #16 // String linyuer 20: invokespecial #18 // Method person/Woman."<init>":(ILjava/lang/String;) V 23: astore_2 24: aload_1 25: invokevirtual #19 // Method person/Person.speak:()V 28: aload_1 29: aload_2 30: invokevirtual #24 // Method person/Person.sayInfo:(Lperson/Person;) V 33: aload_2 34: invokevirtual #19 // Method person/Person.speak:()V 37: aload_2 38: aload_1 39: invokevirtual #24 // Method person/Person.sayInfo:(Lperson/Person;) V 42: return }Copy the code

There is no end to learning. The things we used to be good at are being eliminated, but the things we weren’t good at are still there. The most basic is often the most difficult, and often is the most important, usually pay attention to the accumulation of basic, learn to analyze the bottom of the implementation process, is the most should master skills in learning, I hope this answer can help you are looking for the answer to this question ~

If you think this article is good, please give me a thumbs up at @codeVald. Thanks for your support!