看表格看不懂的,请回顾一下前三篇文章,这是一份总结表。通过对比更直观。 - Class Interface Trait 目的 封装一类对象的属性和行为。 抽象一类对象的行为准则,而不关注其具体实现。 解决单继承的限制。(历史解决方法是使用混入类(Mixin)) Class class Cla
看表格看不懂的,请回顾一下前三篇文章,这是一份总结表。通过对比更直观。
- | Class | Interface | Trait |
---|---|---|---|
目的 | 封装一类对象的属性和行为。 | 抽象一类对象的行为准则,而不关注其具体实现。 | 解决单继承的限制。(历史解决方法是使用混入类(Mixin)) |
Class | class ClassA extends ClassB | class Class implements Interface |
class Class { use Trait; } |
Interface | - | Interface1 extends Interface2 | - |
Trait | - | - |
trait Trait1 { use Trait2; } |
*注:以上是三者可使用的关系,以下是其具体区别! | |||
abstract | 类前(抽象类),方法前(抽象方法) | - | 方法前(抽象方法) |
final | 类前(不可继承),方法前(不可重写) | - | 方法前(不可重写) |
const | 有类常量 | 有接口常量 | - |
static | 静态变量,静态方法 | 静态方法 | 静态变量,静态方法 |
访问控制 public protected private |
继承可改变访问控制 实现接口只能用public use Trait可以用as改变访问控制。 |
- | use Trait可以用as改变访问控制 |
同名变量 |
覆盖(extends Class) 报错(use Trait) |
- | 报错(use Trait) |
同名方法 |
覆盖(extends) 报错(use Trait) 可用insteadof或as解决 |
报redeclare错(extends Interface) | 报错(use Trait) 可用insteadof或as解决 |
*注:静态成员变量/函数与普通成员变量/函数的命名不可一样,不然会报错。 |
( 完 )
版权所有:老白经 转载请保留来源信息。 >

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)