Java Fundamentals 09 Object Oriented ~ Classes and Objects

Like you who love programming!
Learn SpringBoot actual combat course https://edu.csdn.net/course/detail/31433
Learn SpringCloud introductory course https://edu.csdn.net/course/detail/31451


Object-oriented series:
class and object
encapsulation
inherit
polymorphic
static keyword and singleton mode
interface and inner class

Preface

Object-oriented is the core knowledge point of learning Java, and it is also a difficult point for many students to learn Java. There are many concepts to understand. This article goes from simple to deep, from object-oriented concepts, classes and object concepts to object-oriented programming, and then to attributes And methods, construction methods, this keyword, and finally analyze object-oriented memory allocation to introduce you to Java object-oriented.

Object-oriented concept

Object Oriented (Object Oriented) is a programming model that organizes business-related data and methods as a whole, and performs system modeling from a higher level, which is closer to the natural operation mode of things.

Early program development was process-oriented, focusing on a series of steps to achieve business functions.
Take an example of a meal scene: What is
Insert picture description here
it to describe with a process-oriented thinking?

  1. Little Loli holding chopsticks
  2. Noodles
  3. Put in your mouth
  4. Chew
  5. Swallow

What is it to describe with object-oriented thinking?

  1. The analysis objects are:
  • 小Loli
  • chopsticks
  • bowl
  • noodles
  1. Relationship between analysis objects:
  • Little Loli uses chopsticks
  • Chopsticks noodles
  • Bowl to store noodles

The difference between process-oriented and object-oriented is:

  • Process-oriented is mainly concerned with the steps to realize the function and the details of the realization.
  • Object-oriented mainly focuses on objects and the relationship between objects, and does not pay attention to implementation details.

Classes and objects

The core concepts of object-oriented include: class and object. To understand object-oriented, we must first understand what class and object are.
What kind of horse?
A class is a collection of objects with the same characteristics and behaviors. For example, the heroes of LOL belong to the category, which refers to all heroes in the game.
Insert picture description here
Is the object a god horse?
A concrete individual in a certain class. For example: Timo is the object of a hero.
Insert picture description here
Exercise
Please give an example: Which are the classes and which are the objects in life?

Object-Oriented Programming

Object-oriented programming (Object Oriented Programming) requires the use of classes and objects to achieve.
A class is a model or blueprint of an object, similar to a design drawing of a car.
Insert picture description here

The object is a concrete instance produced according to the design of the class, similar to the actual car.
Insert picture description here
The process of OOP programming is:

  1. Definition class
  2. Create objects using classes
  3. Call object properties or methods

The syntax for defining a class is:

public class 类名{
	属性
	方法
}

Properties and methods

What are attributes?
The characteristics of the object are expressed by nouns.
Such as: car size, color, price, brand

What is the method?
The behavior of the subject is expressed using verbs.
Such as: starting, turning and braking of a car

How are attributes represented in the program?
Member variables defined in the class

public class Car{
 String color;  //颜色属性
 double price; //价格属性
}

How is the method defined?

public 返回值类型 方法名(类型 参数,...){
	方法体;
}

What is the syntax for creating objects?

类名 对象名  = new 类名();

How to call the properties and methods of an object?

对象名.属性 = 值;
对象名.方法(参数);

Example: Define the LOL hero class.
Attributes: name, blood volume, blue volume, attack power
Method: move, return to the city

public class Hero {
	//名字
	String name;
	//血量
	int hp;
	//蓝量
	int mp;
	//攻击力
	int attack;
	
	/**
	 * 移动
	 */
	public void move(){
		System.out.println("英雄"+name+",攻击力"+attack+"在移动!");
	}
	/**
	 * 回城
	 */
	public void back(){
		System.out.println("英雄"+name+"血量"+hp+",蓝量"+mp+",回城了!");
	}
}

public class TestHero {

	public static void main(String[] args) {
		//创建对象
		Hero hero = new Hero();
		//调用属性
		hero.name = "提莫";
		hero.hp = 500;
		hero.mp = 200;
		hero.attack= 100;
		//调用方法
		hero.move();
		hero.back();
	}

}

Exercise: Define the car category, define the brand, price, color attributes, define driving, braking, and turning methods. Create three different car objects in the main method, and call the properties and methods of the car.

Construction method

What does the construction method do?

  1. Create objects and allocate memory
  2. Convenient to initialize the properties of the object

How to define the construction method?

  1. The name is the same as the class name
  2. No return value type
  3. Can have parameters or not
public 类名(){
}
public 类名(类型 参数..){
}

How to call the constructor?
Use the new keyword

new 类名();
new 类名(参数);

Case: Add a constructor method to the Hero class

	public Hero(){
		System.out.println("調用Hero的無參構造方法");
	}

	public Hero(String name,int hp,int mp,int attack){
		this.name = name;
		this.hp = hp;
		this.mp = mp;
		this.attack = attack;
	}

Create object

Hero hero = new Hero("提莫",500,200,100);

Default construction method
Some students may ask: Why can you also use the new class name () to create an object when you don’t write a construction method?

  • Because if no constructor is defined, the compiler will automatically add an empty constructor with no parameters to the class, similar to: public class name(){}
  • If we manually add a constructor, the compiler will delete this default constructor.
  • Under normal circumstances, if we add a parameter construction method to a class, we will manually add a parameterless construction method to facilitate the creation of objects without parameters.

this keyword

this represents the current object
. The function of this keyword is

  1. Call the attributes of the current object, this. attribute
  2. Call the method of the current object, this. method name (…);
  3. Call the construction method of the current object, this(...)

The reason for using this in the construction method is that the parameter name and the attribute name are the same. In order to distinguish between them, the attribute is called with this, and the parameter is called without this.

this.name = name;

Exercise:
define the computer class, attributes: cpu, price, memory, graphics card type;
method: watch movies, play games
, define the construction method for the computer class to initialize the properties,
create computer objects, call watching movies and playing games

Object memory allocation

JVM is a virtual computer, which divides memory into 5 areas for management:
1) Program counter
saves the number of lines of execution code of thread context
2) Local method area
saves information about native methods
3) Method area
saves class information, static variables, and constants etc.
4) virtual machine stack
local variables, object reference (memory address)
5) heap
hold real objects
Insert picture description here

End

After writing the manual, leave a homework. If you have any questions, you can leave a comment.
1. Realize the game scene:
Ultraman fights monsters. Ultraman has attributes such as name, attack power, blood volume, and skills: flying, light attack;
monsters have Name, attack power and blood volume attributes, with skills: running, brute force attack.
Create Ultraman and Monster objects, and then call their properties and methods.


If you need to learn other Java knowledge, poke here ultra-detailed knowledge of Java Summary

Guess you like

Origin blog.csdn.net/u013343114/article/details/112345793