Three characteristics of object orientation

encapsulation

Objective things are encapsulated into abstract classes, which can expose or hide their attributes through access control. Let classes or objects you trust use their own exposed properties, and hide their properties from classes or objects you don’t trust

The purpose of this is to “separate the object itself from the user of the object. The user only needs to know what the object can do, not how it is implemented, so as to ensure the security of the class.”

As a not-so-good example, let’s say I have a 100-mile defense class with properties such as Health, gun, range of attack, rank, and methods such as Eye, snipe, jump back, and snipe

<? php
class BaiLi 
{

 public $blood; / / health  public $gun; / / gun  public $attackRange; // Range of attack  public $level; / / level // Set the level private function setLevel($level)  {  $this->level = $level;  }  / / eyes public function pushVision(a) {  echo "Push a Vision";  }  / / sniper public function sniper(a) {  echo "Shot a Shot";  }  // Jump back and snipe public function jumpAndSniper(a) {  echo "Jump And Shot";  } }  Copy the code

When a player chooses to keep a hundred miles, it is considered to have created an object of a hundred miles class. The object has these member attributes and methods encapsulated by this class, and the player can only see and use some of the attributes and methods provided by it, such as health, eye insertions, etc., but the setting level is not visible to the player. At the same time, the player only needs to know what attributes and skills are available, not how it is implemented

inheritance

Inheritance mechanism can create multiple levels of classes, “the subclass inherits the attributes and methods of the parent class, so that the subclass object also has the attributes and methods of the parent class object. It is usually used to extract public attributes or methods.”

Take the heroes in King of Glory for example. Look at the picture

When we try to cover all the heroes in King of Glory, we find that some of his heroes have public attributes. For example, heroes like Bali and Mengyasuo can launch ranged attacks, so we have extracted the public attributes and methods of his heroes into a lower class called Archer. Similarly, heroes such as Lian Po and Dun Shan can encapsulate a bottom class called tanks and so on

When encapsulating archer, mage, and tank classes, we find that they also have some common attributes, such as the ability to move, attack, etc., which can also be extracted and encapsulated into a lower class, that is, hero class

Assuming that king glory new hero is belong to the donkey, is a striker, first of all, it will have some special properties of its own and skills, and then the hero will inherit its parent class striker class, at this time the hero had ranged attack these skills, then striker class inherits the hero class, then the new hero with the move, attack the class attribute. At this point, an entire new hero is created (several hundred million hands).

polymorphism

Polymorphism can simply be understood as “one external interface, multiple internal implementations”. Its technical definition is: “The same operation on different instances of a class will result in different execution results.” That is, when objects of different classes receive the same message, they will get different results. The above description is still very abstract. Look at the example

In actual development, we usually write common code and make common programming to adapt to changing requirements

Using king of Glory as an example, suppose there are different classes of shooters, all of which have shooting methods

<? php

Class BailiShooter //{
 public function bailiShot(a) {  echo A hundred mile shot..PHP_EOL;  } }  Class mengYaShooter // mengYaShooter{  public function mengYaShot(a) {  echo "Montchallenger Shooting".PHP_EOL;  } }  function operateShot($obj= null)// Method to handle shooting{  if ($obj instanceof BailiShooter) {  $obj->bailiShot();  } elseif ($obj instanceof mengYaShooter) {  $obj->mengYaShot();  } else {  echo "Operation error";  } }  operateShot(new BailiShooter()); operateShot(new mengYaShooter());  Output:In the best shootingMongolia 犽 shootingCopy the code

Two striker classes are defined above: Mongchallenger and Barry. Then there is a method below to process the shot, determine which shooter is passed in to shoot, and then call the corresponding shooting method

As can be seen, if there is a new shooter shooting operation, we need to define the specific shooter class first, and then implement a shooting method, and then add a judgment in the method of handling shooting. This scalability is very poor, if the use of polymorphism can be a good solution to this problem

You can create a shooter’s parent class and define a shooting method in the parent class. All heroes with shooter attributes inherit this parent class (along with all attributes and methods of the parent class). Each subclass can implement the shooting method of the parent class

<? php

Shooter // Shooter{
 public function shot() { echo "Shooting methods should be overridden in all subclasses.";  } }  Class BailiShooter extends Shooter{  public function shot(a) {  echo A hundred mile shot..PHP_EOL;  } }  Class mengYaShooter extends Shooter // mengYaShooter{  public function shot(a) {  echo "Montchallenger Shooting".PHP_EOL;  } }  function operateShot($obj= null)// Method to handle shooting{  if ($obj instanceof Shooter) {  $obj->shot();  } else {  echo "Operation error";  } }  operateShot(new BailiShooter()); operateShot(new mengYaShooter());  Output:In the best shootingMongolia 犽 shootingCopy the code

At this point, you can easily extend, just inherit the shooter class, and reload the shooting method to achieve their own shooting method