?? ch11s04.html
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css"><!--@import url(stylesheet/text.css);@import url(stylesheet/box.css);--></style><title>簡明 Python 教程 / 面向對象的編程 / 對象的方法 </title></head><body><table width="100%"><tr><th colspan="3" align="center"><span class="header">簡明 Python 教程</span></th></tr><th colspan="3" align="center">第11章 面向對象的編程</th><tr><th width="20%" align="left"><a href="ch11s03.html">上一頁</a></th><th width="60%" align="center"><span class="header2">對象的方法</span></th><th align="right"><a href="ch11s05.html">下一頁</a></th></tr></table><hr noshade><h1>對象的方法</h1><p>我們已經討論了類/對象可以擁有像函數一樣的方法,這些方法與函數的區別只是一個額外的<code>self</code>變量?,F在我們來學習一個例子。</p><h2><a name="using">使用對象的方法</a></h2><p class="exampletitle"><a name="e112">例11.2 使用對象的方法</a></p><p class="filebox"><code class="comment">#!/usr/bin/python<br># Filename: method.py</code><br><br><code class="key">class </code><code class="func">Person</code><code>:</code><br><code class="key"> def </code><code class="func">sayHi</code><code>(self):</code><br><code class="key"> print </code><code class="cite">'Hello, how are you?'</code><br><br><code>p = Person()<br>p.sayHi()</code><br><br><code class="comment"># This short example can also be written as Person().sayHi()</code></p><p>(源文件:<a href="code/method.py">code/method.py</a>)</p><h2>輸出</h2><p class="codebox"><code>$ python method.py<br>Hello, how are you?</code></p><h2>它如何工作</h2><p>這里我們看到了<code>self</code>的用法。注意<code>sayHi</code>方法沒有任何參數,但仍然在函數定義時有<code>self</code>。</p><hr noshade><table width="100%"><tr><th width="20%" align="left"><a href="ch11s03.html">上一頁</a></th><th width="60%" align="center"><a href="ch11.html">上一級</a></th><th width="20%" align="right"><a href="ch11s05.html">下一頁</a></th></tr><tr><th width="20%" align="left">類</th><th width="60%" align="center"><a href="index.html">首頁</a></th><th align="right">__init__方法</th></tr></table></body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -