今天我正在阅读设计模式,我尝试制作一个示例程序,它由一个接口、两个实现该接口的类和一个主索引类组成。让我们看看下面给出的代码。首先是接口Iproduct
<?php
interface Iproduct
{
//Define the abstract method
public function apple();
public function mango();
}
实现接口的两个类
<?php
// Including the interface
include_once 'Iproduct.php';
class Apple implements Iproduct
{
public function apple()
{
echo ("We sell apples!");
}
public function mango()
{
echo ("We do not sell Mango!");
}
}
<?php
// Include the interface Iprodduct
include_once 'Iproduct.php';
class Mango implements Iproduct
{
public function apple()
{
echo ("We do not sell Apple");
}
public function mango()
{
echo ("We sell mango!");
}
}
现在是主要课程
<?php
include_once ('apple.php');
include_once ('Mango.php');
class UserProduct
{
public function __construct()
{
$apple_class_obj=new Apple();
$mango_class_obj=new Mango();
//echo("<br/> the apple class object: ".$apple_class_obj);
}
}
//creating the object of the UserProduct
echo ("creating the object!<br/>");
$userproduct_obj=new UserProduct();
?>
我执行代码时得到的输出是:
creating the object!
we sell apples!we sell mango
现在的问题是我无法知道第二个输出如何,即我们卖苹果!我们卖芒果!正在显示。请告诉我原因