OOP: encapsulation, inheritance and polymorphism

OOP refers to object-oriented programming, which is a programming style with classes as the basic unit of code organization. It can make the code more maintainable, reusable and extensible. There are three characteristics: encapsulation, inheritance and polymorphism.

1, Encapsulation

  • What is it: encapsulate attributes and expose only the necessary operations through a limited number of functions.
  • What to solve: in order to prevent data from being modified at will, resulting in uncontrollable attributes, affecting code maintainability and readability.
  • How to use: access the internal properties of the operation through the functions provided by the class.

Access control support is required: private, protected and public.

class Person
{
    private $name; // $name: private property (can only be accessed by its own class)
    protected $age; // $age: protected property (can only be accessed by its own class and its parent and child classes)
    public $sex; // $sex: public property (accessible anywhere)

    public function __construct($name, $age, $sex)
    {
        $this->name = $name;
        $this->age = $age;
        $this->sex = $sex;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        return $this->name = $name;
    }

    public function getAge()
    {
        return $this->age;
    }

}

$Person = new Person('gnahz', 22, 'male');
echo $Person->getName() ."\n"; // Output: gnahz (get name through getName method provided by Person class)
echo $Person->setName('zhang') ."\n"; // Output: zhang (to modify name, you can use the setName method provided by the class)
echo $Person->getAge() ."\n"; // Output: 22
// Echo $person - > name; / / if you cannot access private properties like this, an error will be reported: Cannot access private property Person::$name
echo $Person->sex ."\n"; // Output: male (public attributes can be accessed directly or modified at will)
echo $Person->sex = 'female'; // Output: female (can't hide information and protect data)

2, Inheritance

  • What is it: is-a relationship between classes (for example, cat is a mammal). Inheritance can be divided into single inheritance and multiple inheritance (for example, cat is a mammal as well as a reptile).

Single inheritance (a subclass can only inherit one parent class): Java, PHP

Multiple inheritance (a subclass can inherit multiple parents): Python, C++

  • What to solve: in order to be able to reuse code, suppose two classes have some same functions and properties, then they can be extracted to be a parent class for inheritance and use, to avoid code duplication.

The combination can also be realized. If the inheritance level is too deep and the inheritance relationship is too complex, the code readability and maintainability will be affected, so the combination is better.

  • How to use: Extensions (Java, PHP)
class Animal
{  
    public function run()
    {
        echo "I can run \n";
    }
}

// Cat class inherits animal class
class Cat extends Animal
{}

Animal::run(); // I can run
Cat::run(); // I can run

Three. Polymorphism

  • What is it: different states of a class in different situations, that is, different subclasses of the same class can have different operations when calling the same method.

  • What to solve: to improve the expansibility and reusability of code is the basis of many design patterns, design principles and programming skills.

// Interface implementation polymorphism

// Define a USB device interface
interface USB
{
    function run();
}

// Define a computer class to start the function of USB device
class Computer
{
    function useUSB($usb)
    {
        $usb->run();
    }
}

// Expand a USB keyboard setting to realize USB interface
class UsbKeyboard implements USB
{
    function run()
    {
        echo "UsbKeyboard is running \n";
    }
}

// Extend a USB mouse setting to realize USB interface
class UsbMouse implements USB
{
    function run()
    {
        echo "UsbMouse is running \n";
    }
}

$computer = new Computer();
$computer->useUSB(new UsbKeyboard()); // UsbKeyboard is running
$computer->useUSB(new UsbMouse()); // UsbMouse is running


// Abstract implementation of polymorphism
class Teacher
{
    public function drawPolygon(Polygon $polygon)
    {
        $polygon->draw();
    }
}

// Define an abstract class that can draw polygons
abstract class Polygon
{
    abstract protected function draw();
}

// Inherit polygon abstract class draw a rectangle
class Rectangle extends Polygon
{
    public function draw()
    {
        echo "draw a Rectangle \n";
    }
}

// Inherit polygon abstract class draw a square
class Square extends Polygon
{
    public function draw()
    {
        echo 'draw a Square';
    }
}

$teacher = new Teacher();
$teacher->drawPolygon(new Rectangle()); // draw a Rectangle
$teacher->drawPolygon(new Square()); // draw a Square

Keywords: Programming Java PHP Python

Added by alchemist_fr on Mon, 09 Mar 2020 09:36:14 +0200