cmc*_*loh 2 php oop extends interface
PHP中是否有一种方法可以覆盖扩展该接口的接口中的一个接口声明的方法?
这个例子:
我可能做错了什么,但这就是我所拥有的:
interface iVendor{
public function __construct($vendors_no = null);
public function getName();
public function getVendors_no();
public function getZip();
public function getCountryCode();
public function setName($name);
public function setVendors_no($vendors_no);
public function setZip($zip);
public function setCountryCode($countryCode);
}
interface iShipper extends iVendor{
public function __construct($vendors_no = null, $shipment = null);
public function getTransitTime($shipment = null);
public function getTransitCost($shipment = null);
public function getCurrentShipment();
public function setCurrentShipment($shipment);
public function getStatus($shipment = null);
}
Run Code Online (Sandbox Code Playgroud)
通常在PHP中,当你扩展某些东西时,你可以覆盖其中包含的任何方法(对吗?).但是,当一个界面延伸到另一个界面时,它不会让你.除非我正在考虑这个错误...当我实现iShipper接口时,我不必使Shipper对象扩展Vendor对象(实现iVendor接口).我只想说:
class FedEx implements iShipper{}
Run Code Online (Sandbox Code Playgroud)
并使FedEx实现iVendor和iShipper的所有方法.但是,我需要__construct
iVendor和iShipper中的功能是独一无二的.我知道我可以取出它$shipment = null
,但是创建Shippers并不方便(通过在实例化时传递vendors_no和货件).
有谁知道如何使这项工作?我的后备是$shipper->setShipment($shipment);
在我实例化之后通过调用Shipper 来设置货物,但我希望有办法解决这个问题......
对好奇的更多解释:
联邦快递对象有方法进入联邦快递网站(使用cURL)并获得有关货运的估计.我有一个UPS对象,一个BAXGlobal对象,一个Conway对象等.每个人都有完全不同的方法来实际获得运费估算,但所有系统需要知道的是他们是"托运人",并且列出的方法接口可以在它们上调用(因此它可以完全相同地对待它们,并在"托运人"阵列中循环通过它们,getTransitX()
以便为货物找到最佳的托运人).
每个"托运人"也是一个"供应商",并且在系统的其他部分也是如此(获取和放入数据库等等)我们的数据设计是一堆垃圾,因此联邦快递与像Dunder Mifflin在"Vendors"表中,这意味着它拥有所有其他Vendor的所有属性,但需要iShipper提供的额外属性和方法).
@cmcculloh是的,在Java中,您没有在Interfaces中定义构造函数.这允许您扩展接口并且还具有实现多个接口的类(在许多情况下允许并且非常有用),而不必担心必须满足特定构造函数.
编辑:
这是我的新模型:
答:每个接口不再具有构造函数方法.
B.所有托运人(UPS,FedEx等)现在实施iShipper(扩展iVendor)并扩展抽象类Shipper(其中包含所有常见的非抽象方法,包括在其中定义的托运人,getName(),getZip()等).
C.每个托运人都有自己独特的_construct方法,它覆盖托运人中包含的抽象__construct($ vendors_no = null,$ shipment = null)方法(我不记得为什么我现在允许这些方法是可选的.我'我必须通过我的文档回复...).
所以:
interface iVendor{
public function getName();
public function getVendors_no();
public function getZip();
public function getCountryCode();
public function setName($name);
public function setVendors_no($vendors_no);
public function setZip($zip);
public function setCountryCode($countryCode);
}
interface iShipper extends iVendor{
public function getTransitTime($shipment = null);
public function getTransitCost($shipment = null);
public function getCurrentShipment();
public function setCurrentShipment($shipment);
public function getStatus($shipment = null);
}
abstract class Shipper implements iShipper{
abstract public function __construct($vendors_no = null, $shipment = null);
//a bunch of non-abstract common methods...
}
class FedEx extends Shipper implements iShipper{
public function __construct($vendors_no = null, $shipment = null){
//a bunch of setup code...
}
//all my FedEx specific methods...
}
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!
PS.因为我现在已将此添加到"你的"答案,如果有什么关于它你不喜欢/认为应该是不同的,随意改变它...
归档时间: |
|
查看次数: |
2343 次 |
最近记录: |