page112.html

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

HTML
110
字号
<HTML><HEAD><TITLE>Abstract Data Types</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="tex2html2500" HREF="page113.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2498" HREF="page111.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2492" HREF="page111.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2502" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION005100000000000000000">Abstract Data Types</A></H1><P>An object in a programming language such asC++<A NAME=4262>&#160;</A>,Java<A NAME=4263>&#160;</A>,and Python<A NAME=4264>&#160;</A>,is an abstraction.The abstraction comprises a number of <em>attributes</em><A NAME=4266>&#160;</A>--name<A NAME=4267>&#160;</A>, address<A NAME=4268>&#160;</A>,value<A NAME=4269>&#160;</A>, lifetime<A NAME=4270>&#160;</A>,scope<A NAME=4271>&#160;</A>, type<A NAME=4272>&#160;</A>, and size<A NAME=4273>&#160;</A>.Each attribute has an associated value.For example, if we create an integer object in Python, <tt>x = int(5)</tt>,we say that the name attribute has value ``<tt>x</tt>''and that the type attribute has value ``<tt>int</tt>''.<P>Unfortunately, the terminology can be somewhat confusing:The word ``value'' has two different meanings--in one instance it denotes one of the attributesand in the other it denotes the quantity assigned to an attribute.For example, after the statement <tt>x&nbsp;=&nbsp;int(5)</tt>,the <em>value attribute</em> of the object named <tt>x</tt>has the <em>value</em> five.<P>The <em>name</em><A NAME=4282>&#160;</A> of an object is the textual labelused to refer to that object in the text of the source program.The <em>address</em><A NAME=4284>&#160;</A> of an object denotesis location in memory.The <em>value</em> attribute is thequantity which that object represents.The <em>lifetime</em><A NAME=4287>&#160;</A> of an object is the interval of timeduring the execution of the program in which the object is said to exist.The <em>scope</em><A NAME=4289>&#160;</A> of an object is theset of statements in the text of the source programin which the object is said to be <em>visible</em><A NAME=4291>&#160;</A>.The <em>type</em> of an object denotes the set of values whichcan be assigned to the <em>value</em> attributeand the set of operations which can be performed on the object.Finally, the <em>size</em> attribute denotes the amount of storagerequired to represent the object.<P>The process of assigning a value to an attributeis called <em>binding</em><A NAME=4296>&#160;</A>.When a value is assigned to an attribute,that attribute is said to be <em>bound</em><A NAME=4298>&#160;</A> to the value.Depending on the semantics of the programming language,and on the attribute in question,the binding may be done statically by the compileror dynamically at run-time.For example, in Python the <em>type</em> of an object is usuallydetermined at compile time--<em>static binding</em><A NAME=4301>&#160;</A>.On the other hand, the <em>value</em> of an object is usually notdetermined until run-time--<em>dynamic binding</em><A NAME=4304>&#160;</A>.<P>In this chapter we are concerned primarily withthe <em>type</em> attribute of an object.The type of an object specifies two sets:<UL><LI> a set of values; and,<LI> a set of operations.</UL>For example, when we create an object, say <tt>x</tt>,  of type <tt>int</tt>,we know that <tt>x</tt> can represent an integer in the range  <IMG WIDTH=96 HEIGHT=28 ALIGN=MIDDLE ALT="tex2html_wrap_inline60659" SRC="img640.gif"  >and that we can perform operations using <tt>x</tt>such as addition, subtraction, multiplication, and division.<P>The type <tt>int</tt> is an <em>abstract data type</em><A NAME=4316>&#160;</A>in the sense that we can think about the qualities of an <tt>int</tt>apart from any real thing having that quality.In other words, we don't need to know <em>how</em> <tt>int</tt>s arerepresented nor how the operations are implementedto be able to be able to use them or reason about them.<P>In designing <em>object-oriented</em><A NAME=4321>&#160;</A>programs,one of the primary concerns of the programmer isto develop an appropriate collection of abstractionsfor the application at hand,and then to define suitable abstract data typesto represent those abstractions.In so doing, the programmer must be conscious of the factthat defining an abstract data typerequires the specification of <em>both</em>a set of values and a set of operations on those values.<P>Indeed, it has been only since the advent of the so-called<em>object-oriented programming languages</em><A NAME=4324>&#160;</A>that the we see programming languageswhich provide the necessary constructs to properlydeclare abstract data types.For example, in Python, the <tt>class</tt> construct is the meansby which both a set of values and an associated set of operations is declared.Compare this with the <tt>struct</tt> construct of Cor Pascal's <tt>record</tt><A NAME=4328>&#160;</A>,which only allow the specification of a set of values!<P><HR><A NAME="tex2html2500" HREF="page113.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2498" HREF="page111.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2492" HREF="page111.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2502" 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 + -
显示快捷键?