当前位置:网站首页>The difference between abstract classes and interfaces in PHP (PHP interview theory question)
The difference between abstract classes and interfaces in PHP (PHP interview theory question)
2022-07-05 14:59:00 【Back end regular developers】
Catalog
php The difference between abstract class and interface in
1、 Concept
Three concepts of object orientation : encapsulation , Inherit , polymorphic
Encapsulating properties and methods is a class .
The replication of a class's properties and methods by another class is inheritance ,PHP Any class in it can be inherited , Inherited properties and methods can be redefined by subclasses , This is polymorphism .
Abstract classes and abstract methods :
- The so-called abstract method is empty with only name “ function ”, And the abstract method must be an empty method .
- An abstract class is a class that contains abstract methods , At the same time, abstract classes can also contain ordinary methods .
- Because the abstract method must be an empty method , So when any subclass inherits an abstract class , Must redefine the concrete connotation of abstract methods , To instantiate .
Interface :
Special abstract classes that contain only abstract methods .
let me put it another way , Ordinary methods and properties are not allowed in interface classes .
2、 case analysis
(1) php Interface class :interface
In fact, their role is very simple , When many people work together to develop a project , May call some classes written by others , Then you'll ask , How do I know the name of the implementation method of a function , This time Hou php Interface classes work , When we define an interface class , The method in it must be implemented by the following subclasses , such as :
interface Shop
{
public function buy($gid);
public function sell($gid);
public function view($gid);
}
I declare that shop Interface class , Three methods are defined : buy (buy), sell (sell), see (view), Then all subclasses that inherit this class must implement this 3 There is no way without one method , If the subclass does not implement these things , Can't run . In fact, interface classes are plain , Is a class template , Provisions of a class , If you belong to this kind of , You must follow my rules , Not without one , But how do you do it , I don't care , That's your business , Such as :
class BaseShop implements Shop {
public function buy($gid) {
echo(' You bought it ID by :'.$gid.' The goods ');
}
public function sell($gid) {
echo(' You sold it ID by :'.$gid.' The goods ');
}
public function view($gid) {
echo(' You checked ID by :'.$gid.' The goods ');
}
}
Do you think , In a large multi person project , How convenient it is to have interface classes , So you don't have to ask others , What is the method name of your XXX function , Of course, if you like this, I have no way .
Conclusion : The interface class is the leader of a class , Point out the direction , Subclass must complete its specified method .
(2)php abstract class : abstract
In fact, some abstract classes and interface classes are very similar , Remember where I saw such a sentence , Abstract classes extract the part of the class image , This sentence looks very funny , In fact, it speaks the truth of abstract classes , The function of abstract classes is , When you find that many of your classes use many methods, you are constantly writing repeatedly , Then you can consider using abstract classes , You might say “ I can't rewrite a class. I instantiate each public class , Just call the same method ”, Here you can , In fact, this is what abstract classes do , However, it omits the step of instantiation , It makes you as convenient as calling this class method directly , And you can also overload this method . Such as :
abstract class BaseShop
{
public function buy($gid)
{
echo(' You bought it ID by :'.$gid.' The goods ');
}
public function sell($gid)
{
echo(' You sold it ID by :'.$gid.' The goods ');
}
public function view($gid)
{
echo(' You checked ID by :'.$gid.' The goods ');
}
}
class BallShop extends BaseShop
{
var $itme_id = null;
public function __construct()
{
$this->itme_id = 2314;
}
public function open()
{
$this->sell($this->itme_id);
}
}
Here is an example , Like the above, I defined a store class , Pull out all its like parts , buy (buy), sell (sell), see (view), And these methods are implemented in abstract classes , Then the subclasses that inherit it automatically get these methods , Subclasses do their own unique things , Introduce code repetition , Improve reusability .
Conclusion : An abstract class is a service provider of a class , With many services , You don't have to , You can use it when you need it , If you don't feel satisfied with the service , You can also do it yourself .
3、 Their differences
- Abstract classes can have non abstract methods, while interfaces can only have abstract methods !
- A class can inherit multiple interfaces , A class can only inherit one abstract class !
- The interface is used through implements Keyword , Abstract classes inherit extends Keyword !
- Member variables cannot be declared in an interface ( Including class static variables ), But you can declare class constants . Abstract classes can declare various types of member variables , Realize the encapsulation of data .( another JAVA All member variables in the interface should be declared as public static final type )
- Interface does not have a constructor , Abstract classes can have constructors .
- The default methods in the interface are public Type of , Methods in abstract classes can use private,protected,public To modify .
边栏推荐
- Ecotone technology has passed ISO27001 and iso21434 safety management system certification
- 计算中间件 Apache Linkis参数解读
- Two Bi development, more than 3000 reports? How to do it?
- CPU design practice - Chapter 4 practice task 3 use pre delivery technology to solve conflicts caused by related issues
- R 熵权法计算权重及综合得分
- JS bright blind your eyes date selector
- Penetration testing methodology
- 社区团购撤城“后遗症”
- NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
- 可转债打新在哪里操作开户是更安全可靠的呢
猜你喜欢
FR练习题目---简单题
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
[12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!
Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
Machine learning notes - gray wolf optimization
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
百亿按摩仪蓝海,难出巨头
Penetration testing methodology
一键更改多个文件名字
机器学习笔记 - 灰狼优化
随机推荐
be careful! Software supply chain security challenges continue to escalate
想进阿里必须啃透的12道MySQL面试题
useMemo,memo,useRef等相关hooks详解
maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
leetcode:881. lifeboat
Two Bi development, more than 3000 reports? How to do it?
市值蒸发超百亿美元,“全球IoT云平台第一股”赴港求生
漫画:程序员不是修电脑的!
Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
长列表优化虚拟滚动
[recruitment position] Software Engineer (full stack) - public safety direction
Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification
1330:【例8.3】最少步数
Security analysis of Web Architecture
What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
Super wow fast row, you are worth learning!
How to solve the problem of garbled code when installing dependency through NPM or yarn
729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
两个BI开发,3000多张报表?如何做的到?
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "