当前位置:网站首页>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 .
边栏推荐
- 百亿按摩仪蓝海,难出巨头
- 用 Go 跑的更快:使用 Golang 为机器学习服务
- mysql8.0JSON_ Instructions for using contains
- Is the securities account given by the head teacher of qiniu school safe? Can I open an account?
- Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
- 【C 题集】of Ⅷ
- Handwriting promise and async await
- 超级哇塞的快排,你值得学会!
- CPU design related notes
- 启牛学堂班主任给的证券账户安全吗?能开户吗?
猜你喜欢

Install and configure Jenkins

FR练习题目---综合题

【jvm】运算指令

Machine learning notes - gray wolf optimization

MySQL之CRUD

【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !

APR protocol and defense
![[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)](/img/d7/f49bca8da2ce286c18508325985990.png)
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)

PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用

Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
随机推荐
MongDB学习笔记
js亮瞎你眼的日期选择器
useMemo,memo,useRef等相关hooks详解
How to open an account of qiniu securities? Is it safe to open an account?
【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !
危机重重下的企业发展,数字化转型到底是不是企业未来救星
当代人的水焦虑:好水究竟在哪里?
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
外盘入金都不是对公转吗,那怎么保障安全?
Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
[C question set] of Ⅷ
华为哈勃化身硬科技IPO收割机
CPU设计相关笔记
be careful! Software supply chain security challenges continue to escalate
Type declaration of all DOM elements in TS
[recruitment position] Software Engineer (full stack) - public safety direction
安装配置Jenkins
MySQL----函数
选择排序和冒泡排序
【华为机试真题详解】字符统计及重排