If you cannot read the table, review the first three articles. This is a summary table. Comparison is more intuitive.
If you cannot read the table, review the first three articles. This is a summary table. Comparison is more intuitive. -Class Interface Trait encapsulates the attributes and behaviors of a Class of objects. Abstract The behavior rules of a class object without paying attention to its specific implementation. Solve the limitation of single inheritance. (The Historical solution is to use the mixed Class (Mixin) class Class CIA
If you cannot read the table, review the first three articles. This is a summary table. Comparison is more intuitive.
- |
Class |
Interface |
Trait |
Purpose |
Encapsulate the attributes and behaviors of a class of objects. |
Abstract The behavior rules of a class object without paying attention to its specific implementation. |
Solve the limitation of single inheritance. (The Historical solution is to use Mixin )) |
Class |
Class ClassA extends ClassB |
Class Class implements Interface |
Class Class { Use Trait; } |
Interface |
- |
Interface1 extends Interface2 |
- |
Trait |
- |
- |
Trait Trait1 { Use Trait2; } |
* Note: The above are the available relationships among the three. The specific differences are as follows! |
Abstract |
Class (abstract class), method (abstract method) |
- |
Method (abstract method) |
Final |
Class (not inherited), method (not overridden) |
- |
Method (not override) |
Const |
Class Constants |
Has an interface constant |
- |
Static |
Static variables and static methods |
Static Method |
Static variables and static methods |
Access Control Public Protected Private |
Inheritance can change access control Only public Use Trait can use as to change access control. |
- |
Use Trait to change access control using |
Variable with the same name |
Overwrite (extends Class) Use Trait) |
- |
Use Trait) |
Method with the same name |
Overwrite (extends) Use Trait can be resolved using insteadof or |
Error in redeclare (extends Interface) |
Use Trait can be resolved using insteadof or |
* Note: The names of static member variables/functions cannot be the same as those of common member variables/functions. Otherwise, an error is returned. |
(End)
All Rights Reserved: the source information of the old white is reprinted. < <类、接口、特性三者之间的关系和区别> >