PHP abstract method and abstract class abstract keyword introduction and application, abstract keyword_PHP tutorial

WBOY
Release: 2016-07-13 10:16:55
Original
1125 people have browsed it

Introduction and application of abstract keyword in PHP abstract methods and abstract classes, abstract keyword

PHP abstract methods and abstract classes abstract keyword
The abstract keyword is used to define abstract methods and abstract classes.

Abstract method

Abstract method refers to a method without a method body. Specifically, when the method is declared, there is no {} brackets and its contents. Instead, it is directly declared with a semicolon after the method name.

abstract keyword is used to define abstract methods, syntax:
abstract function function_name();

Abstract class

As long as there is an abstract method in a class, then the class must be defined as an abstract class. Abstract classes are also defined using the abstract keyword.
Abstract classes cannot produce instance objects. Abstract methods are usually used as templates for subclass method overloading, and all methods in the inherited abstract class must be implemented. In fact, abstract classes are introduced to facilitate inheritance.

Example:

Copy code The code is as follows:

abstract class AbstractClass{
// Define abstract method
abstract protected function getValue();
// Normal method
public function printOut(){
print $this->getValue()."
";
}
}
class ConcreteClass extends AbstractClass{
protected function getValue(){
return "Implementation of abstract method";
}
}

$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 child class.

What is an abstract class in C# and how to use it? It is best to explain it in code below

Used to provide basic methods for derived classes to inherit!

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 instantiated directly.
5. Use ":" (colon) to implement abstract classes, and use the override keyword to implement abstract methods.
6. Abstract classes can be inherited by abstract classes, and the result is still an abstract class.
7. After the abstract method is implemented, the modifier cannot be changed.
An example is as follows:
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();
}
}

Where are php abstract methods and classes generally used?

Abstract method
Abstract method refers to a method without a method body. Specifically, when the method is declared, there is no {} brackets and its contents. Instead, it is directly declared with a semicolon after the method name.

abstract keyword is used to define abstract methods, syntax:

abstract function function_name();
Abstract class
As long as one method in a class is an abstract method, then this The class must be defined as an abstract class. Abstract classes are also defined using the abstract keyword.

Abstract classes cannot produce instance objects. Abstract methods are usually used as templates for subclass method overloading, and all methods in the inherited abstract class must be implemented. In fact, abstract classes are introduced to facilitate inheritance.

Example:
abstract class AbstractClass{
// Define abstract method
abstract protected function getValue();
// Ordinary method
public function printOut(){
print $this->getValue()."
";
}
}
class ConcreteClass extends AbstractClass{
protected function getValue (){
return "Implementation of abstract method";
}
}

$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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/895118.htmlTechArticlePHP Introduction and application of abstract methods and abstract classes abstract keyword, abstract keyword PHP abstract methods and abstract classes abstract key The abstract keyword is used to define abstract methods and abstract classes...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!