page117.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 62 行
HTML
62 行
<HTML><HEAD><TITLE>Abstract Methods</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="tex2html2562" HREF="page118.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2560" HREF="page115.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2556" HREF="page116.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2564" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION005222000000000000000">Abstract Methods</A></H3><P>The <tt>_compareTo</tt> method given in Program <A HREF="page116.html#progobjecta"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> is an abstract method.In particular, the body of the method does nothing.It is expected that the <tt>_compareTo</tt> method will be overriddenin a class derived from the <tt>Object</tt> class.However, this is only a convention--the Python language doesnothing to ensure that the method is overridden.And if the method is not overridden,it remains possible for a Python program to call the abstract method.<P>To prevent the calling of abstract methods,we introduce the <tt>abstractmethod</tt><em>descriptor</em><A NAME=4459> </A> classshown in Program <A HREF="page117.html#progabstractmethoda"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.The idea is to wrap a function objectthat is supposed to be an abstract methodin an instance of the <tt>abstractmethod</tt> classas shown on line 17 of Program <A HREF="page116.html#progobjecta"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P><P><A NAME="4940"> </A><A NAME="progabstractmethoda"> </A> <IMG WIDTH=575 HEIGHT=412 ALIGN=BOTTOM ALT="program4463" SRC="img646.gif" ><BR><STRONG>Program:</STRONG> <tt>abstractmethod</tt> class definition.<BR><P><P>The <tt>abstractmethod</tt> class does its magicby defining a special <tt>__get__</tt> method.Python uses the <tt>__get__</tt> method of a classto bind a method to an instance.For example,suppose you have an instance <tt>obj</tt> of some class <tt>Cls</tt>.When you call a method like this:<PRE>obj.func(*args)</PRE>Python effectively does this:<PRE>meth = __get__(func, obj, Cls)meth.__call__(*args)</PRE><P>In Program <A HREF="page117.html#progabstractmethoda"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,we implement a <tt>__get__</tt> methodthat returns an instance ofthe nested <tt>abstractmethod.method</tt> class.This class in turn implements a <tt>__call__</tt> methodthat raises a <tt>TypeError</tt> exception.The effect of this is that whenever an abstract method is calledan exception is raised.<P><HR><A NAME="tex2html2562" HREF="page118.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2560" HREF="page115.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2556" HREF="page116.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2564" 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 © 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 + -
显示快捷键?