Object-oriented - abstract methods and classes

php abstract class definition

Similar abstract classes and interfaces are special classes. An abstract class is a special one kind of class, interface is a special kind of abstract class. Their role in the understanding will be a little difficult. Want to understand the need to understand abstract class abstract methods in object-oriented programming languages, a class can be a subclass number, and each class has as its at least one common external access method (parent) interface, in order to facilitate on the introduction of the inherited abstract methods before class.

What is php abstract methods?

Abstract method is a method of no method body, there is no method body means no braces and its contents when the method declaration, but when declaring method after method name with a semicolon immediately terminated, the statement abstract method you want to use the abstract keyword modifications . Statement format abstract methods: abstract function ();

What is php abstract class?

As long as the class declaration is an abstract class abstract methods, abstract classes also use the abstract keyword modification, an abstract class can have an abstract method and the method is not a member of attributes, but access can not be private (private key modified)

Highlights:

* As long as declaring class is an abstract class abstract methods, abstract classes also use the abstract keyword modifications 
can have abstract methods in an abstract class, the class exactly the same as normal * a
* can not have abstract methods abstract class method and member properties
* abstract classes can not be instantiated
* All abstract methods of the parent class subclass must override, otherwise the abstract class or subclass


index.php
<? PHP
 / * * 
 * As long as the class declaration is an abstract class abstract methods, abstract classes also use the abstract keyword modification 
 * can have an abstract method in an abstract class, the class exactly the same as normal 
 * abstract class can there is not an abstract method method and member properties 
 * abstract classes can not be instantiated 
 * All abstract methods of the parent class subclass must override, otherwise this sub-class or an abstract class 
 * / 
the include ' car.php ' ; 

// error, an abstract class not instantiate
 // $ new new CAR CAR = ();
 // $ CAR2 new new CAR2 = (); 
$ = Bus new new Bus (); 

$ Bus -> my_car (); 
$ Bus -> getName ();

 

car.php

<? PHP 

abstract  class CAR 
{ 
    public $ name = ' BMW ' ;
     abstract function my_car (); 
    function getName () 
    { 
        echo $ the this -> name; 
    } 

} 

class Bus the extends CAR 
{ 
    function my_car () 
    { 
        echo ' used my_car abstract class /> <br ' ; 
    } 
} 

// a method is not an abstract class abstract 
abstract  class CAR2 
{ 
    public $ name = . 1 ;
    car2_fun function () 
    { 
        echo ' function ' ; 
    } 
}

 

Guess you like

Origin www.cnblogs.com/longqin/p/11708719.html