真实世界抽象类用法简单示例 [英] Real world abstract class usage simple samples
本文介绍了真实世界抽象类用法简单示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用抽象类的任何现实世界简单示例? 我正在尝试进入PHP的OOP,但是我仍然不明白-为什么应该使用抽象类以及何时使用(是的,我知道不可能创建抽象类实例,只能创建继承它的类实例).>
Any real world simple samples of using abstract class? I'm trying to get in PHP's OOP, but I still can't understand - why abstract class should be used and when (Yes, I know that it's impossible to create abstract class instance, only instance of class inheriting it).
推荐答案
也许您有一个图像类,并且有2个驱动程序GD和ImageMagick.
Maybe you have an image class, and you have 2 drivers, GD and ImageMagick.
您的基类可能是
abstract class Image {
public function resize($width, $height) {
// Some prep code...
}
}
然后您的GD驱动程序会像
Then your GD driver will be like
class Image_Gd extends Image {
public function resize($width, $height) {
// Actual code
}
}
请查看 Kohana在GitHub上的源代码 .它有一个Image
类,它是abstract
.
Look at Kohana's source on GitHub. It has an Image
class which is abstract
.
这篇关于真实世界抽象类用法简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文