Basic concepts of javaoop


JavaOOPBasic concepts of

After learning the basic syntax of Java, we can understand the common program logic of Java programs and solve simple common problems. Just like building a city, learning the foundation can only build a village, learning the object-oriented can create a city.

Compared with the early process oriented approach, the stability, scalability and reusability of object-oriented have incomparable advantages.

Later, I will learn how to use java language to realize the three characteristics of object-oriented: encapsulation, inheritance and polymorphism.

What is an object

All things are objects, objective things.

An entity used to describe an objective thing, consisting of a set of attributes and methods.

What is object-oriented

Object oriented, which literally means face-to-face object, focusing on the object.

From the point of view of computer program, it can be understood that we should pay attention to all aspects of the information of reality, and carry out relevant program design according to the characteristics of things from the point of view of objects.

What is a class?

A class is a template that determines the properties and methods that an object will have.

Class is the type of object.

A collection of objects with the same properties and methods.

What are the properties and methods of an object

For example, if a cat is an object, its attributes are static features and its methods are dynamic features.

Static features: name, color, variety

Dynamic features: eating, playing, sleeping

Relationship between class and object

Class is an abstract concept, and object is a concrete entity.

Class is the type of an object, and object is data of a specific type.

In development, you need to define the class in the instantiated object first.

Principle of single responsibility

Single responsibility principle, also known as single responsibility principle (SRP), is an important principle in object-oriented.

A class should have and only have one cause for change.

Try to put different changes in different classes, that is, encapsulate different functions in different classes.

Object instantiation

The process of instantiating an object is divided into two parts:

Declared object: class name object name;

Instantiation object: new class name ();

Combine two into one: class name object name = new class name ();

One is the name of the object created.

Each time a new object is created, a new instanced object is generated. Such as:

Dog one = new Dog();

Dog two = new Dog();

Multiple objects can point to the same instanced space

Objects must be instantiated before they can be used. In fact, the reference passing between objects transfers the right to use the heap memory space.

Construction method

The constructor has the same name as the class and has no return value. It can only be called when the object is instantiated.

Public constructor name (parameters can be specified) {/ / initialization code}

When no constructor is specified, the system automatically adds a nonparametric constructor. When a construction method is specified, no parameter construction method will be added automatically, whether it is a construction method with or without parameters. A class can have multiple construction methods.

This keyword

This: the default reference of the current object.

It can solve the conflict between calling member property and local property with the same name.

You can also call the constructor.

This (parameter list);

Keep up with what I have learned for a long time.

Error hope correction

We will be better, come on!

See you in the comment area!