面向对象问题 输出没结果

ccav5 发布于 2012/07/08 22:15
阅读 181
收藏 0
PHP
name = $name; } abstract function fire(); } class NastyBoss{ //员工数组 private $employees = array(); function addEmployee( Employee $employee ){ $employees[] = $employee; // print_r($employees); } function projectFails(){ // print_r($this->employees); if(count($this->employees) ){ $emp = array_pop($this->employees); $emp->fire(); } } } class wellconnected extends Employee{ function fire(){ print "{$this->name} :I'll call my layer"; } } class minion extends Employee{ function fire(){ print "{$this->name} :I'll call my layer"; } } class cluedup extends Employee{ function fire(){ print "{$this->name} :I'll call my layer"; } } $boss = new NastyBoss(); $boss->addEmployee(Employee::recruit("hary")); $boss->addEmployee(Employee::recruit("pot")); //var_dump($boss); $boss->projectFails(); ?> 为什么 project 方法中 count的结果为0 而 addEmplyee中的 emplyees数组有打印出来

以下是问题补充:

@ccav5:我在回答里重新 附上代码了 (2012/07/08 22:37)
加载中
0
deleted
deleted
22行$employees改成$this->employees

28行if改成while

0
中山野鬼
中山野鬼
我写出这种代码,也一样会错的。放心,输出没结果这个很正常,也很正确。这样写代码,输出有结果,才叫不正常。
0
leo108
leo108
你这样问问题,没有人帮你解决很正常
ccav5
ccav5
不知道怎么换个格式
0
ExtremeTalk
ExtremeTalk
问问题不认真,回答的人又怎么会认真呢?
0
ccav5
ccav5
<?php
abstract class Employee{
	protected $name;
	private static $types = array('minion','cluedup','wellconnected');
	
	static function recruit( $name ){
		$num = rand(1,count(self::$types))-1;
		$class = self::$types[$num];
		return new $class($name); 
	}
	public function __construct( $name ){
		$this->name = $name;
	}
	abstract function fire();
}

class NastyBoss{
	//员工数组
	private $employees = array();
	
	function addEmployee( Employee $employee ){
		$employees[] = $employee;
//		print_r($employees);
	}
	
	function projectFails(){
//		print_r($this->employees);
		if(count($this->employees) ){
			$emp = array_pop($this->employees);
			$emp->fire();
		}
	}
}

class wellconnected extends Employee{
	function fire(){
		print "{$this->name} :I'll call my layer";
		
	}
}
class minion extends Employee{
	function fire(){
		print "{$this->name} :I'll call my layer";
	}
}
class cluedup extends Employee{
	function fire(){
		print "{$this->name} :I'll call my layer";
	}
}

$boss = new NastyBoss();

$boss->addEmployee(Employee::recruit("hary"));
$boss->addEmployee(Employee::recruit("pot"));
//var_dump($boss);

$boss->projectFails();
?>
新手第一次问问题,不好意思

0
ccav5
ccav5
每日10贴,被我发完了,哎,不能重发了
0
中山野鬼
中山野鬼

引用来自“code_monkey”的答案

每日10贴,被我发完了,哎,不能重发了
见过一日10贴的,没见过你这种一帖10次的。哈。。。。。。快笑喷了。。。别介意,OSC就是个相互嘲笑中学习的地方。
ccav5
ccav5
没事,学习最重要
0
ccav5
ccav5

引用来自“中山野鬼”的答案

引用来自“code_monkey”的答案

每日10贴,被我发完了,哎,不能重发了
见过一日10贴的,没见过你这种一帖10次的。哈。。。。。。快笑喷了。。。别介意,OSC就是个相互嘲笑中学习的地方。

可能是网速不行 ,反应过来,已经发完了

0
ccav5
ccav5

引用来自“勇者天空”的答案

22行$employees改成$this->employees

28行if改成while

恩 对了 谢谢
中山野鬼
中山野鬼
记得给@勇者天空 标记最佳答案。怎么说别人也眼花花的帮你查错呢。
返回顶部
顶部