发新话题
打印

接口问题

接口问题

<?PHP
// How to hold objects in an arrayList and retrieve it //
// Interface implementation //
class Data {
   
   public $Name;
   public $Age;
   public $Address;
   
   public function __construct($Name,$Age,$Address) {//带参构造函数初始化数据
       $this->Name = $Name;
       $this->Age = $Age;
       $this->Address = $Address;
   }
   
   public function __destruct() {//析构函数,毁掉数据
       echo "Default Constructor...\n\r";echo"<br>";
   }
}

$ArrayList = array();//定义数组并在下面赋值
$ArrayList[0] = new Data("John McDonald","65","Address");
$ArrayList[1] = new Data("Turgut Z. Yesilyurt","30","NJ, USA");
$ArrayList[2] = new Data("Maria ","25","NJ, USA");

//print_r($ArrayList);//测试看下内容是不是自己写的

$obj1 = $ArrayList[1];//第其中每二个元素,按我理解就是把地址传给了$obj1
echo"<br>";echo "Name : ".$obj1->Name."\n";echo"<br>";
echo"<br>";echo "Age  : ".$obj1->Age."\n";echo"<br>";
echo"<br>";echo "Address : ".$obj1->Address."\n";echo"<br>";

// 在interface中只需声明方法头,而将方法体留给实现的class来做。这些实现的class的实例完全可以当作interface的实例来对待
interface ShowMe {
   public function ShowName();
}


//implements用于实现一个接口(interface)

class ShowData implements ShowMe {   
   public $obj;
public function __construct($Obj) {
   $this->obj = $Obj;
   $this->ShowName();
}
public function ShowName() {
   echo "Age  : ".$this->obj->Age."\n";
}
}

echo"<br>";
echo "\nDisplay Single Data =========================\n\n";echo"<br>";

new ShowData($ArrayList[1]);//这里呢什么意思?大概也许可能一定是把$ArrayList[1]做为ShowData类的一个实例

echo"<br>";echo "\n=============================================\n";echo"<br>";
?>


预期输出:
// Turgut Z. YESILYURT, MS
// Software Developer //
// NJ, USA  //
Display Single Data =========================
Age : 30
=============================================
Default Constructor...

但实际上输出的却不是,呵呵呵!哪错了?还是没有错 自己验证下吧 不同意见贴在下面喽,剩得我这贴子为害人民
学会怎么调试程序,学会怎么设计模式,学会怎么配置环境,学会怎么带项目,学会怎么分析客户需求

TOP

发新话题