Java Notes Name Morning Summary 07- Classes and Objects-Elementary

Classes and Object Oriented Programming-Elementary
What is Object Oriented Programming?
Does it mean having a girlfriend and programming in front of a girlfriend?
The actual meaning of a concept in many programs is somewhat different from its superficial meaning.
Suppose we want to develop a tank battle game.
Our game has some attributes.
How many lives are currently remaining (if it is dead, the game ends, there are 3 lives at the beginning) the
current game score (each time the enemy
is killed ) whether the boss is dead (death) Restart the game and lose one life)
We have a protagonist (tank) which has some attributes,
level (the higher the level, the stronger the attack power, the initial level 1)
current HP
We have some types of enemies,
ordinary enemies, copper enemies, gold Enemies
Different enemies have different attack power and blood volume.
Enemies can move by themselves.
Interaction
can control the movement of the protagonist.
You can shoot bullets to attack the enemy. The
enemy will automatically track the protagonist and attack.

When the program we write has a lot of code, we can classify the things in the program, one type is related to the protagonist, one type is related to the enemy, and one type is related to the entire game.
Classification can make the program more structured. In the program, you can use class to classify the code. When using class programming, the variable created is called an object. This is the legendary object-oriented programming.

The difference between object-oriented and process-oriented. There are no objects in the process-oriented process. It emphasizes the steps and processes of developing a function, step-by-step class, object-oriented, we operate and deal with objects one by one, (detailed steps and processes of each function are encapsulated in the class, through Class classification division module) program is more structured. (More suitable for the development of large-scale projects, and more convenient for later maintenance)

How to illustrate the difference between "object-oriented" and "process-oriented" in an easy-to-understand way?
https://www.zhihu.com/question/27468564

Three characteristics of object-oriented (class, object):
encapsulation, inheritance, polymorphism (understand slowly later)

A class represents a category (such as the above example, there is a protagonist enemy game management system)
through a class to manage all related things in this category. Like enemy, protagonist

We can define a human protagonist class to control the management of properties and behaviors (methods) related to the protagonist.
The attributes and behaviors mentioned here are called member variables and member methods in our program.

How to define a class?
class Class name{ //Member variables (the properties of this class) //Member methods (what behaviors and functions does this class have) } How to declare objects Class name and object name; after the class is defined, why use the class to define variables (Object)? A class is equivalent to a template, which can be used to produce instances. The class is equivalent to the car model (mold) in the car factory, and the car model can be used to generate many cars that can be actually used. class








Car model objectCars
(enemy class and many enemies)

What are the attributes and behaviors of the protagonist?

How to use classes to create objects?

Use attributes, call behavior

The problem of putting the elephant into the refrigerator,
analyze which kinds of
elephants, refrigerators

Assignment between objects (assignment by reference)

Member variables and local variables
Defined location
When does the memory
occupy
? When does the occupied memory are destroyed
Initial value

When an object is used as a method parameter,
because the class is a reference type, when the data in the reference type is modified in the method, the data in the original reference type is also modified!

What is an anonymous object?
If this object is only used once

The garbage collection mechanism GC (JVM) in Java

Educational management system
If we want to develop an educational management system. This educational administration management system is for schools to manage students and teachers, such as student achievement information management, teacher all information management... etc.

Student class creation
Teacher class creation

How objects are stored in memory

Access modifier private
(during project development, the classes we develop are not only for our own use but also for other people in the team)
can modify member variables, and can modify member methods.
Encapsulation:
hide all attributes that do not need external access to
provide methods Allow access to the outside world
Advantages:
make the code structure clearer and
safer (writing code is not easy to make mistakes, especially for large projects)
Use:
all properties are privatized
Provide getter and setter method access

About this keyword
Use
this to represent the current object in class definition.
Advantages:
distinguish method parameters and member variables
through this. It is convenient for IDE to give hints through this (eclipse demonstration later)

The construction method (used to construct the object) is
what will be called (when new) the
default construction method (only when there is no construction method, the system will provide the default construction method without parameters)
format:
modifier class name (parameter ){}

Guess you like

Origin blog.csdn.net/qq_33961136/article/details/108563908