zoukankan      html  css  js  c++  java
  • php继承多态

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    
    <body>
    
    <?php
    
    /*
    class Ren
    {
        public $name;
        
        function Say()
        {
            echo $this->name."正在讲话";
        }
    }
    
    class China extends Ren
    {
        //子类对父类的方法进行重写
        function Say()
        {
            parent::Say();
            echo "你好";
        }
        
        function Run()
        {
            echo $this->name."正在跑步";
        }
    }
    
    class America extends Ren
    {
        //子类对父类的方法进行重写
        function Say()
        {
            echo "hello";
        }
    }*/
    
    /*$c = new China();
    $c->Say();
    $a = new America();
    $a->Say();
    var_dump($c);
    */
    
    //overload 重载
    //可以使类里面的某个方法产生多种效果,根据传入的参数不同,可以执行不同的逻辑
    //也是多态的一种,编译多态
     /*  class Test
        {
            public string show()
            {
                return "0参";
            }
            public string show(string s)
            {
                return "1参";
            }
            public string show(string s,string a)
            {
                return "2参";
            }
        }
        
         Test t = new Test();
    
         t.show("a","b");
    */
    
    
    
    //父类
      /*  class Ren
        {
            public virtual string Say()
            {
                return "说话";
            }
        }
    
        //子类
        class China : Ren
        {
            public override string Say()
            {
                return "你好";
            }
        }
        //子类
        class America : Ren
        {
            public override string Say()
            {
                return "hello";
            }
        }
        
         //父类引用r指向子类实例
        Ren r = new America();
        MessageBox.Show(r.Say());
    
        r = new China();
        MessageBox.Show(r.Say());
        
        
        //子类的对象可以代替父类的对象
         America a = new America();
         MessageBox.Show(Eat(a));
          //怪兽吃人,需要一个人的参数
            public string Eat(Ren r)
            {
                return "这个人味道不错!";
            }
    */
    
    
    
    
    
    
    
    
    
    
    ?>
    
    
    
    </body>
    </html>
    
    1.封装
    目的:为了使类更加安全
    做法:
    1.将成员变量变为私有的
    2.在类里面做一个方法来间接的访问成员变量
    3.在该方法里面加控制
    
    2.继承
    1.父类
    2.子类
    子类可以继承父类的一切
    重写:override
    特点:单继承,一个子类只能有一个父类,一个父类可以派生多个子类
    3.多态
    当父类引用指向子类实例的时候,由于子类对父类的方法进行了重写,父类引用在调用该方法的时候表现出的不同,称为多态
    运行多态
    
    条件
    1.要有继承
    2.父类引用指向子类实例
    3.要有重写
    4.调重写的方法
    
    
  • 相关阅读:
    加入创业公司有什么利弊
    Find Minimum in Rotated Sorted Array II
    Search in Rotated Sorted Array II
    Search in Rotated Sorted Array
    Find Minimum in Rotated Sorted Array
    Remove Duplicates from Sorted Array
    Spiral Matrix
    Spiral Matrix II
    Symmetric Tree
    Rotate Image
  • 原文地址:https://www.cnblogs.com/qishuang/p/6170798.html
Copyright © 2011-2022 走看看