Section Tenth-abstract methods and abstract classes-Classes and Objects in PHP5 [10]_php tutorial

Source: Internet
Author: User
Section Tenth-abstract methods and abstract classes

Object-oriented programs are built from a hierarchical structure of classes. In single-inheritance languages such as PHP, the inheritance of classes is tree-like. A root class has one or more subclasses, and then inherits one or more sub-classes from each subclass. Of course, there may be multiple root classes that can be used to implement different functions. In a well-designed system, each root class should have a useful interface that can be used by the application code. If our application code is designed to work with the root class, it can also cooperate with any subclass that inherits from the Root class.

An abstract method is a placeholder for a generic method like a subclass (which occupies a place but does not work), and it differs from the general method-there is no code. If there is one or more abstract methods in the class, then the class becomes an abstract class. You cannot instantiate an abstract class. You must inherit them, and then instantiate the subclasses. You can also think of abstract classes as a template for subclasses.

If you overwrite all the abstract methods, the subclass becomes a normal class. If all methods are not covered, the subclass is still abstract. If a class contains abstract methods (even if there is only one), you must declare that the class is abstract and add an abstract before the class keyword.

The syntax for declaring an abstract method differs from declaring a general method. The abstract method does not contain the body part of the curly braces {} As the general method does, and ends with a semicolon.

In example 6.13, we define a class shape containing the Getarea method. But because we do not know the shape is impossible to determine the area of the graph, we declare the Getarea method as an abstract method. You cannot instantiate a Shape object, but you can inherit it or use it in an expression, as in example 6.13.

If you create a class that has only an abstract method, you define an interface (interface). To illustrate this situation, PHP has the interface and implements keywords. You can use interface instead of abstract classes, and use implements instead of extends to illustrate your class definition or use an interface. For example, you can write a MyClass implements Myiterface. These two methods can be chosen according to personal preference.

/* Note:
Both methods refer to:
1. Abstract class aaa{} (Note that there are only abstract methods in AAA, no general method)
Class BBB extends aaa{} (the abstract method of covering AAA in BBB)
2. Interface aaa{}
Class BBB implements aaa{} (the abstract method of covering AAA in BBB)
*/

Listing 6.13 Abstract Classes
 Base * $this->height)/2);        } public Function Getnumberofsides ()//overwrite the number of edges statistic method {return (3);        }}//concrete class entity class quadrilateral class Rectangle extends Polygon {public $width;        Public $height;        Public Function Getarea () {return ($this->width * $this->height);        } public Function Getnumberofsides () {return (4);        }}//concrete class entity class Circle class Circle extends Shape {public $radius;        Public Function Getarea () {return (PI () * $this->radius * $this->radius);    }}//concrete the root class defines a color class, type class Color {public $name; } $myCollection = Array ();    Create a collection of shapes, put in an array//make a rectangle $r = new Rectangle;    $r->width = 5;    $r->height = 7;    $myCollection [] = $r;    Unset ($R);    Make a triangle $t = new triangle;    $t->base = 4;    $t->height = 5; $myCollection [] =$t;    Unset ($t);    Make a circle $c = new Circle;    $c->radius = 3;    $myCollection [] = $c;    Unset ($c);    Make a color $c = new color;    $c->name = "Blue";    $myCollection [] = $c;    Unset ($c); foreach ($myCollection as $s) {if ($s instanceof Shape)//If $s is an instance of the Shape class {print ("area:".                $s->getarea (). "
"); } if ($s instanceof Polygon) {print ("Sides:". $s->getnumberofsides (). "
"); if ($s instanceof Color) {print ("Color: $s->name
"); } print ("
"); }?>

http://www.bkjia.com/PHPjc/532553.html www.bkjia.com true http://www.bkjia.com/PHPjc/532553.html techarticle the tenth section-abstract methods and abstract class object-oriented programs are constructed from the hierarchical structure of classes. In single-inheritance languages such as PHP, the inheritance of classes is tree-like. A root class has one or more ...

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