CStack

优质
小牛编辑
127浏览
2023-12-01
所有包 | 属性 | 方法
system.collections
继承class CStack » CComponent
实现IteratorAggregate, Traversable, Countable
源自1.0
版本$Id: CStack.php 3427 2011-10-25 00:03:52Z alexander.makarow $
源码framework/collections/CStack.php
CStack实现一个栈。

这个类已经实现了一些典型的栈的操作,包括 push(),pop()和peek()。另外, contains()可以用来检查项目是否存在 于栈中。通过Count属性,可以取得 列表中的项目数。

栈中的项目可以像下面那样使用foreach遍历,
foreach($stack as $item) ...

公共属性

隐藏继承属性

属性类型描述定义在
countinteger返回栈中的项目数。CStack
iteratorIterator返回遍历这个栈的项目的迭代器。CStack

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__construct()构造方法。CStack
__get()返回一个属性值、一个事件处理程序列表或一个行为名称。CComponent
__isset()检查一个属性是否为null。CComponent
__set()设置一个组件的属性值。CComponent
__unset()设置一个组件的属性为null。CComponent
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
clear()删除栈中所有项目。CStack
contains()CStack
copyFrom()将迭代器中的数据复制到栈。CStack
count()返回栈中的项目数。CStack
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
getCount()返回栈中的项目数。CStack
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getIterator()返回遍历这个栈的项目的迭代器。CStack
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasProperty()确定属性是否被定义。CComponent
peek()返回处于栈头部的项目。CStack
pop()取出处于栈头部的项目。CStack
push()添加一个对象到栈里面。CStack
raiseEvent()发起一个事件。CComponent
toArray()CStack

属性详细

count 属性 只读 public integer getCount()

返回栈中的项目数。

iterator 属性 只读 public Iterator getIterator()

返回遍历这个栈的项目的迭代器。 此方法为接口IteratorAggregate强制要求实现。

方法详细

__construct() 方法
public void __construct(array $data=NULL)
$dataarray初始化的数据。默认为null,意味着不会初始化。
源码: framework/collections/CStack.php#52 (显示) publicfunction__construct($data=null)
{
if($data!==null)
$this->copyFrom($data);
}

构造方法。 根据数组或者迭代对象初始化这个栈。

clear() 方法
public void clear()
源码: framework/collections/CStack.php#90 (显示) publicfunctionclear()
{
$this->_c=0;
$this->_d=array();
}

删除栈中所有项目。

contains() 方法
public boolean contains(mixed $item)
$itemmixed项目对象
{return}boolean返回值说明栈中是否包含该项目
源码: framework/collections/CStack.php#100 (显示) publicfunctioncontains($item)
{
returnarray_search($item,$this->_d,true)!==false;
}
copyFrom() 方法
public void copyFrom(mixed $data)
$datamixed要复制的数据, 只能是数组或者继承于Traversable的对象。
源码: framework/collections/CStack.php#72 (显示) publicfunctioncopyFrom($data)
{
if(is_array($data)||($datainstanceofTraversable))
{
$this->clear();
foreach($dataas$item)
{
$this->_d[]=$item;
++$this->_c;
}
}
elseif($data!==null)
thrownewCException(Yii::t('yii','StackdatamustbeanarrayoranobjectimplementingTraversable.'));
}

将迭代器中的数据复制到栈。 注意,栈中已经存在的数据会被首先删除。

count() 方法
public integer count()
{return}integer返回栈中的项目数。
源码: framework/collections/CStack.php#169 (显示) publicfunctioncount()
{
return$this->getCount();
}

返回栈中的项目数。 此方法为接口Countable强制要求实现。

getCount() 方法
public integer getCount()
{return}integer返回栈中的项目数。
源码: framework/collections/CStack.php#159 (显示) publicfunctiongetCount()
{
return$this->_c;
}

返回栈中的项目数。

getIterator() 方法
public Iterator getIterator()
{return}Iterator返回遍历这个栈的项目的迭代器。
源码: framework/collections/CStack.php#150 (显示) publicfunctiongetIterator()
{
returnnewCStackIterator($this->_d);
}

返回遍历这个栈的项目的迭代器。 此方法为接口IteratorAggregate强制要求实现。

peek() 方法
public mixed peek()
{return}mixed返回处于栈头部的项目。
源码: framework/collections/CStack.php#111 (显示) publicfunctionpeek()
{
if($this->_c)
return$this->_d[$this->_c-1];
else
thrownewCException(Yii::t('yii','Thestackisempty.'));
}

返回处于栈头部的项目。 跟pop()不同,这个方法不会删除栈的项目。

pop() 方法
public mixed pop()
{return}mixed返回处于栈头部的项目。
源码: framework/collections/CStack.php#124 (显示) publicfunctionpop()
{
if($this->_c)
{
--$this->_c;
returnarray_pop($this->_d);
}
else
thrownewCException(Yii::t('yii','Thestackisempty.'));
}

取出处于栈头部的项目。

push() 方法
public void push(mixed $item)
$itemmixed要添加到栈里面的对象。
源码: framework/collections/CStack.php#139 (显示) publicfunctionpush($item)
{
++$this->_c;
array_push($this->_d,$item);
}

添加一个对象到栈里面。

toArray() 方法
public 栈中的项目列表。 toArray()
{return}栈中的项目列表。
源码: framework/collections/CStack.php#61 (显示) publicfunctiontoArray()
{
return$this->_d;
}