PHP object-oriented Strategy (12) abstract methods and abstract class _php Foundation

Source: Internet
Author: User
Tags abstract
abstract methods and abstract classes
In an OOP language, a class can have one or more child classes, and each class has at least one public method as
The external code accesses its interface. and 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 again.
What is an abstract method? The method that we define in the class without the method body is the abstract method, the so-called no square
The law refers to the method declaration when there is no curly braces and its contents, but directly in the declaration after the method name
With the semicolon ending, another keyword "abstract" is added to the declaration of the abstract method.
For example:
Abstract function fun1 ();
Abstract function fun2 ();
The example above is an abstract method of "abstract" decorated with no method body "fun1 ()" and "fun2 ()", Don't forget
There is also a semicolon behind the abstract method, so what is an abstract class? As long as a class has a method that is abstract
method, the class is defined as an abstract class, and the abstract class is decorated with the "abstract" keyword, in the abstract class
Faces can have methods and member properties that are not abstract, but as long as there is a method that is abstract, the class must declare
As an abstract class, decorated with "abstract".
For example:
Code fragment
Copy Code code as follows:

Abstract class demo{
var $test;
Abstract function fun1 ();
Abstract function fun2 ();
function Fun3 () {
... .
}
}

The example above defines an abstract class "Demo" that is decorated with "abstract", in which the class defines a
member property "$test", and two abstract methods "Fun1" and "fun2" also have a Non-abstract method Fun3 ();
How do we use abstract classes? The most important point is that an abstract class cannot produce an instance object, so it is not possible to directly
Used, we mentioned several times that the class can not be used directly, we use the object that is instantiated by the class, and then smoke
Like a class cannot produce an instance object what's the use of declaring an abstract class? We are modeling the abstract method as a subclass of overloaded
Board, defining an abstract class is equivalent to defining a specification that requires subclasses to adhere to, subclass relay abstraction
class, the abstract method inside the abstract class is implemented according to the needs of the subclass. Subclasses must have all the abstract methods in the parent class
are implemented, or if there are abstract methods in subclasses, then subclasses are abstract classes, or they cannot be instantiated; why do I
Are you going to inherit from the abstract class? Because sometimes we have to implement some functions must inherit from the abstract class, otherwise
You can not achieve these functions, if you inherit the abstract class, it is necessary to implement the abstract method of the class;
Code fragment
Copy Code code as follows:

?
Abstract class demo{
var $test;
Abstract function fun1 ();
Abstract function fun2 ();
function Fun3 () {
... .
}
}
$demo =new demo (); Abstract class to be able to produce an instance object, so this is wrong, instantiate the object to the subclass
Class Test extends demo{
function Fun1 () {
...
}
function fun2 () {
...
}
}
$test =new test (); Subclasses can instantiate objects because all 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.