Occurrence Reason: parent classOf uncertainties
Why design abstract class this technology?
1. in real-world development, we may have a class that is the parent of other classes , but it does not need to be instantiated by itself, and the main purpose is to let subclasses inherit. This achieves code reuse, while benefiting project designers and design classes.
2. keywords :abstract
3. Basic usage :
Abstract class name
{// method
// Properties
abstract modifier function function name ( Parameter list );
/ *theabstract modifier function function name ( parameter list )
{
Echo " This is a wrong formulation! ";// because the method body is written more
}
*/
}
Abstract modifies a class, which is called an abstract class;
Abstract Modify a method that is called an abstract method "if it is an abstract method, there cannot be a method body";
4. precautions :
a . Abstract classes can have no abstract methods, but also can have implemented methods (complete functions);
B. once a method declares Abstract method, you must declare that the class is Abstract class, which is Abstract class name corresponding Abstract method;
c. if a class inherits an abstract class, it must Inheritance of the abstract class. All abstract method, unless it is an abstract class;
Case 1 :
abstract class Animal// parent class
{
Public $name;
protected $price;
abstractPublic Function Cry ();
// there is no method body, this method is mainly to let subclass to implement
}
class Dog extends Animal// sub-class
{
Public function Cry ()// subclass Inherits Parent class method
{
echo " The Puppy barks! ";
}
}
$dog 1=new Dog ();
$dog 1->cry ();//
?>
The above introduces the PHP abstract class, including PHP, abstract class content, I hope that the PHP tutorial interested in a friend helpful.