Table of Contents
接口与抽象类的区别,接口抽象类区别
Home php教程 php手册 接口与抽象类的区别,接口抽象类区别

接口与抽象类的区别,接口抽象类区别

Jun 13, 2016 am 08:40 AM
abstract class

接口与抽象类的区别,接口抽象类区别

接口和抽象类有什么区别

你选择使用接口和抽象类的依据是什么?


接口和抽象类的概念不一样。接口是对动作的抽象,抽象类是对根源的抽象。

抽象类表示的是,这个对象是什么。接口表示的是,这个对象能做什么。比如,男人,女人,这两个类(如果是类的话……),他们的抽象类是人。说明,他们都是人。

人可以吃东西,狗也可以吃东西,你可以把“吃东西”定义成一个接口,然后让这些类去实现它.

所以,在高级语言上,一个类只能继承一个类(抽象类)(正如人不可能同时是生物和非生物),但是可以实现多个接口(吃饭接口、走路接口)。

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

总结几句话来说:

1、抽象类和接口都不能直接实例化,如果要实例化,抽象类变量必须指向实现所有抽象方法的子类对象,接口变量必须指向实现所有接口方法的类对象。

2、抽象类要被子类继承,接口要被类实现。

3、接口只能做方法申明,抽象类中可以做方法申明,也可以做方法实现

4、接口里定义的变量只能是公共的静态的常量,抽象类中的变量是普通变量。

5、抽象类里的抽象方法必须全部被子类所实现,如果子类不能全部实现父类抽象方法,那么该子类只能是抽象类。同样,一个实现接口的时候,如不能全部实现接口方法,那么该类也只能为抽象类。

6、抽象方法只能申明,不能实现,接口是设计的结果 ,抽象类是重构的结果

7、抽象类里可以没有抽象方法

8、如果一个类里有抽象方法,那么这个类只能是抽象类

9、抽象方法要被实现,所以不能是静态的,也不能是私有的。

10、接口可继承接口,并可多继承接口,但类只能单根继承。

 

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

 

当你关注一个事物的本质的时候,用抽象类;当你关注一个操作的时候,用接口。

 

抽象类的功能要远超过接口,但是,定义抽象类的代价高。因为高级语言来说(从实际设计上来说也是)每个类只能继承一个类。在这个类中,你必须继承或编写出其所有子类的

所有共性。虽然接口在功能上会弱化许多,但是它只是针对一个动作的描述。而且你可以在一个类中同时实现多个接口。在设计阶段会降低难度的。

 

接口的使用

 

接口:interface

在PHP中,我们可以规定,一个对象应该具有哪些公共的外部操作,即可使用interface来规定。
公共的方法就是接口。用于规定一个对象应该用于哪些公共的操作方法(接口),这个也叫接口(公共操作方法的集合)
即:接口(interface结构,公共方法集合)

公共方法(接口方法)
定义:用于限定某个对象所必须拥有的公共操作方法的一种结构,称之为接口(interface)
语法:定义接口结构,使用interface关键字。接口内定义的都是一些公共方法。

 

注意:
1.接口方法,访问权限必须是公共的 public
2.接口内只能有公共方法,不能存在成员变量
3.接口内只能含有未被实现的方法,也叫抽象方法,但是不用abstract关键字。

类实现接口,利用关键字implements完成。

 

 

这样,实现该接口的类,必须实现接口内所有的抽象方法。而且可以肯定,该方法一定是公共的外部操作方法。

多实现:该功能,在理论上可以通过抽象类来实现,但是抽象类,不专业。
使用接口则专业些,实现上,因为php支持多实现,而仅支持单继承。

php对象接口的支持,可以定义类常量,接口之间也可以继承

 

 

抽象方法和抽象类

在OOP 语言中,一个类可以有一个或多个子类,而每个类都有至少一个公有方法做为 
外部代码访问其的接口。而抽象方法就是为了方便继承而引入的,我们先来看一下抽象类和 
抽象方法的定义再说明它的用途。 
什么是抽象方法?我们在类里面定义的没有方法体的方法就是抽象方法,所谓的没有方 
法体指的是,在方法声明的时候没有大括号以及其中的内容,而是直接在声明时在方法名后 
加上分号结束,另外在声明抽象方法时还要加一个关键字“abstract”来修饰; 
例如: 
abstract function fun1(); 
abstract function fun2(); 
上例是就是“abstract”修饰的没有方法体的抽象方法“fun1()”和“fun2()”,不要忘记 
抽象方法后面还要有一个分号;那么什么是抽象类呢?只要一个类里面有一个方法是抽象方 
法,那么这个类就要定义为抽象类,抽象类也要使用“abstract”关键字来修饰;在抽象类里 
面可以有不是抽象的方法和成员属性,但只要有一个方法是抽象的方法,这个类就必须声明 
为抽象类,使用“abstract”来修饰。

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

上例中定义了一个抽象类“Demo”使用了“abstract”来修饰,在这个类里面定义了一 
个成员属性“$test”,和两个抽象方法“fun1”和“fun2”还有一个非抽象的方法fun3();那 
么抽象类我们怎么使用呢?最重要的一点就是抽象类不能产生实例对象,所以也不能直接使 
用,前面我们多次提到过类不能直接使用,我们使用的是通过类实例化出来的对象,那么抽 
象类不能产生实例对象我们声明抽象类有什么用呢?我们是将抽象方法是做为子类重载的模 
板使用的,定义抽象类就相当于定义了一种规范,这种规范要求子类去遵守,子类继函抽象 
类之后,把抽象类里面的抽象方法按照子类的需要实现。子类必须把父类中的抽象方法全部 
都实现,否则子类中还存在抽象方法,那么子类还是抽象类,还是不能实例化对;为什么我 
们非要从抽象类中继承呢?因为有的时候我们要实现一些功能就必须从抽象类中继承,否则 
这些功能你就实现不了,如果继承了抽象类,就要实现类其中的抽象方法;

 

 

单例模式

  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

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: 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

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

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