<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//CN">
<html>
<head>
<link rel="STYLESHEET" href="style.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="tut.html" title='Python Tutorial' />
<link rel='contents' href='node2.html' title="Contents" />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="node12.html" />
<link rel="prev" href="node10.html" />
<link rel="parent" href="tut.html" />
<link rel="next" href="node12.html" />
<meta name='aesop' content='information' />
<title>9. 类 Classes </title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="8. 错误和异常 Errors and"
href="node10.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="Python Tutorial"
href="tut.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="10. 标准库概览 Brief Tour"
href="node12.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%" class='online-navigation'>Python Tutorial</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="node2.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node10.html">8. 错误和异常 Errors and</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="tut.html">Python Tutorial</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node12.html">10. 标准库概览 Brief Tour</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<div class='online-navigation'>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
<UL CLASS="ChildLinks">
<LI><A href="node11.html#SECTION0011100000000000000000">9.1 有关术语的话题 A Word About Terminology</a>
<LI><A href="node11.html#SECTION0011200000000000000000">9.2 Python 作用域和命名空间 Python Scopes and Name Spaces</a>
<LI><A href="node11.html#SECTION0011300000000000000000">9.3 初识类 A First Look at Classes</a>
<UL>
<LI><A href="node11.html#SECTION0011310000000000000000">9.3.1 类定义语法 Class Definition Syntax</a>
<LI><A href="node11.html#SECTION0011320000000000000000">9.3.2 类对象 Class Objects</a>
<LI><A href="node11.html#SECTION0011330000000000000000">9.3.3 实例对象 Instance Objects</a>
<LI><A href="node11.html#SECTION0011340000000000000000">9.3.4 方法对象 Method Objects</a>
</ul>
<LI><A href="node11.html#SECTION0011400000000000000000">9.4 一些说明 Random Remarks</a>
<LI><A href="node11.html#SECTION0011500000000000000000">9.5 继承 Inheritance</a>
<UL>
<LI><A href="node11.html#SECTION0011510000000000000000">9.5.1 多继承 Multiple Inheritance</a>
</ul>
<LI><A href="node11.html#SECTION0011600000000000000000">9.6 私有变量 Private Variables</a>
<LI><A href="node11.html#SECTION0011700000000000000000">9.7 补充 Odds and Ends</a>
<LI><A href="node11.html#SECTION0011800000000000000000">9.8 异常也是类 Exceptions Are Classes Too</a>
<LI><A href="node11.html#SECTION0011900000000000000000">9.9 迭代器 Iterators</a>
<LI><A href="node11.html#SECTION00111000000000000000000">9.10 生成器 Generators</a>
</ul>
<!--End of Table of Child-Links-->
</div>
<HR>
<H1><A NAME="SECTION0011000000000000000000"></A><A NAME="classes"></A>
<BR>
9. 类 Classes
</H1>
<P>
Python's class mechanism adds classes to the language with a minimum
of new syntax and semantics. It is a mixture of the class mechanisms
found in C++ and Modula-3. As is true for modules, classes in Python
do not put an absolute barrier between definition and user, but rather
rely on the politeness of the user not to ``break into the
definition.'' The most important features of classes are retained
with full power, however: the class inheritance mechanism allows
multiple base classes, a derived class can override any methods of its
base class or classes, a method can call the method of a base class with the
same name. Objects can contain an arbitrary amount of private data.
<P>
Python 在尽可能不增加新的语法和语义的情况下加入了类机制。这种机制是 C++ 和 Modula-3 的混合。Python中的类没有在用户和定义之间建立一个绝对的屏障,而是依赖于用户自觉的不去“破坏定义”。然而,类机制最重要的功能都完整的保留下来。类继承机制允许多继承,派生类可以覆盖(override)基类中的任何方法,方法中可以调用基类中的同名方法。对象可以包含任意数量的私有成员。
<P>
In C++ terminology, all class members (including the data members) are
<em>public</em>, and all member functions are <em>virtual</em>. There are
no special constructors or destructors. As in Modula-3, there are no
shorthands for referencing the object's members from its methods: the
method function is declared with an explicit first argument
representing the object, which is provided implicitly by the call. As
in Smalltalk, classes themselves are objects, albeit in the wider
sense of the word: in Python, all data types are objects. This
provides semantics for importing and renaming. Unlike
C++ and Modula-3, built-in types can be used as base classes for
extension by the user. Also, like in C++ but unlike in Modula-3, most
built-in operators with special syntax (arithmetic operators,
subscripting etc.) can be redefined for class instances.
<P>
用 C++ 术语来讲,所有的类成员(包括数据成员)都是公有( <em>public</em> )的,所有的成员函数都是虚拟( <em>virtual</em> )的。没有特定的构造和析构函数。用Modula-3的术语来讲,在成员方法中没有什么简便的方式(shorthands)可以引用对象的成员:方法函数在定义时需要以引用的对象做为第一个参数,调用时则会隐式引用对象。这样就形成了语义上的引入和重命名。( This provides semantics for importing and renaming. )但是,像 C++ 而非 Modula-3 中那样,大多数带有特殊语法的内置操作符(算法运算符、下标等)都可以针对类的需要重新定义。
<P>
<H1><A NAME="SECTION0011100000000000000000"></A><A NAME="terminology"></A>
<BR>
9.1 有关术语的话题 A Word About Terminology
</H1>
<P>
Lacking universally accepted terminology to talk about classes, I will
make occasional use of Smalltalk and C++ terms. (I would use Modula-3
terms, since its object-oriented semantics are closer to those of
Python than C++, but I expect that few readers have heard of it.)
<P>
由于没有什么关于类的通用术语,我从 Smalltalk 和 C++ 中借用一些(我更希望用 Modula-3 的,因为它的面向对象机制比 C++更接近Python,不过我想没多少读者听说过它)。
<P>
I also have to warn you that there's a terminological pitfall for
object-oriented readers: the word ``object'' in Python does not
necessarily mean a class instance. Like C++ and Modula-3, and
unlike Smalltalk, not all types in Python are classes: the basic
built-in types like integers and lists are not, and even somewhat more
exotic types like files aren't. However, <em>all</em> Python types
share a little bit of common semantics that is best described by using
the word object.
<P>
我要提醒读者,这里有一个面向对象方面的术语陷阱,在 Python 中“对象”这个词不一定指类实例。Python 中并非所有的类型都是类:例如整型、链表这些内置数据类型就不是,甚至某些像文件这样的外部类型也不是,这一点类似于 C++ 和 Modula-3,而不像 Smalltalk。然而,所有的 Python 类型在语义上都有一点相同之处:描述它们的最贴切词语是�

dhmj2ee
- 粉丝: 17
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


