📄 2.9.htm
字号:
src="2.9.files/spacer.gif" width=1 border=0></TD></TR>
<TR>
<TD class=v1 align=middle width=188 height=1><IMG height=1
src="2.9.files/spacer.gif" width=1
border=0></TD></TR></TBODY></TABLE></DIV></FORM></TD>
<TD align=middle>
<TABLE width="95%">
<TBODY>
<TR>
<TD class=v2 align=middle>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD height=15> </TD></TR>
<TR>
<TD class=v2 vAlign=top><A
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=print&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-9&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"
target=_new><IMG height=12 src="2.9.files/img_print.gif"
width=14 align=textTop border=0></A> <A class=v2
title="Printer-friendly version of this section"
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=print&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-9&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"
target=_new>Print</A> <A
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=email&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-9&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=9 hspace=3 src="2.9.files/img_email.gif" width=15
align=absMiddle border=0></A> <A class=v2
title="E-Mail this section"
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=email&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-9&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1">E-Mail</A>
<A
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=bookmark&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-9&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=14 hspace=3 src="2.9.files/img_bookmarks.gif" width=6
align=absMiddle border=0></A> <A class=v2
title="Add a bookmark about this section"
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=bookmark&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-9&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1">Add
Bookmark</A></TD>
<TD class=v2 vAlign=top align=right> <A
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=section&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-8&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=16 src="2.9.files/btn_prev.gif" width=56
align=absMiddle border=0></A> <A
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=section&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9/ruby-CHP-2-SECT-10&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1"><IMG
height=16 src="2.9.files/btn_next.gif" width=41
align=absMiddle border=0></A></TD></TR></TBODY></TABLE>
<HR SIZE=1>
<TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
<TBODY>
<TR>
<TD vAlign=top width=67 rowSpan=3><IMG
src="2.9.files/ruby_xs.gif" width=67 border=0></TD>
<TD class=v2 vAlign=top><A class=v1
href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&mode=section&sortKey=title&sortOrder=asc&view=&xmlid=0-59600-214-9&open=false&catid=&s=1&b=1&f=1&t=1&c=1&u=1&srchText=">Ruby
in a Nutshell</A><BR>By Yukihiro Matsumoto<BR></TD></TR>
<TR>
<TD class=v2> </TD></TR>
<TR>
<TD class=t1 vAlign=bottom>Chapter 2. Language
Basics</TD></TR></TBODY></TABLE>
<HR SIZE=1>
<BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD vAlign=top><A name=ruby-CHP-2-SECT-9></A>
<H3 class=docSection1Title>2.9 Object-Oriented
Programming</H3><A name=ruby-IDXTERM-337></A><A
name=ruby-IDXTERM-338></A>
<P class=docText>Phew, seems like a long time since I
introduced Ruby as "the object-oriented scripting language,"
eh? But now you have everything you need to get the
nitty-gritty details on how Ruby treats classes and objects.
After you've mastered a few concepts and Ruby's syntax for
dealing with objects, you may never want to go back to your
old languages, so beware! </P><A name=ruby-CHP-2-SECT-9.1></A>
<H4 class=docSection2Title>2.9.1 Classes and Instances</H4>
<P class=docText><A name=IXT-2-127092></A><A
name=IXT-2-127093></A>All Ruby data consists of objects that
are instances of some class. Even a class itself is an object
that is an instance of the <TT>Class</TT> class. As a general
rule, new instances are created using the <TT>new</TT> method
of a class, but there are some exceptions (such as the
<TT>Fixnum</TT> class). </P><PRE>a = Array::new
s = String::new
o = Object::new</PRE><A name=ch02-79-fm2xml></A><A
name=IXT-2-127094></A><A name=IXT-2-127095></A><A
name=IXT-2-127096></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>class Statement</I></B></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<HR align=left width=515 color=black noShade SIZE=3>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><PRE>class <TT><I>class_name</I></TT> [< <TT><I>superclass</I></TT>]<TT><I>
code</I></TT>
end</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText><A name=IXT-2-127095></A><A
name=IXT-2-127096></A>Defines a class. A
<TT><I>class_name</I></TT> must be a constant. The defined
class is assigned to that constant. If a class of the same
name already exists, the class and <TT><I>superclass</I></TT>
must match, or the <TT><I>superclass</I></TT> must not be
specified, in order for the features of the new class
definition to be added to the existing class. <TT>class</TT>
statements introduce a new scope for local variables. </P><A
name=ruby-CHP-2-SECT-9.2></A>
<H4 class=docSection2Title>2.9.2 Methods</H4><A
name=IXT-2-127097></A>
<P class=docText><A name=IXT-2-127098></A><A
name=IXT-2-127099></A>Class methods are defined with the
<TT>def</TT> statement. The <TT>def</TT> statement adds a
method to the innermost class or module definition surrounding
the <TT>def</TT> statement. A <TT>def</TT> statement outside a
class or module definition (at the top level) adds a method to
the <TT>Object</TT> class itself, thus defining a method that
can be referenced anywhere in the program. </P>
<P class=docText>When a method is called, Ruby searches for it
in a number of places in the following order: </P><SPAN
style="FONT-WEIGHT: bold">
<OL class=docList type=1>
<LI><SPAN style="FONT-WEIGHT: normal">
<P class=docList>Among the methods defined in that object
(i.e., singleton methods).</P></SPAN>
<LI><SPAN style="FONT-WEIGHT: normal">
<P class=docList>Among the methods defined by that object's
class.</P></SPAN>
<LI><SPAN style="FONT-WEIGHT: normal">
<P class=docList>Among the methods of the modules included
by that class.</P></SPAN>
<LI><SPAN style="FONT-WEIGHT: normal">
<P class=docList>Among the methods of the
superclass.</P></SPAN>
<LI><SPAN style="FONT-WEIGHT: normal">
<P class=docList>Among the methods of the modules included
by that superclass.</P></SPAN>
<LI><SPAN style="FONT-WEIGHT: normal">
<P class=docList>Repeats Steps 4 and 5 until the top-level
object is reached.</P></SPAN></LI></OL></SPAN><A
name=ruby-CHP-2-SECT-9.3></A>
<H4 class=docSection2Title>2.9.3 Singleton Classes</H4><A
name=IXT-2-127100></A>
<P class=docText>Attribute definitions for a specific object
can be made using the class definition construction. Uses for
this form of class definition include the definition and a
collection of singleton methods. </P><PRE>class << <TT><I>object</I></TT>
<TT><I>code</I></TT>
end</PRE>
<P class=docText>Creates a virtual class for a specific
object, defining the properties (methods and constants) of the
class using the class definition construction. </P><A
name=ruby-CHP-2-SECT-9.4></A>
<H4 class=docSection2Title>2.9.4 Modules</H4>
<P class=docText><A name=IXT-2-127101></A><A
name=IXT-2-127102></A><A name=IXT-2-127103></A>A module is
similar to a class except that it has no superclass and can't
be instantiated. The <TT>Module</TT> class is the superclass
of the <TT>Class</TT> class. </P><A name=ch02-83-fm2xml></A><A
name=IXT-2-127104></A><A name=IXT-2-127105></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -