php基础--取默认值以及类的继承
2024-08-30 14:58:49
(1)对于php的默认值的使用和C++有点类似,都是在函数的输入中填写默认值,以下是php方法中对于默认值的应用:
<?php
function makecoffee($types = array("cappuccino"), $coffeeMaker = NULL)
{
$device = is_null($coffeeMaker) ? "hands" : $coffeeMaker;
return " </br> Making a cup of ".join(", ", $types)." with $device.\n";
}
echo makecoffee();
echo makecoffee(array("cappuccino", "lavazza"), "teapot"); function makeyogurt($flavour, $type = "acidophilus")
{
return "</br>Making a bowl of $type $flavour.\n";
}
echo makeyogurt("raspberry"); ?>
以上程序的输出为:
当输入为空时,当前的值采用默认的值,当不为空时,采用输入值;
(2)以下是PHP中对于class中默认值的使用:
<?php class pot{ protected $x;
protected $y;
protected $c;
public function __construct($x = 0, $y=1){ $this->x = $x;
$this->y = $y;
$this->c = $this->x+$this->y;
} function printC()
{
echo "</br>".$this->c;
}
} $pot1 = new pot(9, 6);
$pot1->printC();
$pot2 = new pot();
$pot2->printC(); ?>
输出结果为:
(三)继承取默认值:
(1)当继承的class有construct的情况:
<?php
class pot{ protected $x;
protected $y;
protected $c;
public function __construct($x = 0, $y=1){ $this->x = $x;
$this->y = $y; $this->c = $this->x+$this->y;
} function printC()
{
echo "</br>".$this->c;
}
} class pots extends pot{
protected $x;
protected $y;
protected $c;
public function __construct($x = 10, $y = 20){ $this->x = $x;
$this->y = $y;
$this->c = $this->x+$this->y;
}
}
$pots1 = new pots(10, 5);
$pots1->printC();
$pots2 = new pots();
$pots2->printC(); ?>
当继承的class有自己的construct,那它的取值为自身的construct的取值, 对于自身的construct没有默认值,则默认取0值, 对于自身没有的方法,继承父类的方法;
以上的输出结果为:
(2)当继承的class没有construct的情况,取父类的值:
<?php
class pot{ protected $x;
protected $y;
protected $c;
public function __construct($x = 0, $y=1){ $this->x = $x;
$this->y = $y; $this->c = $this->x+$this->y;
} function printC()
{
echo "</br>".$this->c;
}
} class pots extends pot{ }
$pots1 = new pots(10, 5);
$pots1->printC();
$pots2 = new pots();
$pots2->printC(); ?>
以上输出结果为:
最新文章
- RPC框架实现 - 通信协议篇
- 全面理解Git
- 如何用ORM支持SQL语句的CASE WHEN?
- 【循序渐进学Python】15.网络编程
- sybase常用SQL语句,工作中积累的
- thinkphp框架的相关总结
- Web静态和动态项目委托代理基于面向方面编程AOP
- aync await 进一步探索
- PHP 面试知识点整理归纳
- 安装tftp
- vs2008将 win32项目改为console项目
- MySQL8.0.15安装教程(Windows)
- MapReduce实例学习
- Js/Bind()的认识
- ImageLorderUtil
- 先加载js 后载控件
- java去除数组中重复的元素方法总结
- Activiti 5.1.4最佳实践
- 关于SDWebImage加载高清图片导致app崩溃的问题
- redis3.2装完后 其它机子访问爆protocol error, got &#39;n&#39; as reply type byte
热门文章
- css3之animation制作闪烁文字效果 转
- Hadoop Hive概念学习系列之什么是Hive?
- selenium定位不到元素
- LeetCode109. 有序链表转换二叉搜索树
- 洛谷 P3233 [HNOI2014]世界树(虚树+dp)
- 蓝桥-青蛙跳杯子(bfs)
- 在linux服务器上配置anaconda和Tensorflow,并运行
- #537 (Div. 2) Creative Snap (思维+dfs)
- HDU 6336 (规律 + 二维矩阵的前缀和妙用)
- UVALive - 3942 左儿子trie DP