当前位置:网站首页>[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
边栏推荐
猜你喜欢

KunlunDB查询优化(二)Project和Filter下推

c# sqlsugar,hisql,freesql orm框架全方位性能测试对比 sqlserver 性能测试

Smart data won two annual awards at the second isig China Industrial Intelligence Conference

Reverse proxy haproxy

昆仑分布式数据库Sequence功能及其实现机制

KunlunDB查询优化(三)排序下推

Redis缓存

DCC888 :SSA (static single assignment form)

Enterprise digitalization is not a separate development, but a comprehensive SaaS promotion

Flutter outsourcing, undertaking flutter project
随机推荐
多种方案实现图片的懒加载
OJ daily practice - find the first character that only appears once
'dare not doubt the code, but have to doubt the code 'a network request timeout analysis
China Mobile's mobile phone users grow slowly, but strive for high profit 5g package users
同步电路与跨时钟域电路设计2——多bit信号的跨时钟域传输(FIFO)
Array and string offset access syntax with curly braces is no longer support
flink同步mysql数据到ES
Learning the interpretable representation of quantum entanglement, the depth generation model can be directly applied to other physical systems
Future alternatives to IPv4! Read the advantages, features and address types of IPv6
IPV4的未来替代品!一文读懂IPV6的优势特点和地址类型
Redis缓存
14. 最长公共前缀
昆仑分布式数据库Sequence功能及其实现机制
程序员接私活兼职选择
Various schemes for lazy loading of pictures
13. Roman numeral to integer
C sqlsugar, hisql, FreeSQL ORM framework all-round performance test vs. sqlserver performance test
KunlunDB查询优化(二)Project和Filter下推
canvas生成海报
Canvas generate Poster