In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand abstract classes and abstract methods in php". In daily operation, I believe many people have doubts about how to understand abstract classes and abstract methods in php. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions about how to understand abstract classes and abstract methods in php. Next, please follow the editor to study!
In an object-oriented (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 access. Abstract methods are introduced to facilitate inheritance, so now let's take a look at how abstract classes and abstract methods are defined and their characteristics.
What is an abstract method? We define in the class only the method name without the method body is the abstract method, the so-called no method body is no curly braces and the contents of the method declaration, but directly declare the method name with a semicolon after the end, in addition to declare the abstract method with a keyword "abstract" to modify.
1. Abstract keyword: abstract
Abstraction means that it cannot be specified, but there are certain concepts or names. To declare an abstract class or method in PHP, we need to use the adstract keyword.
2. Definition of abstract methods and abstract classes
At least one method in a class is abstract, which we call an abstract class. So if you define an abstract class, you first define an abstract method.
The copy code is as follows:
Abstract class class1 {
Abstract function fun1 ()
……
}
1. There is at least one abstract method in the class
2. Abstract methods are not allowed to have {}
3. Abstract must be added before the abstract method.
3. rules for the use of abstract classes and methods
Several characteristics of abstract classes:
1. It cannot be instantiated, it can only be inherited
2. All abstract methods must be overloaded in inherited derived classes before they can be instantiated.
The declaration of the abstract method is as follows:
The copy code is as follows:
What is an abstract class? As long as a method in a class is an abstract method, the class must be defined as an abstract class. Abstract classes are also modified with the keyword "abstract". Abstract classes cannot instantiate objects, so abstract methods are used as templates for subclass method overloading, and all methods in inherited abstract classes are implemented.
Examples of abstract classes and their implementation are as follows:
The copy code is as follows:
Note: when an abstract class inherits another abstract class (intended to extend that abstract class), you cannot override the abstract method of the parent class.
In PHP5.1, static abstract methods are supported in abstract classes. In the following example, you can see that static abstract methods can be declared. When implementing this method, it must be static.
The copy code is as follows:
At this point, the study on "how to understand the problems of abstract classes and abstract methods in php" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.