In 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 to access it. The abstract method is introduced to facilitate inheritance. Let's first take a look at the definition of abstract class and abstract method before explaining its use.
Abstract methods and abstract classes
In OOP language, a class can have one or more subclasses, and each class has at least one public method as
The interface through which external code accesses it. Abstract methods are introduced to facilitate inheritance. Let's first take a look at the definitions of abstract classes and
abstract methods before explaining their uses.
What is an abstract method? The method we define in the class without a method body is an abstract method. The so-called method body means that when the method is declared, there are no curly brackets and its contents, but directly in the method name when declaring it. After
, add a semicolon to end. In addition, when declaring an abstract method, add a keyword "abstract" to modify it;
For example:
abstract function fun1();
abstract function fun2() ;
The above example is the abstract method "fun1()" and "fun2()" without a method body modified by "abstract". Don't forget that
there is a semicolon after the abstract method; so what is abstraction? What about classes? As long as there is a method in a class that is an abstract method, then the class must be defined as an abstract class, and the abstract class must also be modified with the "abstract" keyword; in an abstract class, the surface can be abstract or not. Methods and members
attributes
, but as long as one method is an abstract method, this class must be declared
as an abstract class and decorated with "abstract". For example: Code snippet
abstract class Demo{ var $test; abstract function fun1(); abstract function fun2(); function fun3(){ … . } }
objects
, so they cannot be used directly. We have mentioned many times that classes cannot be used directly. What we use is instantiated by classes. Object, then an abstract class cannot produce instance objects. What is the use of declaring an abstract class? We use abstract methods as templates for subclasses
overloading. Defining an abstract class is equivalent to defining a specification. This specification requires subclasses to comply. Subclasses Following the abstract class, implement the abstract methods in the abstract class according to the needs of the subclass. The subclass must implement all the abstract methods in the parent class. Otherwise, if there are still abstract methods in the subclass, the subclass will still be an abstract class and cannot be instantiated. Why do we have to start from the abstract class? What about inheritance? Because sometimes if we want to implement some functions, we must inherit from the abstract class, otherwise
you will not be able to implement these functions. If you inherit the abstract class, you must implement the abstract method in the class;
Code snippet <?php
abstract class Demo{
var $test;
abstract function fun1();
abstract function fun2();
function fun3(){
… .
}
}
$demo=new Demo(); //抽象类为能产生实例对象,所以这样做是错的,实例化对象交给子类
class Test extends Demo{
function fun1(){
…
}
function fun2(){
…
}
}
$test=new Test(); //子类可以实例化对象,因为实现了父类中所有抽象方法
?>
The above is the detailed content of Detailed explanation of php abstract methods and abstract class usage examples. For more information, please follow other related articles on the PHP Chinese website!