跟黃哥學(xué)python序列文章之python方法鏈(method chaining)

跟黃哥學(xué)python序列文章之python方法鏈(method chaining)

寫這篇文章來由,有朋友說下面這樣的代碼看不懂。

choice = raw_input("please input:\n").strip()[0].lower()
很多對于有經(jīng)驗的程序員來說,這些都不是事,
但對于初學(xué)者來說,看到這樣的語法頭有點大。

這個其實是面向?qū)ο笾蟹椒ㄦ湹母拍睢?/h2>

請看維基百科上Method chaining的定義

    Method chaining, also known as named parameter idiom,   
    is a common syntax for invoking   multiple method calls 
    in object-oriented programming languages.
    Each method returns an   object, allowing the calls 
    to be chained together in a single statement without requiring   
    variables to store the intermediate results.
    Local variable declarations are syntactic   
    sugar because of the difficulty humans have with deeply nested method calls.
    A method chain is also known as a train wreck due to the increase 
    in the number of methods that come one after another in the same 
    line that occurs as more methods are chained together
    even though line breaks are often added between methods.

具體在python中,請看黃哥的分析:

有的python初學(xué)者對python方法連續(xù)調(diào)用不是很清楚,像霧里看花一樣。
python一切都是對象,對象調(diào)用它的方法,如果帶返回值,放回值也是對象,  
這個返回值也有方法,當(dāng)然就可以用點號調(diào)用它的方法,   
如此下去,就是python方法鏈調(diào)用也。

如何設(shè)計方法鏈python代碼

# coding:utf-8
"""
如何通過學(xué)習(xí)python學(xué)會編程
https://github.com/pythonpeixun/article/blob/master/python/how_to_learn_python.md
黃哥python遠(yuǎn)程視頻培訓(xùn)班
https://github.com/pythonpeixun/article/blob/master/index.md
黃哥python培訓(xùn)試看視頻播放地址
https://github.com/pythonpeixun/article/blob/master/python_shiping.md
黃哥python培訓(xùn) 咨詢qq:1465376564
"""


class Person(object):
    """方法鏈小sample"""

    def name(self, value):
        self.name = value
        return self # 返回實例對象自己才能再調(diào)用實例對象的方法。

    def work(self, value):
        self.working = value
        return self

    def introduce(self):
        print "你好, 我的名字:", self.name, ",我的工作:", self.working, ",教初學(xué)者學(xué)會編程!"

person = Person()
person.name("黃哥").work("黃哥python培訓(xùn)").introduce()

php方法鏈代碼

    <?php
    /*
    黃哥php培訓(xùn) 咨詢qq:1465376564
    https://github.com/pythonpeixun/article/blob/master/php_education.md
    */


    class Person{
        public $name;
        public $working;

        public function setName($value){
            $this->name = $value;
            return $this;
        }

        public function work($value){
            $this->working = $value;
            return $this;
        }

        public function introduce(){
            echo "你好, 我的名字:".$this->name.",我的工作:".$this->working.",教初學(xué)者學(xué)會編程!\n";
        }
    }

    $person = new Person();
    $person->setName("黃哥")->work("黃哥php培訓(xùn)")->introduce();

點擊黃哥python培訓(xùn)試看視頻播放地址

黃哥python遠(yuǎn)程視頻培訓(xùn)班

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,777評論 25 709
  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,030評論 0 9
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,060評論 4 61
  • 雷咖閱讀 164評論 0 0

友情鏈接更多精彩內(nèi)容