php基础--取默认值以及类的继承
(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(); ?>
以上输出结果为:

php基础--取默认值以及类的继承的更多相关文章
- php取默认值以及类的继承
(1)对于php的默认值的使用和C++有点类似,都是在函数的输入中填写默认值,以下是php方法中对于默认值的应用: <?phpfunction makecoffee($types = array ...
- Java 基础类型 默认值
(1)数据库里的列,如果有默认值,不能赋值有业务含义的值. (2)int 默认值 java会分配默认值的额.
- 8. react 基础 - props 默认值和类型限制 与 Props , State , render 函数 关系
一. PropTypes 与 DefaultProps 官方文档 1. PropTypes 属性校验 引入 PropTypes import PropTypes from 'prop-types'; ...
- PythonStudy——函数默认值
# 如果函数的默认参数的默认值为变量,在所属函数定义阶段一执行就被确定为当时变量存放的值 a = 100 def fn(num=a): a = 200 fn() 输出: 100 也就是说在函数调用的时 ...
- 【译】python configparser中默认值的设定
在做某一个项目时,在读配置文件中,当出现配置文件中没有对应项目时,如果要设置默认值,以前的做法是如下的: try: apple = config.get(section, 'apple') excep ...
- java的基本数据类型默认值
这里就举int类型 默认值在类实例化,也就是对象中才有默认值0,或者是静态变量. 1.先看局部变量使用(不行,报错) 2.静态变量 3.类非静态属性
- [原创] Shell 参数传递 与 默认值
目录 简介 基本传参 $* 与 $@ 区别 默认参数(变量默认值) if 繁琐方式 - 变量为null = 变量为null时, 同时改变变量值 :- 变量为null 或 空字符串 := 变量为null ...
- Mysql数据表字段设置了默认值,插入数据后默认字段的值却为null,不是默认值
我将mysql的数据表的某个字段设置了默认值为1,当向该表插入数据的时候该字段的值不是默认值,而是null. 我的错误原因: 对数据库的操作我使用了持久化工具mybatis,插入数据的时候插入的是整个 ...
- ES6特性之:参数默认值
作为一个开发者,跟进行业步伐是非常需要的,不能躺在现有的知识和经验温床上做美梦.JavaScript的ES2015标准(即我们说的ES6)在2016年已经被广泛应用了,还没开始使用的朋友,赶紧来磨一下 ...
随机推荐
- (转)Delphi7中QuickReport组件(QReport报表)安装方法及重要属性
Delphi7中没有办法直接使用QuickReport组件,因为在Delphi7中没有将QuickReport组件包作为默认组件打包,如果要使用此组件,需要先安装一下. 打开delphi7,点 ...
- C# 委托的三种调用示例(同步调用、异步调用、异步回调)
首先,通过代码定义一个委托和下面三个示例将要调用的方法: 代码如下: public delegate int AddHandler(int a,int b); public class 加法类 { p ...
- 2、开始学习C++
1.创建C++程序 C++程序是严格区分大小写,Cout与COUT都是识别不出来的,下面给出了一段简单的C++程序. #include "stdafx.h" #include &l ...
- 移动端适配1px问题
(function(document) { var dcl = document.documentElement, wh; var scale = 1/window.devicePixelRatio; ...
- asp web服务
项目名--->添加--->web引用 网址:http://www.webxml.com.cn/zh_cn/index.aspx https://blog.csdn.net/linshich ...
- Android WebView的使用(用来显示网页)
1.WebView介绍 2.URL介绍 简单说就是网址. 3.java后台代码 package com.lucky.test34webview; import android.support.v7.a ...
- python之常用的数据类型
1. 变量的定义以及声明 在学习变量之前,咱们需要知道变量的命名规则: ① 变量必须由数字字母下划线构成,如a_1 ② 变量名不能以数字开头,如1a ③ 需要遵循驼峰命名法 给变量赋值通常采用“=”, ...
- Celery 大量任务 分发
Celery是由Python开发的一个简单.灵活.可靠的处理大量任务的分发系统,它不仅支持实时处理也支持任务调度. user:用户程序,用于告知celery去执行一个任务. broker: 存放任务( ...
- 磁盘挂载 fdisk
fdisk -l 查看是否 已加入磁盘 fdisk /dev/sdb fdisk -cu /dev/sdb 取消兼容模式 添加一个硬盘,只创建一个分区,然后把分区挂载到/data 第 ...
- Hash算法总结
1. Hash是什么,它的作用 先举个例子.我们每个活在世上的人,为了能够参与各种社会活动,都需要一个用于识别自己的标志.也许你觉得名字或是身份证就足以代表你这个人,但是这种代表性非常脆弱,因为重名的 ...