Introduction and Application of abstract keywords in PHP abstract methods and abstract classes, abstract keywords
PHP abstract methods and abstract keywords
Abstract keywords are used to define abstract methods and abstract classes.
Abstract Method
An abstract method is a method without a method body. Specifically, when a method is declared, there is no {} arc or content in it. Instead, the method name is directly added with a semicolon after the method name.
Abstract keywords are used to define abstract methods. Syntax:
Abstract function function_name ();
Abstract class
As long as a method in a class is an abstract method, this class must be defined as an abstract class. Abstract classes are also defined using abstract keywords.
Abstract classes cannot generate instance objects. They are usually used as templates that overload subclass methods, and all methods in the inherited abstract classes must be implemented. Abstract classes are introduced to facilitate inheritance.
Example:
Copy codeThe Code is as follows:
<? Php
Abstract class AbstractClass {
// Define the abstract Method
Abstract protected function getValue ();
// Common method
Public function printOut (){
Print $ this-> getValue (). "<br/> ";
}
}
Class ConcreteClass extends AbstractClass {
Protected function getValue (){
Return "Implementation of abstract methods ";
}
}
$ Class1 = new ConcreteClass;
$ Class1-> printOut ();
?>
In this example, the parent class defines the abstract method and the implementation of the method, but the actual content is defined in the subclass.
C # It is best to use the following code to describe the abstract class and how to use it
It is used to provide basic methods to inherit from a derived class!
1. Declare an abstract method using the abstract keyword.
2. A class can contain one or more abstract methods.
3. Non-abstract methods can exist in abstract classes.
4. abstract classes cannot be directly instantiated.
5. the abstract class is implemented using ":" (colon), and the abstract method is implemented using the override keyword.
6. the abstract class can be inherited by the abstract class, and the result is still the abstract class.
7. After an abstract method is implemented, the modifier cannot be changed.
Example:
Public abstract class Person
{
Public abstract void SayHello ();
Public void about ()
{
Console. WriteLine ("Abstract Demo ");
}
}
Public class Student: Person
{
Public override void SayHello ()
{
Console. WriteLine ("SayHello ");
}
}
Class MainClass
{
Public static void Main ()
{
New Student (). SayHello ();
}
}
Php abstract methods and classes are generally used in
Abstract Method
An abstract method is a method without a method body. Specifically, when a method is declared, there is no {} arc or content in it. Instead, the method name is directly added with a semicolon after the method name.
Abstract keywords are used to define abstract methods. Syntax:
Abstract function function_name ();
Abstract class
As long as a method in a class is an abstract method, this class must be defined as an abstract class. Abstract classes are also defined using abstract keywords.
Abstract classes cannot generate instance objects. They are usually used as templates that overload subclass methods, and all methods in the inherited abstract classes must be implemented. Abstract classes are introduced to facilitate inheritance.
Example:
<? Php
Abstract class AbstractClass {
// Define the abstract Method
Abstract protected function getValue ();
// Common method
Public function printOut (){
Print $ this-> getValue (). "<br/> ";
}
}
Class ConcreteClass extends AbstractClass {
Protected function getValue (){
Return "Implementation of abstract methods ";
}
}
$ Class1 = new ConcreteClass;
$ Class1-> printOut ();
?>
In this example, the parent class defines the abstract method and the implementation of the method, but the actual content is defined in the subclass.