⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csdn_文档中心_oo三步曲之浅析oo的基石.htm

📁 csdn10年中间经典帖子
💻 HTM
📖 第 1 页 / 共 5 页
字号:
            lang=EN-US>OOP</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的内涵。</SPAN></P>
            <P class=MsoNormal><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
            <P class=MsoNormal><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
            <P class=MsoNormal><B><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">什么是类型(</SPAN><SPAN 
            lang=EN-US>type</SPAN></B><B><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)</SPAN><SPAN 
            lang=EN-US>?<o:p></o:p></SPAN></B></P>
            <P class=MsoNormal><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
            <P class=MsoNormal><SPAN lang=EN-US><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN></SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类型以及类型系统的起源以及研究与发展是独立于</SPAN><SPAN 
            lang=EN-US>OOP</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的。早在五十年代的</SPAN><SPAN 
            lang=EN-US>FORTRAN</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语言编译器实现中,就已经采用类型系统作为类型检查的一种手段。广义的类型一般被定义为一种约束,也就是一种逻辑公式。而在对类型的研究过程中产生多种方法,比如</SPAN><SPAN 
            lang=EN-US>[C&amp;W 1985]</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">等。而代数方法(</SPAN><SPAN 
            lang=EN-US>algebraic approach</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)是一种非常好的建立类型的形式化规范的方法。代数中的一个类型对应于一系列元素,在它们之上定义代数操作。同时在此基础上二阶</SPAN><SPAN 
            style="FONT-FAMILY: 宋体">λ</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">演算已经被用于继承和模板所支持的模型。在上面两种方法中,类型被认为是一系列满足确定约束条件的元素,更抽象的方式可以把一个类型当作规定一个约束条件,如果我们规定的约束条件越好,相对应的被定义元素的集合就越精密,所以逻辑公式(</SPAN><SPAN 
            lang=EN-US>logical formulas</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)就成为描叙类型特征的最合适工具。在这里,我们不想深入的探究对于类型理论的各种不同的数学模型,我们需要明白的是类型(</SPAN><SPAN 
            lang=EN-US>type</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)以及类型理论这个在编程语言中经常应用到的概念的内涵是极其丰富的,而其自身理论的发展并非局限于</SPAN><SPAN 
            lang=EN-US>OOP</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">之中,但当两者相结合的时候就对我们的程序观产生了巨大的影响。</SPAN></P>
            <P class=MsoNormal><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
            <P class=MsoNormal><B><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类(</SPAN><SPAN 
            lang=EN-US>class</SPAN></B><B><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">),类型</SPAN><SPAN 
            lang=EN-US>(type)</SPAN></B><B><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,接口(</SPAN><SPAN 
            lang=EN-US>interface</SPAN></B><B><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)</SPAN><SPAN 
            lang=EN-US><o:p></o:p></SPAN></B></P>
            <P class=MsoNormal><B><SPAN 
            lang=EN-US>&nbsp;<o:p></o:p></SPAN></B></P>
            <P class=MsoNormal><SPAN lang=EN-US><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN></SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这三个概念是在</SPAN><SPAN 
            lang=EN-US>OOP</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中出现频率最多,也最容易混淆的。而对于这三个概念的澄清也是文章写作的初衷。让我们先看看大师们对于这三个概念的描叙</SPAN><SPAN 
            lang=EN-US>----</SPAN></P>
            <P class=MsoNormal style="TEXT-INDENT: 21pt"><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">“</SPAN><SPAN 
            lang=EN-US>The fundamental unit of programming in Java programming 
            language is the <B>class</B>, but the fundamental unit of the 
            object-oriented design is the <B>type</B>.while <B>classes</B> 
            define <B>types</B>,it is very useful and powerful to be able to 
            define a <B>type</B> without defining a 
            <B>class</B>.<B>Interface</B> define <B>types</B> in an abstract 
            form as a collection of methods or other <B>types</B> that form the 
            <B>contract</B> for the<B> type</B>.</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”</SPAN> 
            <SPAN lang=EN-US>[Jams 2000]</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN><SPAN 
            lang=EN-US> </SPAN></P>
            <P class=MsoNormal style="TEXT-INDENT: 21pt"><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">“</SPAN><SPAN 
            lang=EN-US>In C++,A <B>class</B> is a user definite 
            <B>type</B></SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”</SPAN><SPAN 
            lang=EN-US>[B.S 1998]</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
            <P class=MsoNormal style="TEXT-INDENT: 21pt"><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">“</SPAN><SPAN 
            lang=EN-US>A <B>type</B> is a name used to denote a particular 
            <B>interface</B>……An object may have many <B>types</B>,and widely 
            different objects can share a <B>type</B>.Part of an object’s 
            <B>interface</B> may be characterized by one <B>type</B> ,and other 
            parts by other <B>types</B>.Two objects of the same<B> type</B> need 
            only share parts of their i<B>nterface</B>.<B>Interface</B> can 
            contain other <B>interface</B> as subset.We say that a <B>type</B> 
            is a <B>subtype</B> of another if its <B>interface</B> contain the 
            <B>interface</B> of its <B>supertype</B>.Often we speak of a 
            <B>subtype</B> inheriting the <B>interface </B>of its 
            <B>supertype</B></SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”</SPAN><SPAN 
            lang=EN-US>[Gamma 1995]</SPAN></P>
            <P class=MsoNormal><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
            <P class=MsoNormal style="TEXT-INDENT: 21pt"><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在其中,一共出现了四个概念:类(</SPAN><SPAN 
            lang=EN-US>class</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">),类型</SPAN><SPAN 
            lang=EN-US>(type)</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,接口(</SPAN><SPAN 
            lang=EN-US>interface</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)以及契约(</SPAN><SPAN 
            lang=EN-US>contract</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)。这里我们说到的类型和上面提到的类型有所不同,是狭义的</SPAN><SPAN 
            lang=EN-US>OOP</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的类型。为了理解这几个概念,我先划分出三个概念域:一个是针对现实世界的,一个是针对特定程序设计范型的(在这里就是</SPAN><SPAN 
            lang=EN-US>OO</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设计范型),最后一个是针对编译器实现的。也就是说,在现实世界中的概念必须有一种手段映射到</SPAN><SPAN 
            lang=EN-US>OO</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">范型中去,而</SPAN><SPAN 
            lang=EN-US>OO</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">范型中的概念也应该在编译器实现中有相同的概念对应。由此,我们可以这样说,类是做为现实世界中的概念,而传统的</SPAN><SPAN 
            lang=EN-US>OOPL</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">都会提供</SPAN><SPAN 
            lang=EN-US>class</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">关键字来表示对现实世界模拟的支持。而接口,是作为</SPAN><SPAN 
            lang=EN-US>OO</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序设计范型中与类对应的一个概念。在</SPAN><SPAN 
            lang=EN-US>OO</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设计中,我们所要做的就是针对接口进行设计和编程,而接口的实质含义就是对象之间的一种契约。而类型就是编译器实现中针对类和接口所定义的对应概念。可以这样说,类是现实世界中存在的客观概念,是唯物的。接口是设计人员定义出来的,存在于设计人员心中的概念,是唯心的。而类型是类和接口这两种概念的编译器实现的映射概念,也是唯物的。类型主要是用来指导编译器的类型检查的谓词,类是创建现实对象的模板,接口是</SPAN><SPAN 
            lang=EN-US>OO</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">设计中的关键概念。这三个概念相互区别(分别位于不同的概念域),又相互联系(都是代表相同的概念的不同概念域的映射)。有了上面的理解,我们看看下面最常见的</SPAN><SPAN 
            lang=EN-US>Java</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语句:</SPAN></P>
            <P class=MsoNormal><SPAN lang=EN-US><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN>people a=new man();</SPAN></P>
            <P class=MsoNormal><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这代表了什么?程序员向编译器声明了一个</SPAN><SPAN 
            lang=EN-US>people</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类型(</SPAN><SPAN 
            lang=EN-US>type</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)的对象变量</SPAN><SPAN 
            lang=EN-US>a</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,而对象变量</SPAN><SPAN 
            lang=EN-US>a</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">本身却指向了一个</SPAN><SPAN 
            lang=EN-US>man</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类(</SPAN><SPAN 
            lang=EN-US>class</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)的实体(而在编译器中理解是对象变量</SPAN><SPAN 
            lang=EN-US>a</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">指向了一个类型为</SPAN><SPAN 
            lang=EN-US>man</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的实体)。再让我们回到</SPAN><SPAN 
            lang=EN-US>[Jams 2000]</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,其中句子的根本含义我们可以概括如下:声明一个类或者一个接口都同时向编译器注册了一个新的类型,而此类或者接口以及类型都是共享同样的一个名字。也就是说。编译器所能理解的全部都是类型,而程序员的工作是把现实中的类概念转化为设计中的接口概念,而编译器对应于上两种概念都有直接的支持,那就是一个类声明或者接口声明在编译器的理解来看就是一个类型声明。但是反过来却不一定成立。一个类可以有多个接口(一个类完全有可能实现了设计人员的多个契约条件),同时也就可能有多个类型(因为类型不过是接口这个设计域内的概念在编译器中的实现)。</SPAN></P>
            <P class=MsoNormal><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
            <P class=MsoNormal><SPAN lang=EN-US>&nbsp;<o:p></o:p></SPAN></P>
            <P class=MsoNormal><B><SPAN 

⌨️ 快捷键说明

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