Detailed explanation of object-oriented content
Whether it is object-oriented or process-oriented, they are both ways of solving problems, but from different angles.
Process-oriented:
emphasizes every aspect of solving problems Every step is done personally, and every detail is implemented manually.
Object-oriented:
Use specific functional objects to solve specific problems, every detail No need to pay attention, just create the corresponding object.
Object-oriented is based on process-oriented
Classes and objects and their relationships
Class: a collective name for things with the same characteristics and behaviors (functions), an abstract concept
Object: a certain individual in this type of thing
Classes and objects Relationship
A class can create multiple objects. A class is the abstraction of an object, and an object is an instance of a class.
Describes a thing----> Class
Characteristics ----> Member attributes (member variables)
Behavior (function) ---> Member methods
Class creation format:
Modifier class class Name {
Data type Member variable name 1;
Data type Member variable name 2;
Data type Member variable name 3;
...
Member method 1 ;
Member method 2;
Member method 3;
...
}
Modifier: public
Class name: Identifier See the name to know the meaning
Member variable list: the characteristics of things, only write the characteristics that the current scene is concerned about, it is not necessary to write all the characteristics of the things. The writing method is similar to defining variables
Member method: the behavior (function) of things, only write the characteristics that the current scene is concerned about Behavior (feature) is written similarly to the previous definition method. The difference is that static
needs to be removed when writing at this time. When a class is used for the first time, it must be loaded into the method area, and it must be used every time thereafter. The class does not need to be loaded repeatedly
The difference between member variables and local variables
1. The definition location is different
Local variables: defined inside the method
Member variables : Defined inside the class, outside the method
2. The storage locations in the memory are different
Local variables: Local variables are stored in the stack area
Member variables: Member variables are stored in the heap area
3 .Different life cycles
Local variables: generated with the call of the method, disappear with the end of the method relatively short
Member variables: generated with the creation of the object, disappear with the recycling of the object data relatively short Long
4. Different initial values
Local variables: no initial value, must be assigned before use
Member variables: have initial value, reference type null, integer 0, decimal 0.0 characters: space, Boolean false
Encapsulation
Encapsulation is one of the three major characteristics of object-oriented, the other two are inheritance and polymorphism
Class name: Person
Attribute: name ,age
Method: Demonstrate
Benefits of encapsulation: Improve program security
Steps to encapsulate attributes:
1 .Use the private keyword to modify the corresponding attribute to prevent the outside world from directly accessing the attribute through the object name. Attribute
2. Write the set and get methods of the corresponding attribute to give the outside world a channel to access the attribute
Encapsulation It is an object-oriented programming language's simulation of the objective world. In the objective world, member variables are hidden inside the object and cannot be directly manipulated or modified by the outside world.
Principles of encapsulation:
Hide content that does not need to be provided to the outside world.
Hide the properties and provide public methods to access them.
Member variables are private, providing corresponding getXxx()/setXxx() methods
Benefits:
Control the operation of member variables through methods, which improves the security of the code
Encapsulate the code with methods to improve the reusability of the code
Proximity principle of variable access:
When multiple variables with the same name appear, the target code uses the variable closest to this line of code
Problems that this can solve:
When local variables and member variables have the same name, and you want to access the member variables at a specific code point, you can add this in front of the variable name in the target line.
this only is the object that is currently calling this method
whoever calls this refers to who
Construction method
is a method. Function: assign a default value to the object's data
Definition format:
Modifier Method name (parameter list) {
Several initialization statements
}
Modifier: public
Method name: direct Write the class name
Parameter list: The same as writing the formal parameters of the previously defined method
Note that the constructor does not have a return value type
If we do not manually write a parameterless constructor, the system will provide one by default , if we manually write a constructor without parameters,
then the system will not provide it. When using it, we will directly use the constructor we wrote ourselves
The constructor is also a method and can be overloaded.
Whenever we write any constructor by hand, the system will not provide a parameterless constructor.
When we must use a parameterless constructor to create an object, we must write it by ourselves. Constructor without parameters
The above is the detailed content of Detailed explanation of object-oriented content. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

According to news from this site on April 17, TrendForce recently released a report, believing that demand for Nvidia's new Blackwell platform products is bullish, and is expected to drive TSMC's total CoWoS packaging production capacity to increase by more than 150% in 2024. NVIDIA Blackwell's new platform products include B-series GPUs and GB200 accelerator cards integrating NVIDIA's own GraceArm CPU. TrendForce confirms that the supply chain is currently very optimistic about GB200. It is estimated that shipments in 2025 are expected to exceed one million units, accounting for 40-50% of Nvidia's high-end GPUs. Nvidia plans to deliver products such as GB200 and B100 in the second half of the year, but upstream wafer packaging must further adopt more complex products.

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

This website reported on July 9 that the AMD Zen5 architecture "Strix" series processors will have two packaging solutions. The smaller StrixPoint will use the FP8 package, while the StrixHalo will use the FP11 package. Source: videocardz source @Olrak29_ The latest revelation is that StrixHalo’s FP11 package size is 37.5mm*45mm (1687 square millimeters), which is the same as the LGA-1700 package size of Intel’s AlderLake and RaptorLake CPUs. AMD’s latest Phoenix APU uses an FP8 packaging solution with a size of 25*40mm, which means that StrixHalo’s F

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

Title: Using Python's __le__() function to define a less than or equal comparison of two objects In Python, we can define comparison operations between objects by using special methods. One of them is the __le__() function, which is used to define less than or equal comparisons. The __le__() function is a magic method in Python and is a special function used to implement the "less than or equal" operation. When we compare two objects using the less than or equal operator (<=), Python

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values are passed and object references are passed.
