Introduction and Application of abstract keywords in PHP abstract methods and abstract classes, abstract keywords

Source: Internet
Author: User
Tags define abstract

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.

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.