page598.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 60 行

HTML
60
字号
<HTML><HEAD><TITLE>Nested Classes</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html8021" HREF="page599.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html8019" HREF="page585.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html8013" HREF="page597.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html8023" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION0017500000000000000000">Nested Classes</A></H1><P>In Python it is possible to define one class <em>inside</em> another.A class defined inside another one is calleda <em>nested class</em><A NAME=57133>&#160;</A>.<P>Consider the following Python code fragment:<PRE>class A(object):    def __init__(self):	self.y = 0    class B(object):	def __init__(self):	    self.x = 0	def f(self):	    pass</PRE>This fragment defines the class <tt>A</tt> which containsthe nested class <tt>B</tt>.<P>A nested class behaves like any ``outer'' (un-nested) class.It may contain methods and instance attributes,and it may be instantiated like this:<PRE>obj = A.B()</PRE>This statement creates an new instance of the nested class <tt>B</tt>.Given such an instance,we can invoke the <tt>f</tt> method in the usual way:<PRE>obj.f()</PRE><P>Note, it is not necessarily the case that an instanceof the outer class <tt>A</tt> exists even when we have createdan instance of the inner class.Similarly, instantiating the outer class <tt>A</tt>does not create any instances of the inner class <tt>B</tt>.<P>The methods of a nested class may access the instance attributesof the nested class instancebut not of any outer class instanceThus, method <tt>f</tt> can access the instance attribute <tt>x</tt>,but it cannot access the instance attribute <tt>y</tt>.<P><HR><A NAME="tex2html8021" HREF="page599.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html8019" HREF="page585.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html8013" HREF="page597.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html8023" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright &#169; 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

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