架构师规范下面的工程师一个类中,必须使用几个方法,以及方法的命名必须使用我的命名.可以写一个接口类.
接口类中,只需写一个方法名空方法就可以.
interface User{
function getDiscount();
function getUserType();
}
vip用户类实现了用户接口类,必须把接口类中的方法,复制过来,写上方法体.
然后可以在vip类中.定义自己的折扣,判断用户类型,返回不同的折扣.
商品类中,定义商品价格,定义结算价格方法.(乘以折扣_->实际价格)
实例化,商品类,调用自己的结算方法,希望在商品类结算方法中,获取外部类vip类中的方法(判断用户类型,返回不同的折扣),再结算.
只有自己的对象.才能调用自己的方法.所以.必须把vip类实例化.作为结算方法的参数,传递进来.
但是传递进来的对象.无法直接使用.必须得赋值给内部的属性.才能调用自己的方法.
//接口类
interface User{
function getUserType();//获取用户类型.
function getDiscount();//获取折扣
}
class VipUser implements User{
private $discount = 0.8;// VIP 用户折扣系数
function getDiscount() {
return $this->discount;
}
function getUserType() {
return "VIP用户";
}
}
class Goods{
var $price = 100;
var $dd;
function run(User $a){ //限定了传递的参数必须是User类的子类(实现了User类的都算是其子类).不能传递其他类型.
$this->dd = $a;
$discount = $this->dd->getDiscount();
$usertype = $this->dd->getUserType();
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
echo $usertype."商品价格:".$this->price*$discount;
}
}
class AUser {//没有实现(User类),无法调用run()方法
private $discount = 0.9;// VIP 用户折扣系数
function getDiscount() {
return $this->discount;
}
function getUserType() {
return "VIP用户";
}
}
$display = new Goods();
$display ->run(new AUser());
接口类中,只需写一个方法名空方法就可以.
interface User{
function getDiscount();
function getUserType();
}
vip用户类实现了用户接口类,必须把接口类中的方法,复制过来,写上方法体.
然后可以在vip类中.定义自己的折扣,判断用户类型,返回不同的折扣.
商品类中,定义商品价格,定义结算价格方法.(乘以折扣_->实际价格)
实例化,商品类,调用自己的结算方法,希望在商品类结算方法中,获取外部类vip类中的方法(判断用户类型,返回不同的折扣),再结算.
只有自己的对象.才能调用自己的方法.所以.必须把vip类实例化.作为结算方法的参数,传递进来.
但是传递进来的对象.无法直接使用.必须得赋值给内部的属性.才能调用自己的方法.
//接口类
interface User{
function getUserType();//获取用户类型.
function getDiscount();//获取折扣
}
class VipUser implements User{
private $discount = 0.8;// VIP 用户折扣系数
function getDiscount() {
return $this->discount;
}
function getUserType() {
return "VIP用户";
}
}
class Goods{
var $price = 100;
var $dd;
function run(User $a){ //限定了传递的参数必须是User类的子类(实现了User类的都算是其子类).不能传递其他类型.
$this->dd = $a;
$discount = $this->dd->getDiscount();
$usertype = $this->dd->getUserType();
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
echo $usertype."商品价格:".$this->price*$discount;
}
}
class AUser {//没有实现(User类),无法调用run()方法
private $discount = 0.9;// VIP 用户折扣系数
function getDiscount() {
return $this->discount;
}
function getUserType() {
return "VIP用户";
}
}
$display = new Goods();
$display ->run(new AUser());