php——27-抽象类

抽象类与抽象方法

  • PHP5开始支持抽象类和抽象方法,用abstract修饰的类表示这个类是一个抽象类,用abstract修饰的方法表示这个方法是一个抽象方法。
  • 抽象类不能被实例化。
  • 抽象方法只能声明,不能定义其具体的功能实现。
  • 任何一个类,如果它在里面至少有一个方法是被声明为抽象的,那么这个类就必须被声明为抽象类。

抽象类的继承

  • 抽象类继承另外一个抽象类时,不用重写其中的抽象方法。
  • 抽象类中,不能重写抽象父类的抽象方法。这样的用法,可以理解为对抽象类的扩展。
  • 继承抽象类的非抽象类必须实现抽象类中的所有抽象方法
abstract class Test
{
    //abstract function t(){};//报错,只能声明,不能定义其具体的功能
    abstract function t();

    abstract function t1();
}

abstract class Test0 extends Test
{
    abstract function t2();
}

class Text1 extends Test0
{
    //abstract function t();//报错,不能重写
    function t()//不可少,继承抽象类的非抽象类必须实现抽象类中的所有抽象方法
    {
        // TODO: Implement t() method.
        echo '我是 Test 中的 Text1 的 t 方法'.'<br>';
    }

    function t1()//不可少,理由同上
    {
        // TODO: Implement t1() method.
    }

    function t2()//继承自Test0抽象类的抽象方法,也不可少
    {
    }

    function t3()//新定义的
    {
    }
}

$cc = new Text1();//我是 Test 中的 Text1 的 t 方法
$cc->t();

设计模式之模板模式

  • 在一个类中定义一个算法骨架,而将一些步骤延迟到子类中。
  • 模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤
  • 业务场景:
    • 银行计算利息(interest)的时候都是以本金(principal)乘以利率(interest rate)得到。利率对于每个银行都不一样,而且对于不同的存款方式和本金利率又不同。所以在账户这个类的相关方法里,我们只定义算法骨架,但不具体实现。实现由各个银行的子类来完成。
abstract class Account
{
    protected $principal = 1000;
    protected $interest;

    /**
     * 获取利息
     * @return float|int
     */
    function getInterest()
    {
        //利息=利率*本金
        $this->interest = $this->principal * $this->getInterestRate();
        return $this->interest;
    }

    /**
     * 获取本金
     * @return mixed
     */
    public function getPrincipal()
    {
        return $this->principal;
    }

    /**
     * 获取银行利率
     * @return mixed
     */
    abstract function getInterestRate();
}

class ABC extends Account{
    function getInterestRate(){
        // TODO: Implement getInterestRate() method.
        echo '我是农行的利率计算方式'.'<br>';
        return 0.5;
    }
}

class ICBC extends Account{
    function  getInterestRate()
    {
        // TODO: Implement getInterestRate() method.
        echo '我是工商银行的利率计算方式'.'<br>';
        return 0.6;
    }
}

$cc=new ABC();
echo $cc->getInterest();//我是农行的利率计算方式  500

$dd=new ICBC();
echo $dd->getInterest();//我是工商银行的利率计算方式  600
  • 不同国家欢迎介绍
abstract class Welcome
{
    protected $words = 'hello! I am from ';

    function sayWords()//拼接欢迎语
    {
        return $this->words . $this->getCountry().'<br>';
    }

    abstract function getCountry();//获取不同国家
}

class China extends Welcome{
    function getCountry()
    {
        // TODO: Implement getCountry() method.
        return 'China';
    }
}
class American extends Welcome{
    function getCountry()
    {
        // TODO: Implement getCountry() method.
        return 'American';
    }
}

$cc=new China();
echo $cc->sayWords();//hello! I am from China

$cc=new American();
echo $cc->sayWords();//hello! I am from American
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值