Table of Contents
The difference between interface and abstract class, the difference between interface abstract class
Home Backend Development PHP Tutorial The difference between interface and abstract class, the difference between interface abstract class_PHP tutorial

The difference between interface and abstract class, the difference between interface abstract class_PHP tutorial

Jul 12, 2016 am 08:53 AM
abstract class

The difference between interface and abstract class, the difference between interface abstract class

What is the difference between interface and abstract class

What is your basis for choosing to use interfaces and abstract classes?


The concepts of interfaces and abstract classes are different. The interface is the abstraction of actions, and the abstract class is the abstraction of the source.

Abstract class represents what this object is. The interface represents what this object can do. For example, men and women, these two classes (if they are classes...), their abstract class is people. Explain that they are all human beings.

People can eat, and dogs can also eat. You can define "eating" as an interface, and then let these classes implement it.

So, in high-level languages, a class can only inherit one class (abstract class) (just as a person cannot be a living thing and a non-living thing at the same time), but it can implement multiple interfaces (eating interface, walking interface).

http://hovertree.com/h/bjaf/to3l3tjm.htm

To summarize in a few words:

1. Neither abstract classes nor interfaces can be instantiated directly. If they are to be instantiated, abstract class variables must point to subclass objects that implement all abstract methods, and interface variables must point to class objects that implement all interface methods.

2. Abstract classes must be inherited by subclasses, and interfaces must be implemented by classes.

3. Interfaces can only be used for method declarations, while abstract classes can be used for method declarations and method implementations

4. Variables defined in the interface can only be public static constants, and variables in abstract classes are ordinary variables.

5. All abstract methods in an abstract class must be implemented by the subclass. If the subclass cannot implement all the abstract methods of the parent class, then the subclass can only be an abstract class. Similarly, when a class implements an interface, if it cannot implement all the interface methods, then the class can only be an abstract class.

6. Abstract methods can only be declared, not implemented. Interfaces are the result of design, and abstract classes are the result of reconstruction

7. There can be no abstract method in an abstract class

8. If there is an abstract method in a class, then this class can only be an abstract class

9. Abstract methods must be implemented, so they cannot be static or private.

10. Interfaces can inherit interfaces and multiple interfaces, but classes can only inherit from a single root.

1.抽象类 和 接口 都是用来抽象具体对象的. 但是接口的抽象级别最高
2.抽象类可以有具体的方法 和属性,  接口只能有抽象方法和不可变常量
3.抽象类主要用来抽象类别,接口主要用来抽象功能.
Copy after login
4、抽象类中,且不包含任何实现,派生类必须覆盖它们。接口中所有方法都必须是未实现的。
Copy after login

When you focus on the essence of a thing, use abstract classes; when you focus on an operation, use interfaces.

Abstract classes have far more functions than interfaces, but the cost of defining abstract classes is high. Because in high-level languages ​​(and in terms of actual design), each class can only inherit one class. In this class, you must inherit or write out

for all its subclasses

All commonalities. Although the interface will be much weaker in function, it is only a description of an action. And you can implement multiple interfaces in one class at the same time. The difficulty will be reduced during the design stage.

Usage of interface

Interface: interface

In PHP, we can specify what public external operations an object should have, which can be specified using interface.
Public methods are interfaces. Used to specify which public operation methods (interfaces) an object should be used for. This is also called an interface (a collection of public operation methods)
that is: interface (interface structure, collection of public methods)

Public methods (interface methods)
Definition: A structure used to limit the public operation methods that an object must have, called an interface (interface)
Syntax: To define an interface structure, use interface Keywords. What are defined in the interface are some public methods.

Note:
1. Interface methods, access rights must be public public
2. There can only be public methods in the interface, and member variables cannot exist
3. Within the interface It can only contain unimplemented methods, also called abstract methods, but does not use the abstract keyword.

The class implements the interface and uses the keyword implements to complete.

In this way, the class that implements the interface must implement all abstract methods in the interface. And it is certain that this method must be a public external operation method.

Multiple implementations: This function can theoretically be implemented through abstract classes, but abstract classes are not professional.
Using interfaces is more professional in terms of implementation, because PHP supports multiple implementations and only supports single inheritance.

PHP object interface support, class constants can be defined, and interfaces can also be inherited

Abstract methods and abstract classes

In OOP language, a class can have one or more subclasses, and each class has at least one public method as an interface for
external code to access it. Abstract methods are introduced to facilitate inheritance. Let’s first take a look at the definitions of abstract classes and
abstract methods before explaining their uses.
What is an abstract method? The method we define in the class without a method body is an abstract method. The so-called method body means that when the method is declared, there are no curly brackets and its contents, but it is directly included in the method name when declaring it. After
, add a semicolon to end. In addition, when declaring an abstract method, add a keyword "abstract" to modify it;
For example:
abstract function fun1();
abstract function fun2() ;
The above example is the abstract method "fun1()" and "fun2()" without a method body modified by "abstract". Don't forget
There is also a semicolon after the abstract method; so what is abstraction? What about classes? As long as there is an abstract method in a class
, then the class must be defined as an abstract class, and the abstract class must also be modified with the "abstract" keyword; in an abstract class,
can be abstract or not. Methods and member attributes, but as long as one method is an abstract method, the class must be declared
as an abstract class and decorated with "abstract".

http://hovertree.com/menu/php/

In the above example, an abstract class "Demo" is defined and modified with "abstract". In this class, a

member attribute "$test" and two abstract methods "fun1" and " "fun2" also has a non-abstract method fun3(); then
how do we use abstract classes? The most important point is that abstract classes cannot produce instance objects, so they cannot be used directly. We have mentioned many times that classes cannot be used directly. We are using objects instantiated through classes, so abstract classes cannot be used directly. >Image classes cannot produce instance objects. What is the use of declaring abstract classes? We use abstract methods as templates for subclass overloading
. Defining an abstract class is equivalent to defining a specification. This specification requires subclasses to comply. Subclasses inherit abstraction
After the class, implement the abstract methods in the abstract class according to the needs of the subclass. The subclass must implement all abstract methods in the parent class. Otherwise, if there are still abstract methods in the subclass, the subclass will still be an abstract class and cannot be instantiated. Why do we have to start from the abstract class? What about inheritance? Because sometimes we must inherit from an abstract class to implement some functions, otherwise
you will not be able to implement these functions. If you inherit an abstract class, you must implement the abstract method in the class;




Single case mode

  1. 单例模式(职责模式):  
  2. 简单的说,一个对象(在学习设计模式之前,需要比较了解面向对象思想)只负责一个特定的任务;  
  3. 单例类:  
  4. 1、构造函数需要标记为private(访问控制:防止外部代码使用new操作符创建对象),单例类不能在其他类中实例化,只能被其自身实例化;  
  5. 2、拥有一个保存类的实例的静态成员变量  
  6. 3、拥有一个访问这个实例的公共的静态方法(常用getInstance()方法进行实例化单例类,通过instanceof操作符可以检测到类是否已经被实例化)  
  7. 另外,需要创建__clone()方法防止对象被复制(克隆)  
  8. 为什么要使用PHP单例模式?  
  9. 1、php的应用主要在于数据库应用, 所以一个应用中会存在大量的数据库操作, 使用单例模式, 则可以避免大量的new 操作消耗的资源。  
  10. 2、如果系统中需要有一个类来全局控制某些配置信息, 那么使用单例模式可以很方便的实现. 这个可以参看ZF的FrontController部分。  
  11. 3、在一次页面请求中, 便于进行调试, 因为所有的代码(例如数据库操作类db)都集中在一个类中, 我们可以在类中设置钩子, 输出日志,从而避免到处var_dump, echo。  
  12. 代码实现:
  1. /1**  
  2. * 设计模式之单例模式  
  3. $_instance必须声明为静态的私有变量  
  4. * 构造函数和析构函数必须声明为私有,防止外部程序new  
  5. * 类从而失去单例模式的意义  
  6. * getInstance()方法必须设置为公有的,必须调用此方法  
  7. * 以返回实例的一个引用  
  8. * ::操作符只能访问静态变量和静态函数  
  9. new对象都会消耗内存  
  10. * 使用场景:最常用的地方是数据库连接。   
  11. * 使用单例模式生成一个对象后,  
  12. * 该对象可以被其它众多对象所使用。   
  13. */

 

<span>class</span><span> Danli {  
   
</span><span>//</span><span>保存类实例的静态成员变量  </span>
<span>private</span> <span>static</span> <span>$_instance</span><span>;  
   
</span><span>//</span><span>private标记的构造方法  </span>
<span>private</span> <span>function</span><span> __construct(){  
</span><span>echo</span> 'This is a Constructed method;'<span>;  
}  
   
</span><span>//</span><span>创建__clone方法防止对象被复制克隆  </span>
<span>public</span> <span>function</span><span> __clone(){  
</span><span>trigger_error</span>('Clone is not allow!',<span>E_USER_ERROR</span><span>);  
}  
   
</span><span>//</span><span>单例方法,用于访问实例的公共的静态方法  </span>
<span>public</span> <span>static</span> <span>function</span><span> getInstance(){  
</span><span>if</span>(!(self::<span>$_instance</span><span> instanceof self)){  
self</span>::<span>$_instance</span> = <span>new</span><span> self;  
}  
</span><span>return</span> self::<span>$_instance</span><span>;  
}  
   
</span><span>public</span> <span>function</span><span> test(){  
</span><span>echo</span> '调用方法成功'<span>;  
}  
   
}  
   </span><span>//</span><span> 何问起 hovertree.com
//用new实例化private标记构造函数的类会报错  
//$danli = new Danli();  
   
//正确方法,用双冒号::操作符访问静态方法获取实例  </span>
<span>$danli</span> = Danli::<span>getInstance();  
</span><span>$danli</span>-><span>test();  
   
</span><span>//</span><span>复制(克隆)对象将导致一个E_USER_ERROR  </span>
<span>$danli_clone</span> = <span>clone</span> <span>$danli</span>;
Copy after login

http://www.cnblogs.com/roucheng/p/3528396.html

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125681.htmlTechArticle接口与抽象类的区别,接口抽象类区别 接口和抽象类有什么区别 你选择使用接口和抽象类的依据是什么? 接口和抽象类的概念不一样。接...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Does golang have abstract classes? Does golang have abstract classes? Jan 06, 2023 pm 07:04 PM

Golang has no abstract classes. Golang is not an object-oriented (OOP) language. It has no concepts of classes, inheritance, and abstract classes. However, there are structures (structs) and interfaces (interfaces) in golang, which can be indirectly implemented through the combination of struct and interface. Abstract classes in object languages.

Application of interfaces and abstract classes in design patterns in Java Application of interfaces and abstract classes in design patterns in Java May 01, 2024 pm 06:33 PM

Interfaces and abstract classes are used in design patterns for decoupling and extensibility. Interfaces define method signatures, abstract classes provide partial implementation, and subclasses must implement unimplemented methods. In the strategy pattern, the interface is used to define the algorithm, and the abstract class or concrete class provides the implementation, allowing dynamic switching of algorithms. In the observer pattern, interfaces are used to define observer behavior, and abstract or concrete classes are used to subscribe and publish notifications. In the adapter pattern, interfaces are used to adapt existing classes. Abstract classes or concrete classes can implement compatible interfaces, allowing interaction with original code.

Inner class implementation of interfaces and abstract classes in Java Inner class implementation of interfaces and abstract classes in Java Apr 30, 2024 pm 02:03 PM

Java allows inner classes to be defined within interfaces and abstract classes, providing flexibility for code reuse and modularization. Inner classes in interfaces can implement specific functions, while inner classes in abstract classes can define general functions, and subclasses provide concrete implementations.

An in-depth discussion of the similarities and differences between Golang functional interfaces and abstract classes An in-depth discussion of the similarities and differences between Golang functional interfaces and abstract classes Apr 20, 2024 am 09:21 AM

Both functional interfaces and abstract classes are used for code reusability, but they are implemented in different ways: functional interfaces through reference functions, abstract classes through inheritance. Functional interfaces cannot be instantiated, but abstract classes can. Functional interfaces must implement all declared methods, while abstract classes can only implement some methods.

Java Interfaces and Abstract Classes: The Road to Programming Heaven Java Interfaces and Abstract Classes: The Road to Programming Heaven Mar 04, 2024 am 09:13 AM

Interface: An implementationless contract interface defines a set of method signatures in Java but does not provide any concrete implementation. It acts as a contract that forces classes that implement the interface to implement its specified methods. The methods in the interface are abstract methods and have no method body. Code example: publicinterfaceAnimal{voideat();voidsleep();} Abstract class: Partially implemented blueprint An abstract class is a parent class that provides a partial implementation that can be inherited by its subclasses. Unlike interfaces, abstract classes can contain concrete implementations and abstract methods. Abstract methods are declared with the abstract keyword and must be overridden by subclasses. Code example: publicabstractcla

Java interfaces and abstract classes: revealing the inner connection between them Java interfaces and abstract classes: revealing the inner connection between them Mar 04, 2024 am 09:34 AM

Interface Interface defines abstract methods and constants in Java. The methods in the interface are not implemented, but are provided by the class that implements the interface. The interface defines a contract that requires the implementation class to provide specified method implementations. Declare the interface: publicinterfaceExampleInterface{voiddoSomething();intgetSomething();} Abstract class An abstract class is a class that cannot be instantiated. It contains a mixture of abstract and non-abstract methods. Similar to interfaces, abstract methods in abstract classes are implemented by subclasses. However, abstract classes can also contain concrete methods, which provide default implementations. Declare abstract class: publicabstractcl

What is the difference between interfaces and abstract classes in PHP? What is the difference between interfaces and abstract classes in PHP? Jun 04, 2024 am 09:17 AM

Interfaces and abstract classes are used to create extensible PHP code, and there is the following key difference between them: Interfaces enforce through implementation, while abstract classes enforce through inheritance. Interfaces cannot contain concrete methods, while abstract classes can. A class can implement multiple interfaces, but can only inherit from one abstract class. Interfaces cannot be instantiated, but abstract classes can.

What is the difference between abstract classes and interfaces in PHP What is the difference between abstract classes and interfaces in PHP Mar 10, 2021 pm 05:51 PM

Differences: 1. Abstract classes can have attributes and ordinary methods, but interfaces cannot; 2. There may not be abstract methods in abstract classes, but there must be "abstract" methods in interfaces; 3. There are differences in syntax; 4. Abstract classes Use the abstract keyword to declare before a class, and a class is declared as a class. The interface is declared with interface, but cannot be declared with class; 5. The abstract method of an abstract class must be declared with abstract, but interfaces are not required; 6. Abstract classes and interfaces implement detailed methods in different ways.

See all articles