主页
论坛
新帖
最新消息
新帖
最新动态
制作互助群
登录
注册
Toggle sidebar
Toggle sidebar
菜单
安装应用
安装
回复主题
【进站必读】宝可饭堂社区全局规定v1.10
关于坚决维护同人创作社群秩序与共识的公告
论坛
独立开发
进阶教程
【TA】【ESS】07 类的多态性
禁用JavaScript。为了获得更好的体验,请在运行之前启用浏览器中的JavaScript。
您正在使用一款已经过时的浏览器!部分功能不能正常使用。
请尝试升级或使用
其他浏览器
。
信息
<blockquote data-quote="TAAAAAAA" data-source="post: 2353" data-attributes="member: 2783"><p><strong>7: More classes and polymorphism 类的多态性</strong></p><p></p><p>Classes aren’t just stand-alone classes, but they’re actually kind of like a family tree in that they have ancestors. A String, for example, has the Object class as ancestor (yes, Object is also a class). To illustrate which ancestors a class has, we’ll write it down like so:</p><p></p><p>类不仅仅只是单独的一个类,不同的类之间是有一定的关系的。</p><p>就好像是人类跟狗类也是存在一定的关系的一样。</p><p>更加具体的,某些类之间是有父子关系的,比如说String类(字符串类),我们来看下面的这个例子:</p><p></p><p></p><p>This is the ancestor line for the String class. The first class that comes before String, Comparable, is the <strong>superclass</strong> of the String class.</p><p></p><p>从上面的这个例子中,我们可以清楚的看到String的父子类关系链。</p><p>可以看到,String类的父类(Superclass)是Comparable类(可比较类);String类是Comparable类的子类(Subclass)。</p><p></p><p>The ancestor line of the Fixnum class is this:</p><p></p><p>再来看一个Fixnum类(整数类)的父子类关系链:</p><p></p><p></p><p>Now let’s say we’re doing 12.odd? #=> false. This method returns whether or not the number is odd (13.odd? would be true).</p><p>When you call a method on an object (an instance method), it will look at the highest class there is, which is Fixnum in this case. If that Fixnum class contains an instance method called odd?, it will call that method.</p><p></p><p>我们写下12.odd?这段代码的时候,对于这段代码来说,我们对12这个整数类的实例调用了odd?方法。</p><p>当整数类存在一个叫做odd?的方法时,那么就会直接调用这个odd?方法。</p><p></p><p>If that Fixnum class does <em>not</em> contain an instance method called odd?, it will look at the class below it, which is Integer. If that Integer class contains an instance method called odd?, it’ll call that. If not, it’ll check Precision, Numeric, Comparable, Object, and then Kernel. If it still hasn’t found it after Kernel, it will raise a NoMethodError. This is the concept of <strong>inheritance</strong>/<strong>polymorphism</strong>; a class will <em>inherit</em> all methods from its ancestors.</p><p></p><p>而如果整数类中不存在一个叫做odd?的方法的话,程序会向上从整数类的父类中继续查找是否有一个叫做odd?的方法,如果有的话就运行这个方法,如果还没有的话,就继续向上查找,有就运行,没有就向上查找,直到Kernel(核心)为止,如果Kernel依然没有,那么就会报错,因为你调用了一个根本不存在的方法。</p><p></p><p>也就是说,子类会继承父类的全部方法。</p><p></p><p>Comparable has an instance method called .is_a?, which Strings, Arrays, Floats, Integers and what not all use. But Kernel also has an instance method called .is_a?, which is used by classes that <em>don’t</em> have Comparable (such as NilClass). NilClass’s ancestors are:</p><p></p><p>我们来看一个is_a?这个方法的例子,这个方法存在于Comparable类中,虽然NilClass并不是Comparable的子类,但是我们依然可以对nil对象使用这个方法,这就是因为在Kernel中也存在着一个叫做is_a?的方法。</p><p>我们来看一下NilClass的父子类关系链:</p><p></p><p></p><p>So when you call 14.is_a?(Fixnum), you’re calling it on Comparable.</p><p>When you call nil.is_a?(Fixnum), you’re calling it on Kernel.</p><p></p><p>所以,当你写14.is_a?(Fixnum)的时候,程序调用的其实是Comparable里面的is_a?。</p><p>而当你写nil.is_a?(Fixnum)的时候,程序调用的其实是Kernel里面的is_a?。</p><p></p><p>Speaking of .is_a?, if you give it a class that is a part of the class’s inheritance tree, it’ll also return true.</p><p></p><p>另外,我们也可以用is_a?来检查一个对象的父类是什么,请看下面的例子:</p><p></p><p></p><p>Every single object in Ruby inherits both Kernel and Object, which means var.is_a?(Object) is always going to return true no matter what (as long as var exists). The same applies to var.is_a?(Kernel).</p></blockquote><p></p>
[QUOTE="TAAAAAAA, post: 2353, member: 2783"] [B]7: More classes and polymorphism 类的多态性[/B] Classes aren’t just stand-alone classes, but they’re actually kind of like a family tree in that they have ancestors. A String, for example, has the Object class as ancestor (yes, Object is also a class). To illustrate which ancestors a class has, we’ll write it down like so: 类不仅仅只是单独的一个类,不同的类之间是有一定的关系的。 就好像是人类跟狗类也是存在一定的关系的一样。 更加具体的,某些类之间是有父子关系的,比如说String类(字符串类),我们来看下面的这个例子: This is the ancestor line for the String class. The first class that comes before String, Comparable, is the [B]superclass[/B] of the String class. 从上面的这个例子中,我们可以清楚的看到String的父子类关系链。 可以看到,String类的父类(Superclass)是Comparable类(可比较类);String类是Comparable类的子类(Subclass)。 The ancestor line of the Fixnum class is this: 再来看一个Fixnum类(整数类)的父子类关系链: Now let’s say we’re doing 12.odd? #=> false. This method returns whether or not the number is odd (13.odd? would be true). When you call a method on an object (an instance method), it will look at the highest class there is, which is Fixnum in this case. If that Fixnum class contains an instance method called odd?, it will call that method. 我们写下12.odd?这段代码的时候,对于这段代码来说,我们对12这个整数类的实例调用了odd?方法。 当整数类存在一个叫做odd?的方法时,那么就会直接调用这个odd?方法。 If that Fixnum class does [I]not[/I] contain an instance method called odd?, it will look at the class below it, which is Integer. If that Integer class contains an instance method called odd?, it’ll call that. If not, it’ll check Precision, Numeric, Comparable, Object, and then Kernel. If it still hasn’t found it after Kernel, it will raise a NoMethodError. This is the concept of [B]inheritance[/B]/[B]polymorphism[/B]; a class will [I]inherit[/I] all methods from its ancestors. 而如果整数类中不存在一个叫做odd?的方法的话,程序会向上从整数类的父类中继续查找是否有一个叫做odd?的方法,如果有的话就运行这个方法,如果还没有的话,就继续向上查找,有就运行,没有就向上查找,直到Kernel(核心)为止,如果Kernel依然没有,那么就会报错,因为你调用了一个根本不存在的方法。 也就是说,子类会继承父类的全部方法。 Comparable has an instance method called .is_a?, which Strings, Arrays, Floats, Integers and what not all use. But Kernel also has an instance method called .is_a?, which is used by classes that [I]don’t[/I] have Comparable (such as NilClass). NilClass’s ancestors are: 我们来看一个is_a?这个方法的例子,这个方法存在于Comparable类中,虽然NilClass并不是Comparable的子类,但是我们依然可以对nil对象使用这个方法,这就是因为在Kernel中也存在着一个叫做is_a?的方法。 我们来看一下NilClass的父子类关系链: So when you call 14.is_a?(Fixnum), you’re calling it on Comparable. When you call nil.is_a?(Fixnum), you’re calling it on Kernel. 所以,当你写14.is_a?(Fixnum)的时候,程序调用的其实是Comparable里面的is_a?。 而当你写nil.is_a?(Fixnum)的时候,程序调用的其实是Kernel里面的is_a?。 Speaking of .is_a?, if you give it a class that is a part of the class’s inheritance tree, it’ll also return true. 另外,我们也可以用is_a?来检查一个对象的父类是什么,请看下面的例子: Every single object in Ruby inherits both Kernel and Object, which means var.is_a?(Object) is always going to return true no matter what (as long as var exists). The same applies to var.is_a?(Kernel). [/QUOTE]
验证
宝可梦系列正统游戏的制作公司是?
回复帖子
最新帖子
○ ESS19-20
【游戏引擎】构造/地震引擎简介
最新更新:TAAAAAAA
今天 18:29
入门教程
ESS
【同人游戏】口袋妖怪 地震 0.4.9
最新更新:TAAAAAAA
昨天 14:25
独立同人
✓ ESS20-21
如何制作一个隐藏性格,并令其无法在野生宝可梦身上出现?
最新更新:TAAAAAAA
昨天 14:24
开发问题
教程攻略
应该是这样
最新更新:幽冥魔帝
2026/07/07
同人改版讨论
mGBA西班牙火箭队五周目汉化版怎么调出内置修改器啊
最新更新:TAAAAAAA
2026/07/06
同人改版讨论
论坛统计
主题
611
消息
2,722
成员
3,849
最新成员
Contrary
联系我们
QQ
2201858342
邮箱
pokefans@qq.com
论坛
独立开发
进阶教程
【TA】【ESS】07 类的多态性
顶部