"Code Learning" PHP Object-oriented abstract classes and interfaces

Source: Internet
Author: User

First, what is an abstract method?

A method is an abstract method if there is no method body (without "{}", a method that ends with a semicolon is the method that does not have a method body).

1, declare a method, do not use {}, and the direct semicolon end

2. If it is an abstract method, it must be modified by using abstract (keyword)

Second, what is abstract class

1. If there is a method in a class that is an abstract method, then this class is an abstract class

2. If an abstract class is declared, the class must be decorated with the abstract keyword

<?PHP//declares an abstract class to use the abstract keyword to identifyAbstract classperson{protected $name; protected $country; //declaring an abstract method without a method body in an abstract class    Abstract functionsay (); //in an abstract class, you can declare a normal fee abstract method    functionrun () {Echo' I'm walking ';//have method body, output a statement    }}?>

Attention:

1, as long as the use of abstract modified class, is an abstraction class

2, abstract class is a special class, in the abstract class can have an abstract method

3, except abstract class can have abstract method, unexpected, and normal class exactly the same

4. Abstract classes cannot instantiate objects

Third, the role of abstract methods

1, the function of the abstract method is to stipulate that the subclass must have the realization of this method, function to two sub-categories

2, only write out the structure and did not realize, to achieve the specific sub-class to achieve

<?PHP//declares an abstract class to use the abstract keyword to identifyAbstract classperson{protected $name; protected $country; function__construct ($name= ",$country= ' China '){    $this->name =$name; $this->country =$country;} //declaring an abstract method without a method body in an abstract class    Abstract functionsay (); //in an abstract class, you can declare a normal fee abstract method    Abstract functionrun () {Echo' I'm walking ';//have method body, output a statement    }    classStudentextendsperson{functionsay () {Echo' I am a Chinese '; }    functionrun () {Echo' I'm running '; }        }}?>

Attention:

1. Abstract class cannot instantiate object (Cannot create object)

2, if the block Money key abstract class, you must write the subclass of this class, the abstract method of abstraction in the class overlay (plus method body)

3, subclasses are not allowed to implement all (overwrite rewrite) abstract method, this subclass can create the object, if only the implementation of the part, then there is an abstract method, the class must be abstract class

Iv. the role of abstract classes

is to require the structure of a subclass, so an abstract class is a specification

"Code Learning" PHP Object-oriented abstract classes and interfaces

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.