当前位置:网站首页>[PHP] PHP polymorphism
[PHP] PHP polymorphism
2022-06-22 23:46:00 【weixin_ forty-three million two hundred and twenty-four thousan】
What does polymorphism mean : When objects of the same class receive the same message , We'll get different results . And the news is unpredictable . polymorphic , seeing the name of a thing one thinks of its function , It's a variety of states , That is, a variety of results .
example 1
<?php
class employee{
protected function working(){
echo ' This method requires overloading ';
}
}
class teacher extends employee{
public function working(){
echo ' Teach ';
}
}
class coder extends employee{
public function working(){
echo ' Write code ';
}
}
function doprint($obj){
if(get_class($obj) == 'employee'){
echo 'Error';
}else{
$obj->working();
}
}
doprint(new teacher());
doprint(new coder());
doprint(new employee());
example 2
<?php
interface employee{
public function working();
}
class teacher implements employee{
public function working(){
echo ' Teach ';
}
}
class coder implements employee{
public function working(){
echo ' Write code ';
}
}
function doprint(employee $i){
$i->working();
}
$a = new teacher;
$b = new coder;
doprint($a);
doprint($b);
example 2 And example 1 It doesn't make much difference , But in the code doprint The parameter of the function is a variable of interface type , accord with “ The same type , Different results ” This condition , A general characteristic of polymorphism , So it's polymorphism
example 1 If you put doprint Functional obj Parameters are treated as a type ( Think of all weak types as one type ), That can also be considered polymorphic , Otherwise, it's not
The key to distinguishing whether an object is polymorphic is whether the object belongs to the same type
php In, the parent and child classes are treated as “ Stepfather ” and “ Stepson ” Relationship , They are inherited , But there is no blood relationship . Therefore, a child class cannot be transformed upward into a parent class , Thus losing the most typical characteristic of polymorphism
边栏推荐
猜你喜欢

OJ daily practice - spanning 2020

Is it difficult to turn weak current into professional network worker? Huawei pre-sales engineers share their own experience

Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading

Redis缓存

Digital data was invited to participate in Nantong enterprise digital transformation Seminar

OLAP ——Druid简介

Programmers' choice of taking private jobs and part-time jobs

DCC888 :SSA (static single assignment form)

Do domestic mobile phones turn apples? It turned out that it was realized by 100 yuan machine and sharp price reduction

What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?
随机推荐
[kubernetes series] overview of kubernetes
Asynchronous FIFO
Tianyi cloud takes advantage of the new infrastructure to build a "4+2" capability system for digital transformation
同步电路与跨时钟域电路设计2——多bit信号的跨时钟域传输(FIFO)
Unity: use ray to detect objects
OLAP ——Druid简介
【GO】Go数组和切片(动态数组)
Anti shake & throttling enhanced version
好东西要分享啦
Webrtc series - 4connection sorting of network transmission
DCC888 :SSA (static single assignment form)
JSBridge
Dip1000,1 of D
Autoincrement attribute of sqlserver replication table
LeetCode_ Backtracking_ Dynamic programming_ Medium_ 131. split palindrome string
Customize multi-level list styles in word
昆仑分布式数据库独特的变量读写功能介绍
考过HCIP依然转行失败,职业网工最看重的到底是什么
tp5.1上传excel文件并读取其内容
three.js模拟驾驶游览艺术展厅---打造超级相机控制器