首页 技术 正文
技术 2022年11月6日
0 收藏 747 点赞 808 浏览 1601 个字

(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"); ?>

以上程序的输出为:

php取默认值以及类的继承

当输入为空时,当前的值采用默认的值,当不为空时,采用输入值;

(2)以下是PHP中对于class中默认值的使用:

<?phpclass 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();?>

输出结果为:

php取默认值以及类的继承

(三)继承取默认值:

(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值, 对于自身没有的方法,继承父类的方法;

以上的输出结果为:

php取默认值以及类的继承

(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();?>

以上输出结果为:

php取默认值以及类的继承

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900