⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch11s05.html

📁 《简明 Python 教程》为 "A Byte of Python" 的唯一指定简体中文译本
💻 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 教程 / 面向对象的编程 / __init__方法 </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="ch11s04.html">上一页</a></th><th width="60%" align="center"><span class="header2">__init__方法</span></th><th align="right"><a href="ch11s06.html">下一页</a></th></tr></table><hr noshade><h1>__init__方法</h1><p>在Python的类中有很多方法的名字有特殊的重要意义。现在我们将学习<code>__init__</code>方法的意义。</p><p><code>__init__</code>方法在类的一个对象被建立时,马上运行。这个方法可以用来对你的对象做一些你希望的 <dfn>初始化</dfn> 。注意,这个名称的开始和结尾都是双下划线。</p><h2><a name="using">使用__init__方法</a></h2><p class="exampletitle"><a name="e113">例11.3 使用__init__方法</a></p><p class="filebox"><code class="comment">#!/usr/bin/python<br># Filename: class_init.py</code><br><br><code class="key">class </code><code class="func">Person</code><code>:</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;def </code><code class="func">__init__</code><code>(self, name):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.name = name</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;def </code><code class="func">sayHi</code><code>(self):</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </code><code class="cite">'Hello, my name is'</code><code>, self.name</code><br><br><code>p = Person(</code><code class="cite">'Swaroop'</code><code>)<br>p.sayHi()</code><br><br><code class="comment"># This short example can also be written as Person('Swaroop').sayHi()</code></p><p>(源文件:<a href="code/class_init.py">code/class_init.py</a>)</p><h2>输出</h2><p class="codebox"><code>$ python class_init.py<br>Hello, my name is Swaroop</code></p><h2>它如何工作</h2><p>这里,我们把<code>__init__</code>方法定义为取一个参数<code>name</code>(以及普通的参数<code>self</code>)。在这个<code>__init__</code>里,我们只是创建一个新的域,也称为<code>name</code>。注意它们是两个不同的变量,尽管它们有相同的名字。点号使我们能够区分它们。</p><p>最重要的是,我们没有专门调用<code>__init__</code>方法,只是在创建一个类的新实例的时候,把参数包括在圆括号内跟在类名后面,从而传递给<code>__init__</code>方法。这是这种方法的重要之处。</p><p>现在,我们能够在我们的方法中使用<code>self.name</code>域。这在<code>sayHi</code>方法中得到了验证。</p><p class="notebox"><span class="boxtitle">给C++/Java/C#程序员的注释</span><br><code>__init__</code>方法类似于C++、C#和Java中的 <dfn>constructor</dfn> 。</p><hr noshade><table width="100%"><tr><th width="20%" align="left"><a href="ch11s04.html">上一页</a></th><th width="60%" align="center"><a href="ch11.html">上一级</a></th><th width="20%" align="right"><a href="ch11s06.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">类与对象的变量</th></tr></table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -