php类学习 - Extends(扩展)
时间:2007-07-06 来源:windlike
class Counter{
var $count = 0;
var $startPoint = 0;
//method
function startCountAt($i){
$this->count = $i;
$this->startPoint = $i;
}
function increment(){
$this->count++;
}
function reset(){
$this->count = $this->startPoint;
}
function showValue(){
echo $this->count;
}
}
class BottleCounter extends Counter
{
function addCase()
{
$this->count += 12;
}
function caseCount()
{
return ceil($this->count / 12);
}
function BottleCounter($startCount)
{
$this->count = $startCount;
}
}
class CanCounter extends BottleCounter
{
function volumeDiscount($var)
{
if($var->count > 24)
{
return 0.95;
}else{
return 1.0;
}
}
}
/*
$count = new Counter;
$count->startCountAt(10);
echo $count->count;
$count->increment();
echo $count->count;
$count->showValue();
$count->reset();
$count->showValue();
*/
$bottles = new BottleCounter(10);
$cans = new CanCounter(25);
echo $bottleDiscountFactor = volumeDiscount($bottles);
echo $canDiscountFactor = volumeDiscount($cans);
var $count = 0;
var $startPoint = 0;
//method
function startCountAt($i){
$this->count = $i;
$this->startPoint = $i;
}
function increment(){
$this->count++;
}
function reset(){
$this->count = $this->startPoint;
}
function showValue(){
echo $this->count;
}
}
class BottleCounter extends Counter
{
function addCase()
{
$this->count += 12;
}
function caseCount()
{
return ceil($this->count / 12);
}
function BottleCounter($startCount)
{
$this->count = $startCount;
}
}
class CanCounter extends BottleCounter
{
function volumeDiscount($var)
{
if($var->count > 24)
{
return 0.95;
}else{
return 1.0;
}
}
}
/*
$count = new Counter;
$count->startCountAt(10);
echo $count->count;
$count->increment();
echo $count->count;
$count->showValue();
$count->reset();
$count->showValue();
*/
$bottles = new BottleCounter(10);
$cans = new CanCounter(25);
echo $bottleDiscountFactor = volumeDiscount($bottles);
echo $canDiscountFactor = volumeDiscount($cans);
相关阅读 更多 +
排行榜 更多 +