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 an interface for
external code to access 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 methods "fun1()" and "fun2()" without a method body modified by "abstract". Don't forget
There is also a semicolon after the abstract method; so what is abstraction? What about classes? As long as there is an abstract method in a class, 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
method can be abstract or not. Methods and member attributes, but as long as one method is abstract, the class must be declared
as an abstract class and decorated with "abstract".
For example:
Code snippet
Copy code 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 modified with "abstract". In this class, a
member attribute "$test" and two abstract methods "fun1" and "fun2" are defined. There is also a non-abstract method fun3(); then
how do we use abstract classes? The most important point is that abstract classes cannot produce instance objects, so they cannot be used directly. We have mentioned many times that classes cannot be used directly. We are using objects instantiated through classes, so we can use them
directly. >Image classes cannot produce instance objects. What is the use of declaring abstract classes? We use abstract methods as templates for subclass overloading
. Defining an abstract class is equivalent to defining a specification. This specification requires subclasses to comply. Subclasses inherit abstract functions
After the class, implement the abstract methods in the abstract class according to the needs of the subclass. The subclass must implement all abstract methods in the parent class. Otherwise, if there are still abstract methods in the subclass, then the subclass is still 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 an abstract class, otherwise
you will not be able to implement these functions. If you inherit an abstract class, you must implement the abstract methods in the class;
Code snippets
Copy code
The code is as follows:
abstract class Demo{ var $test;
abstract function fun1();
abstract function fun2();
function fun3(){
… .
}
}
$demo=new Demo(); //Abstract The class can generate instance objects, so it is wrong to do so. The instantiated object is handed over 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
?>
http://www.bkjia.com/PHPjc/320627.html
www.bkjia.com
truehttp: //www.bkjia.com/PHPjc/320627.htmlTechArticleAbstract methods and abstract classes In OOP language, a class can have one or more subclasses, and each class There is at least one public method as an interface for external code to access it. And abstract methods...