PHP basic tutorial eleventh step object-oriented supplement

inherit

In the previous section, I learned some knowledge about the PHP class, class methods and class member variables, and then started to learn the inheritance in the PHP class. This section is a supplement to the previous section. (In fact, it is just to keep an article published more casually on the day)

Start

PHP inheritance means inheriting all the attributes of the parent class, just like you inherited most of your father's dnf. For example, Xiao Ming's father has red hair, black nose, blue eyes, and a cone-shaped head. This is also the case for Xiao Ming.
We look at the following examples to understand how to implement inheritance in PHP:

<?php
class Human {
    
    
  public $sex;
  public $hair;
  
  private function printInfo($val){
    
    
	  echo '传入的值是 '.$val.'<br/>';
  }
  public function set_Sex($val){
    
    
	  $this->printInfo($val);
	  $this->sex=$val;
  }
  public function set_Hair($val){
    
    
	  $this->hair=$val;
  }
  
  public function get_Sex(){
    
    
	  return $this->sex;
  }
  
  public function get_Hair(){
    
    
	  return $this->hair;
  }
}

class MutantHuman extends Human{
    
    }


$SuperMan=new MutantHuman();
$SuperMan->set_Sex('nan');
$SuperMan->set_Hair('y');

echo $SuperMan->get_Sex();
echo ' ';
echo $SuperMan->get_Hair();
?>

The above code example is modified from the code in the previous section, mainly a new class is created and inherited from the Human class:

class MutantHuman extends Human{
    
    }

In the above class, the class name is MutantHuman, and after the class name, the extends keyword is used. The extends keyword indicates inheritance, followed by the inherited class name Human. In other words, Human is its father, and Mutant Human has its father's methods and member variables. Then it means that MutantHuman can use set_Sex method and set_Hair, and has sex and hair member variables.
So after creating a new MutantHuman object SuperMan, you can use the set_Sex and set_Hair methods:

$SuperMan=new MutantHuman();
$SuperMan->set_Sex('nan');
$SuperMan->set_Hair('y');

echo $SuperMan->get_Sex();
echo ' ';
echo $SuperMan->get_Hair();

The results are as follows:
Insert picture description here

Method rewriting

When the subclass integrates the method of the parent class, you can override and modify the method, as shown in the following example:

<?php
class Human {
    
    
  public $sex;
  public $hair;
  
  private function printInfo($val){
    
    
	  echo '传入的值是 '.$val.'<br/>';
  }
  public function set_Sex($val){
    
    
	  $this->printInfo($val);
	  $this->sex=$val;
  }
  public function set_Hair($val){
    
    
	  $this->hair=$val;
  }
  
  public function get_Sex(){
    
    
	  return $this->sex;
  }
  
  public function get_Hair(){
    
    
	  return $this->hair;
  }
}

class MutantHuman extends Human{
    
    
	public function get_Sex(){
    
    
		echo ' 这是在 MutantHuman 中<br/>';
		return $this->sex;
	}
}


$SuperMan=new MutantHuman();
$SuperMan->set_Sex('nan');
$SuperMan->set_Hair('y');

echo $SuperMan->get_Sex();
echo ' ';
echo $SuperMan->get_Hair();
?>

In the above example, the subclass MutantHuman already has the method of the parent class, and you can directly override the method of the same name in the subclass to achieve different characteristics. After all, father and son are still different.
In the above example, the subclass MutantHuman has rewritten the get_Sex method. Rewriting is to rewrite a method with the same function form as the parent class, and modify the method to have different characteristics. In the above rewritten method, it is output inside echo ' 这是在 MutantHuman 中<br/>';. The results are as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/A757291228/article/details/107395250