文章出處

<!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.調重寫的方法

文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()