April 28 inheritance, polymorphic, overloaded, clone

Source: Internet
Author: User
Tags echo info

Review classes and objects
Class: Abstracted by many objects.
Object: instantiated through the class
<?php
Class Info
{
Public $code;
protected $name;
Private $sex;
function __construct ($s)
{
$this->sex = $s;
}
function Say ()
{
echo "Hello";
}
function Setsex ()
{
return $this->sex;
}
function Getsex ($s)
{
$this->sex = $s;
}
function __set ($a, $n)
{
$this $a = $n;
}
function __get ($a)
{
return $this $a;
}
}


Instantiating an Object
$r =new Info ("male");
$r->code = "Join";
$r->say ();
$r->sex; Error, because it is class-oriented
Var_dump ($R);

?>

Inheritance
Relative to the relationship between the father and the son, can associate with people
Concept: If there is a subclass then the subclass inherits everything from the parent class (the private member cannot access it).
Procedure: Requires a parent class and a subclass

You need to add a keyword when defining a subclass: Extends
Features: Single inheritance, a subclass can have only one parent class, Lenovo to people: Son can only have a father, but father can have more than one son
If there is a constructor in the parent class, the subclass is instantiated with the criteria of the parent class's constructor.
The parent represents Fred, can only be used in subclasses, integrated is info, write info is also possible, but it is best not to write the maintenance
Final: Used to modify a class, which means that the class is a final class that cannot be inherited, and is written on the parent class when used

<?php
Final class Info
Class Info
{
Public $code;
protected $name;
Private $sex;
function __construct ($s)
{
$this->sex = $s;
}
function Say ()
{
echo "Hello";
}
}
Class Test extends Info
{
Public $birthday;
function Run ()
{
echo "Running Man";
}
function Say ()
{
Parent::say ();//The parent class will also be called
echo "Hello";//Direct output will be overwritten
}
}
$t = new Test ("male");
$t->code = "P001";
echo $t->sex;//error, no access to
$t->run (); The output of a new method that is not in the parent class
$t->say (); The output of the methods in the parent class, but the parent class is overwritten.
Var_dump ($t);

?>

Static members
Ordinary members are represented on the object.
Ordinary members of a class belong to the object, not to the class (called when the object is called)
What is static: a static member belongs to a class, not to each object.
The static keyword is defined statically, and static members are defined using the Static keyword
Static methods can not call ordinary members, not with $this, but with self
Static methods can call static members but use the Self keyword
Self represents the class, $this represents the object

<?php
Class Info
{
public static $class; Static statically.
Public $code;
protected $name;
Private $sex;
static function Test ()//Call to Statics method
{
echo Info:: $class; Call a static member inside a class
echo "static method";
}
function __construct ($s)
{
$this->sex = $s;
}
function Say ()
{
echo Info:: $class; Static members can be called in normal methods
echo "Hello";
}
}
$r =new Info ("male");
echo Info:: $class = "0305"; accessing static members
Echo Info::test (); Static inside can not call ordinary members
$r->say ();

In other words: Static inside can not call ordinary, ordinary method inside can call static, static method inside can call static member

?>

Polymorphic:

The concept of polymorphism: When a parent class refers to a subclass instance, because the subclass overrides the parent class function, it causes us to use that reference to invoke the corresponding method to show the different

Condition: 1. Must have inheritance
2. A reference to the parent class points to an instance of the child class
3. Subclasses must override methods of the parent class

<?php
Class Ren
{
Public $name;
Public $sex;
function Say ()
{
echo "Hello";
}
}
Class China extends Ren
{
function Say ()
{
echo "Hello";
}
}
Class America extends Ren
{
{
echo "HI";
}
}

$r = new Ren (); $r represents the Ren class reference
$r 1 = new China (); $r 1 references for the China class

$r 2 =new America ();//$r 2 represents a reference to the America class

$r->say (); Hello
$r 1->say (); How are you doing

$r 2->say ();//Output hi


?>

In C # ren $r =new ren (); All that is used later is Ren's type, and subsequent assignments have no effect.
Ren $r = new China (); C # is also possible, is a reference to the sub-class
Another example: Ren $r = new America (); it will look like some methods in the output America class

Overloading of functions:
About overloading, which generally appears in strongly typed languages, does not exist in weakly typed languages, so it is only known that there are such definitions and will appear in some interview situations. So the concept of condition is a must remember content

If there are overloads in PHP: All of the following are assumed to exist
<?php
Class Ren
{
Public $name;
Public $sex;
function Say ()
{
echo "Hello";
}
function Say ($a)
{
echo $a. " Hello ";
}
function Say ($a, $b)
{
echo $a. $b. " Hello ";
}
}
$r = new Ren ();
$r->say (); According to the number of parameters written inside the output of different content

?>
Conditions for overloading implementation: 1) Function names must be the same (method name)
2) The number of arguments in the function is different (based on the same method name)
3) If the number of parameters is the same, the type is different
Eg:function Say (int, $a) and function Say (string, $b)
PHP is a weakly typed language so there is no overload: first PHP What type can be stored so that different types of conditions can not be satisfied,
PHP has a variable parameter function, so the number of different parameters this piece is difficult to achieve

The __tostring () method differs from C # in that it is converted to a string type in C #

<? Php
Class Ren
{
Public $name;
Public $sex;
function Say ()
{
echo "Hello";
}
function __tostring ()
{
Return "This object variable contains name and sex, and a Say method";
}
}

$r =new Ren (); Output of the __tostring usage
Echo $r;
?>

Clone of the object:

<?php

Class Ren
{
Public $name;
Public $sex;

function Say ()
{
echo "Hello";
}

function __clone ()//object cloning is performed automatically when
{
$this->name= "John Doe"; $this refers to a clone (a new object that is cloned)
}
}

$r =new Ren ();
$r->name = "Zhang San";
$r 1=clone $r;
Echo $r->name;
Echo $r 1->name;
Var_dump ($r 1);
?>
Cloning an object is equivalent to copying a copy to $R1, in the function method $this refers to the newly cloned object

April 28 inheritance, polymorphic, overloaded, clone

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.