|
| 1 | +# python 方法链 method chaining |
| 2 | + |
| 3 | +## 写这篇文章来由,有朋友说这样的代码看不懂。 |
| 4 | +choice = raw_input("please input:\n").strip()[0].lower() |
| 5 | +很多对于有经验的程序员来说,这些都不是事, |
| 6 | +但对于初学者来说,看到这样的语法头有点大。 |
| 7 | + |
| 8 | +## 这个其实是面向对象中方法链的概念。 |
| 9 | +## 请看维基百科上Method chaining的定义 |
| 10 | + |
| 11 | + Method chaining, also known as named parameter idiom, |
| 12 | + is a common syntax for invoking multiple method calls |
| 13 | + in object-oriented programming languages. |
| 14 | + Each method returns an object, allowing the calls |
| 15 | + to be chained together in a single statement without requiring |
| 16 | + variables to store the intermediate results. |
| 17 | + Local variable declarations are syntactic |
| 18 | + sugar because of the difficulty humans have with deeply nested method calls. |
| 19 | + A method chain is also known as a train wreck due to the increase |
| 20 | + in the number of methods that come one after another in the same |
| 21 | + line that occurs as more methods are chained together |
| 22 | + even though line breaks are often added between methods. |
| 23 | + |
| 24 | +## 具体在python中,请看黄哥的分析: |
| 25 | + |
| 26 | + 有的python初学者对python方法连续调用不是很清楚,像雾里看花一样。 |
| 27 | + python一切都是对象,对象调用它的方法,如果带返回值,放回值也是对象,这个返回值也有方法,当然就可以用点号调用它的方法,如此下去,就是python方法链调用也。 |
| 28 | + |
| 29 | +## 如何设计方法链python代码 |
| 30 | + |
| 31 | + |
| 32 | + # coding:utf-8 |
| 33 | + """ |
| 34 | + 如何通过学习python学会编程 |
| 35 | + https://github.com/pythonpeixun/article/blob/master/python/how_to_learn_python.md |
| 36 | + 黄哥python远程视频培训班 |
| 37 | + https://github.com/pythonpeixun/article/blob/master/index.md |
| 38 | + 黄哥python培训试看视频播放地址 |
| 39 | + https://github.com/pythonpeixun/article/blob/master/python_shiping.md |
| 40 | + 黄哥python培训 咨询qq:1465376564 |
| 41 | + """ |
| 42 | + |
| 43 | + |
| 44 | + class Person(object): |
| 45 | + """方法链小sample""" |
| 46 | + |
| 47 | + def name(self, value): |
| 48 | + self.name = value |
| 49 | + return self # 返回实例对象自己才能再调用实例对象的方法。 |
| 50 | + |
| 51 | + def work(self, value): |
| 52 | + self.working = value |
| 53 | + return self |
| 54 | + |
| 55 | + def introduce(self): |
| 56 | + print "你好, 我的名字:", self.name, ",我的工作:", self.working, ",教初学者学会编程!" |
| 57 | + |
| 58 | + person = Person() |
| 59 | + person.name("黄哥").work("黄哥python培训").introduce() |
| 60 | + |
| 61 | + |
| 62 | +## php方法链代码 |
| 63 | + |
| 64 | + <?php |
| 65 | + /* |
| 66 | + 黄哥php培训 咨询qq:1465376564 |
| 67 | + https://github.com/pythonpeixun/article/blob/master/php_education.md |
| 68 | + */ |
| 69 | + |
| 70 | + |
| 71 | + class Person{ |
| 72 | + public $name; |
| 73 | + public $working; |
| 74 | + |
| 75 | + public function setName($value){ |
| 76 | + $this->name = $value; |
| 77 | + return $this; |
| 78 | + } |
| 79 | + |
| 80 | + public function work($value){ |
| 81 | + $this->working = $value; |
| 82 | + return $this; |
| 83 | + } |
| 84 | + |
| 85 | + public function introduce(){ |
| 86 | + echo "你好, 我的名字:".$this->name.",我的工作:".$this->working.",教初学者学会编程!\n"; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + $person = new Person(); |
| 91 | + $person->setName("黄哥")->work("黄哥php培训")->introduce(); |
| 92 | + |
| 93 | + |
| 94 | +[点击黄哥python培训试看视频播放地址](https://github.com/pythonpeixun/article/blob/master/python_shiping.md) |
| 95 | + |
| 96 | +[黄哥python远程视频培训班](https://github.com/pythonpeixun/article/blob/master/index.md) |
| 97 | + |
0 commit comments