Php object-oriented full strategy (12) abstract methods and abstract classes

Source: Internet
Author: User
In OOP, a class can have one or more sub-classes, and each class has at least one public method as an external code to access its interface. Abstract methods are introduced to facilitate Inheritance. let's take a look at the definition of abstract classes and abstract methods and then describe their usage. Abstract methods and abstract classes
In OOP, a class can have one or more sub-classes, and each class has at least one public method
External code access its interface. Abstract methods are introduced to facilitate Inheritance. let's take a look at the abstract class and
The definition of the abstract method illustrates its purpose.
What is an abstract method? The method defined in the class without a method body is an abstract method.
The method body means that there is no braces or content in the method declaration, but directly after the method name
End with a semicolon, and add the keyword "abstract" to declare the abstract method;
For example:
Abstract function fun1 ();
Abstract function fun2 ();
In the above example, the abstract methods "fun1 ()" and "fun2 ()" modified by "abstract" without method bodies should not be forgotten.
An abstract method is followed by a semicolon. So what is an abstract class? As long as there is a method in a class that is the abstract side
This class must be defined as an abstract class. abstract classes must also be modified using the "abstract" keyword.
It can have non-abstract methods and member attributes, but as long as there is a method that is abstract, this class must declare
It is an abstract class and can be modified using abstract.
For example:
Code snippet
The code is as follows:
Abstract class Demo {
Var $ test;
Abstract function fun1 ();
Abstract function fun2 ();
Function fun3 (){
... .
}
}

In the above example, an abstract class "Demo" is defined and "abstract" is used for modification. in this class,
Member attributes "$ test", and two abstract methods "fun1" and "fun2" have a non-abstract method fun3 (); that
How do we use abstract classes? The most important thing is that abstract classes cannot generate instance objects, so they cannot directly
We have mentioned many times that classes cannot be used directly. We use objects instantiated by classes.
The object class cannot generate instance objects. what is the purpose of declaring an abstract class? We use the abstract method as the modulo for subclass overloading.
The abstract class is equivalent to defining a specification, which requires sub-classes to comply with and sub-classes to follow the function abstraction.
Class, the abstract methods in the abstract class are implemented according to the requirements of the subclass. Subclass must include all abstract methods in the parent class
Otherwise, there are still abstract methods in the subclass, so the subclass is still an abstract class and still cannot be instantiated. why?
Do they have to inherit from abstract classes? Sometimes, to implement some functions, we must inherit from the abstract class; otherwise
You cannot implement these functions. if you inherit an abstract class, you must implement the abstract methods in the class;
Code snippet
The code is as follows:
Abstract class Demo {
Var $ test;
Abstract function fun1 ();
Abstract function fun2 ();
Function fun3 (){
... .
}
}
$ Demo = new Demo (); // The abstract class is used to generate instance objects. Therefore, this is an error. The instantiated object is handed over to the subclass.
Class Test extends Demo {
Function fun1 (){
...
}
Function fun2 (){
...
}
}
$ Test = new Test (); // The subclass can instantiate the object, because all the abstract methods in the parent class are implemented.
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.