<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>9. Classes 类 — Python v3.1 documentation</title>
<link rel="stylesheet" href="./_static/default.css" type="text/css" />
<link rel="stylesheet" href="./_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '3.1',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="./_static/jquery.js"></script>
<script type="text/javascript" src="./_static/doctools.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python v3.1 documentation"
href="./_static/opensearch.xml"/>
<link rel="author" title="About these documents" href="../about.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="top" title="Python v3.1 documentation" href="../index.html" />
<link rel="up" title="The Python Tutorial Python 入门指南" href="index.html" />
<link rel="next" title="10. Brief Tour of the Standard Library Python标准库概览" href="stdlib.html" />
<link rel="prev" title="8. Errors and Exceptions 错误和异常" href="errors.html" />
<link rel="shortcut icon" type="image/png" href="./_static/py.png" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../modindex.html" title="Global Module Index"
accesskey="M">modules</a> |</li>
<li class="right" >
<a href="stdlib.html" title="10. Brief Tour of the Standard Library Python标准库概览"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="errors.html" title="8. Errors and Exceptions 错误和异常"
accesskey="P">previous</a> |</li>
<li><img src="./_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="../index.html">Python v3.1 documentation</a> »</li>
<li><a href="index.html" accesskey="U">The Python Tutorial Python 入门指南</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="classes">
<span id="tut-classes"></span><h1>9. Classes 类<a class="headerlink" href="#classes" title="Permalink to this headline">¶</a></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, and a method can call the method of a base class with the same
name. Objects can contain an arbitrary amount of private data.</p>
<p>Python的类机制通过最小的新语法和语义在语言中实现了类。
它是C++何Modula-3语言中类机制的混合。
就像模块一样,Python的类并没有在用户和定义之间设立绝对的屏障,而是依赖于用户不去“强行闯入定义”的优雅。
另一方面,类的大多数重要特性都被完整的保留下来:类继承机制允许多重继承,派生类可以覆盖(override)基类中的任何方法或类,可以使用相同的方法名称调用基类的方法。
对象可以包含任意数量的私有数据。</p>
<p>In C++ terminology, normally class members (including the data members) are
<em>public</em> (except see below <a class="reference internal" href="#tut-private"><em>Private Variables 私有变量</em></a>),
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>
<p>用在C++中的术语讲,普通的类成员(包括数据成员)都是 <em>公有</em> 的(public)(除了下面提到的 <a class="reference internal" href="#tut-private"><em>Private Variables 私有变量</em></a> ),并且所有的成员函数都是 <em>虚</em> 的(virtual)。
类并没有特殊的构造器和析构器。
和在Modula-3中一样,从方法中没有什么简洁的方式可以引用其对象成员,函数方法必须以代表对象的标识符(self)作为第一个明确的参数,在调用时被隐式的提供。
和在Smalltalk中一样,类本身就是对象,从更广发的意义上讲:在Python中,所有的数据类型都是对象。
这就为导入和重命名提供了支持。
不似C++和Modula-3那样,内置类型可以被用户用作基类进行扩展。
并且像在C++中一样,而不是Modula-3,所有内置带有特殊语法的操作符(算术操作符,下标操作符等)都可以针对类的实例进行重定义。</p>
<div class="section" id="a-word-about-terminology">
<span id="tut-terminology"></span><h2>9.1. A Word About Terminology 术语相关<a class="headerlink" href="#a-word-about-terminology" title="Permalink to this headline">¶</a></h2>
<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>
<p>关于类因为缺少普遍的可以接受的术语,我暂时借用Smalltalk和C++中的术语。
(我更想使用Modula-3的术语,因为它的面向对象机制比C++更接近Python,但我想几乎没人听说过它)。</p>
<p>Objects have individuality, and multiple names (in multiple scopes) can be bound
to the same object. This is known as aliasing in other languages. This is
usually not appreciated on a first glance at Python, and can be safely ignored
when dealing with immutable basic types (numbers, strings, tuples). However,
aliasing has an (intended!) effect on the semantics of Python code involving
mutable objects such as lists, dictionaries, and most types representing
entities outside the program (files, windows, etc.). This is usually used to
the benefit of the program, since aliases behave like pointers in some respects.
For example, passing an object is cheap since only a pointer is passed by the
implementation; and if a function modifies an object passed as an argument, the
caller will see the change — this eliminates the need for two different
argument passing mechanisms as in Pascal.</p>
<p>对象具有特性,并且多个名称(在多个作用于中)可以绑
- 1
- 2
前往页