(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基础--取默认值以及类的继承的更多相关文章

  1. php取默认值以及类的继承

    (1)对于php的默认值的使用和C++有点类似,都是在函数的输入中填写默认值,以下是php方法中对于默认值的应用: <?phpfunction makecoffee($types = array ...

  2. Java 基础类型 默认值

    (1)数据库里的列,如果有默认值,不能赋值有业务含义的值. (2)int 默认值 java会分配默认值的额.

  3. 8. react 基础 - props 默认值和类型限制 与 Props , State , render 函数 关系

    一. PropTypes 与 DefaultProps 官方文档 1. PropTypes 属性校验 引入 PropTypes import PropTypes from 'prop-types'; ...

  4. PythonStudy——函数默认值

    # 如果函数的默认参数的默认值为变量,在所属函数定义阶段一执行就被确定为当时变量存放的值 a = 100 def fn(num=a): a = 200 fn() 输出: 100 也就是说在函数调用的时 ...

  5. 【译】python configparser中默认值的设定

    在做某一个项目时,在读配置文件中,当出现配置文件中没有对应项目时,如果要设置默认值,以前的做法是如下的: try: apple = config.get(section, 'apple') excep ...

  6. java的基本数据类型默认值

    这里就举int类型 默认值在类实例化,也就是对象中才有默认值0,或者是静态变量. 1.先看局部变量使用(不行,报错) 2.静态变量 3.类非静态属性

  7. [原创] Shell 参数传递 与 默认值

    目录 简介 基本传参 $* 与 $@ 区别 默认参数(变量默认值) if 繁琐方式 - 变量为null = 变量为null时, 同时改变变量值 :- 变量为null 或 空字符串 := 变量为null ...

  8. Mysql数据表字段设置了默认值,插入数据后默认字段的值却为null,不是默认值

    我将mysql的数据表的某个字段设置了默认值为1,当向该表插入数据的时候该字段的值不是默认值,而是null. 我的错误原因: 对数据库的操作我使用了持久化工具mybatis,插入数据的时候插入的是整个 ...

  9. ES6特性之:参数默认值

    作为一个开发者,跟进行业步伐是非常需要的,不能躺在现有的知识和经验温床上做美梦.JavaScript的ES2015标准(即我们说的ES6)在2016年已经被广泛应用了,还没开始使用的朋友,赶紧来磨一下 ...

随机推荐

  1. 什么是LINQ

    LINQ 什么是LINQLINQ提供程序 匿名类型 方法语法和查询语法查询变量查询表达式的结构 from子句join子句什么是联结查询主体中的from…let…where片段 from子句let子句w ...

  2. 理解Javascript_02_执行上下文01

    执行上下文又名执行上下文环境 JS中为什么会产生这个概念呢,先来看一下下面的这段代码: 通过执行发现,第一句代码报了ReferenceError,第二句和第三句代码是undefined,由于undef ...

  3. 51Nod-1259-整数划分 V2

    51Nod-1259-整数划分 V2 将N分为若干个整数的和,有多少种不同的划分方式,例如:n = 4,{4} {1,3} {2,2} {1,1,2} {1,1,1,1},共5种.由于数据较大,输出M ...

  4. hdu4513吉哥系列故事——完美队形II 马拉车

    题目传送门 题意:求最长回文串长度,要求回文串左边是非下降. 思路一: 先把连续的回文串,满足先上升再下降的序列处理出来,再对这部分序列做马拉车模板就可以了. 需要注意的是,由于他要的是非下降的序列, ...

  5. [转] 在body中没有元素把高度撑开的情况下,设置全屏

    [From] https://segmentfault.com/q/1010000006182839 html,body { margin:; padding:; min-height: 100vh; ...

  6. 微信小程序中如何使用setData修改数组或对象中的某一参数

    本人也是刚开始接触微信小程序,在微信小程序中经常会遇到修改数组中某一项的值,比如array[0]或者是对象中object.item的值.这些值在微信小程序中都需要使用一个名为setData的方法,而这 ...

  7. Xtts v4 xttdriver.pl & xtt.properties

    Perl脚本xttdriver.pl选项的描述  下表描述了主要支持脚本xttdriver.pl的可用选项. 级备份.这些备份将写入xtt.properties变量"backupformat ...

  8. oracle 日志文件

    --Oracel Grid 11.2的Agent有多个,其中有两个最重要:orarootagent.oraagent --它们有各自的日志文件,这些Agent的日志文件位于: $grid_home/l ...

  9. 全网备份脚本rsync

    一,服务端配置 #!/bin/sh ######################################################### #by:kingle # #use: confi ...

  10. GreenPlum 大数据平台--介绍

    一,GreenPlum 01,介绍: Greenplum是一种基于PostgreSQL的分布式数据库,其采用shared-nothing架构,主机.操作系统.内存.存储都是自我控制的,不存在共享. 官 ...