PHP getting started tutorial-object-oriented Feature Analysis (inheritance, polymorphism, interface, abstract class, abstract method, etc.), Getting Started tutorial Polymorphism

Source: Internet
Author: User

PHP getting started tutorial-object-oriented Feature Analysis (inheritance, polymorphism, interface, abstract class, abstract method, etc.), Getting Started tutorial Polymorphism

This example describes the object-oriented features of PHP. We will share this with you for your reference. The details are as follows:

Demo1.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); // create a Computer class Computer {// What is the class, is to create a class within the brackets is called within the class, other places is outside the class. // Public is the public token of the field. This field can be accessed outside the class. The value is assigned and the value is public $ _ name = 'lenovo ';} $ computer = new Computer (); $ computer-> _ name = 'Dell '; echo $ computer-> _ name;?>

Demo2.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {// private is private, that is, the field is encapsulated, cannot be accessed outside of the class, And neither value nor value can operate private $ _ name = 'lenovo ';} $ computer = new Computer (); echo $ computer-> _ name;?>

Demo3.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {private $ _ name = 'lenovo '; // at this time, I use a public external method to access private fields // because private fields can only be accessed within the class, while external public methods are within the class. // More public methods are public, so they can be accessed outside the class. Public function _ run () {// The field must be a class-> field when called within the class, while $ _ name is just a common variable. // The method for calling a field outside the class is the object-> field, and the Computer-> _ name/must be used in the class, but in this class, you can use a keyword instead of a word to replace the Computer, that is, $ this echo $ this-> _ name ;}$ computer = new Computer (); $ computer-> _ run ();?>

Demo4.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {private $ name; private $ model; private $ cpu; private $ keyboard; private $ show; private $ zb; // you must write an external entry to obtain the public function getName () {return $ this-> name ;} // you must write an internal entry and assign a value to the private field public function setName ($ name) {// The $ name here is just a variable, parameter only // $ this-> name is the class field $ this-> name = $ name ;}$ computer = new Computer (); echo $ comp Uter-> getName (); $ computer-> setName ('Dell '); echo $ computer-> getName ();?>

Demo5.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {private $ _ name; private $ _ model; private $ _ cpu; // when an object outside the class calls a private field directly, it will check whether there is an interceptor. // If $ _ name is assigned directly, then the _ set method will be blocked and no error will be reported. // Assign values using the interceptor // assign values to private function _ set ($ _ key, $ _ value) {// use $ _ key = '_ name ', then $ _ value = 'lenovo '; // $ this-> _ name = 'lenovo'; $ this-> $ _ key = $ _ value ;} // value: private function _ get ($ _ key) {return $ this-> $ _ key; // If $ _ key = '_ name', $ this-> _ name; // If $ _ key =' _ cpu ', $ this-> _ cpu; // If $ _ key = '_ model', $ this-> _ model ;}}$ computer = new Computer (); $ computer-> _ name = 'lenovo '; $ computer-> _ cp U = 'quad-core '; $ computer-> _ model = 'i7'; echo $ computer-> _ name; echo $ computer-> _ cpu; echo $ computer-> _ model;?>

Demo6.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {private $ _ name; private $ _ model; private $ _ cpu; // The _ set and _ get methods are private and can still be executed because // The program pointer is already in the class. The class can run encapsulated Methods // The class executes private methods without any errors. // It only requires indirect interception. Interception is performed in the internal class. // To put it bluntly, __set () and _ get () are built-in PHP methods and have some special characteristics: private function _ set ($ _ key, $ _ value) {$ this-> $ _ key = $ _ value;} private function _ get ($ _ key) {return $ this-> $ _ key ;}} $ computer = new Computer (); $ computer-> _ name = 'lenovo '; $ computer-> _ cpu = 'quad-core '; $ computer-> _ model = 'i7 '; echo $ computer-> _ name; echo $ computer-> _ cpu; echo $ computer-> _ model;?>

Demo7.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {const NAME = 'Dell';} // constant output method class :: constant echo Computer: NAME; // DELL?>

Demo8.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {public $ _ count = 0; public function _ add () {$ this-> _ count ++; // $ _ count = $ _ count + 1 $ _ count ++} // perform a cumulative effect $ computer1 = new Computer (); $ computer1-> _ add (); $ computer1-> _ add (); $ computer1-> _ add (); echo $ computer1-> _ count; echo '<br/>'; $ computer2 = new Computer (); $ computer2-> _ add (); $ computer2-> _ add (); $ computer2- > _ Add (); echo $ computer2-> _ count;?>

Demo9.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {public static $ _ count = 0; public function _ add () {// if it is a static member field, use self instead of $ this self: $ _ count ++ ;}} // make a cumulative result $ computer1 = new Computer (); $ computer1-> _ add (); echo Computer ::$ _ count; $ computer1-> _ add (); echo Computer: $ _ count; $ computer1-> _ add (); echo Computer: $ _ count; echo '<br/>'; $ computer2 = new Computer () ; $ Computer2-> _ add (); echo Computer: $ _ count; $ computer2-> _ add (); echo Computer: $ _ count; $ computer2-> _ add (); echo Computer: $ _ count;?>

Demo10.php

<?php  header ( 'Content-Type:text/html; charset=utf-8;' );  class Computer {    public static $_count = 0;    public static function _add(){      self::$_count++;    }  }  Computer::_add();  Computer::_add();  Computer::_add();  echo Computer::$_count;?>

Demo11.php

<?php  header ( 'Content-Type:text/html; charset=utf-8;' );  class Computer {  }  $computer = new Computer();  echo $computer instanceof Computer;?>

Demo12.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); // This is the parent class, Computer class Computer {public $ _ name = 'lenovo '; public function _ run () {echo 'Lenovo is running! ';}}// Sub-class, laptop class NoteComputer extends Computer {}$ noteComputer = new NoteComputer (); echo $ noteComputer-> _ name; $ noteComputer-> _ run ();?>

Demo13.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {public $ _ name = 'lenovo'; public function _ run () {echo 'lenovo is running! ';}} Class NoteComputer extends Computer {// I do not need fields and methods of the parent class, the override method can be used to overwrite the fields and methods of the parent class public $ _ name = 'Dell '; public function _ run () {echo 'Dell is running! ';}}$ NoteComputer = new NoteComputer (); echo $ noteComputer-> _ name; $ noteComputer-> _ run ();?>

Demo14.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {// Private, but cannot inherit from the quilt class, in this case, the protected modifier should be used to encapsulate protected $ _ name = 'lenovo '; protected function _ run () {return' Lenovo is running! ';}} Class NoteComputer extends Computer {public function getTop () {echo $ this-> _ name; echo $ this-> _ run ();}} $ noteComputer = new NoteComputer (); $ noteComputer-> getTop ();?>

Demo15.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {public $ _ name = 'lenovo'; public function _ run () {return 'lenovo is running! ';}} Class NoteComputer extends Computer {// my subclass has covered the fields and methods of the parent class. // but I have to call the fields and methods of the parent class. What should I do? Public $ _ name = 'Dell '; public function _ run () {echo 'Dell is running! '; Echo parent: _ run () ;}$ noteComputer = new NoteComputer (); echo $ noteComputer-> _ name; $ noteComputer-> _ run (); // DellDell is running! Lenovo is running!?>

Demo16.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); // If final is added before the class, indicates that this class cannot be inherited. // final class Computer {//} class Computer {// final. If it is added before the method, it means that the final public function _ run () method cannot be rewritten () {}} class NoteComputer extends Computer {public function _ run () {}}$ noteComputer = new NoteComputer () ;?>

Demo17.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); // creates an abstract class, as long as the abstract class is added before the class, // The abstract class cannot be instantiated, that is, to create an object // only has an abstract method in the class, the class must be an abstract class, abstract class Computer {public $ _ name = 'lenovo 'must be added before the class '; // create an abstract method in the abstract class // The abstract method cannot implement the content of the method body abstract public function _ run (); // can I create a common public function _ run2 () {echo 'I am a common method of the parent class';} // The class cannot implement multi-inheritance, only single inheritance is supported. // The abstract class is used to inherit from the subclass. To implement a standard and resource-sharing class NoteComputer extends Computer {// abstract class abstract method, the subclass must be overwritten. Otherwise, an error is returned. // Normal methods in the abstract class do not need to be overwritten. The subclass will inherit the public function _ run () {echo 'Method of my subclass ';}} $ noteComputer = new NoteComputer (); $ noteComputer-> _ run (); $ noteComputer-> _ run2 (); echo $ noteComputer-> _ name;?>

Demo18.php

<? Php/** should I use abstract classes or interfaces? * if you want to inherit the method specifications of multiple interfaces, you should use interfaces. * If you want to share the content of a method body, use an abstract class. **/Header ('content-Type: text/html; charset = UTF-8 ;'); // create an interface // The interface cannot be instantiated. // The interface is used to standardize its sub-classes for unification. You can also share the data interface Computer {// The member field must be the variable const NAME = 'Member '; // all methods in the interface are abstract methods, the method body cannot be written // and abstract methods of the interface do not need to be written into abstract public function _ run (); public function _ run2 ();} interface Computer2 {public function _ run3 ();} // sub-class inherits the interface, which is called implementation. The interface can implement multiple class NoteComputer implements Computer and Computer2 {public function _ run () {echo 'I have rewritten run';} public function _ run3 () {echo' I have rewritten run3';} public function _ run2 (){ Echo 'I have rewritten run2'; }}$ noteComputer = new NoteComputer (); $ noteComputer-> _ run (); $ noteComputer-> _ run2 (); $ noteComputer-> _ run3 (); echo NoteComputer: NAME; // interface: constant // echo Computer: NAME;?>

Demo19.php

<? Php header ('content-Type: text/html; charset = UTF-8; '); // What is polymorphism, literally, A variety of forms // actions are executed by different people, and different effects or effects are produced, that is, polymorphism. // A person executes the same action in different states to form different effects, which can also be called polymorphism. // The gardener cut flowers and plants and/the barber cut hair/the President cut layoffs/People notebook run win7 boot/People desktop run xp boot // create an interface, interface Computer {public function version (); // indicates the Computer's public function work (); // how is this Computer running} // create a notebook class implementation Interface class NoteComputer implements Computer {public function version () {echo 'notebook ';} public function work () {echo 'can run win7';} // create a desktop class implementation Interface class program topcomputer implements Computer {pu Blic function version () {echo 'desktop ';} public function work () {echo 'run XP on workstation ';}} // create a user class Person {// create a method to accept the acceptance of the Computer (laptop, or desktop, it's okay to pass in their objects. Public function _ run ($ type) {echo 'this person's '; $ type-> version (); $ type-> work () ;}// the principle of polymorphism, that is, if the classes are all written, do not modify them. As long as the parameter changes outside the class and the final result will be changed, this is polymorphism. // There is one interface, two classes, one is the notebook class, and the other is the desktop class // The Notebook $ noteComputer = new NoteComputer () is created (); // create a desktop $ your topcomputer = new your topcomputer (); // create a person $ Person = new person (); // use the computer $ person-> _ run ($ noteComputer ); // This transfer is called object reference transfer?>

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.