主页
论坛
新帖
最新消息
新帖
最新动态
制作互助群
登录
注册
Toggle sidebar
Toggle sidebar
菜单
安装应用
安装
回复主题
【进站必读】宝可饭堂社区全局规定v1.10
关于坚决维护同人创作社群秩序与共识的公告
论坛
独立开发
进阶教程
【TA】【ESS】07 类的多态性
禁用JavaScript。为了获得更好的体验,请在运行之前启用浏览器中的JavaScript。
您正在使用一款已经过时的浏览器!部分功能不能正常使用。
请尝试升级或使用
其他浏览器
。
信息
<blockquote data-quote="TAAAAAAA" data-source="post: 2356" data-attributes="member: 2783"><p><u>Class methods 类方法</u></p><p></p><p>All methods we’ve seen so far were <em>instance</em> methods, but there’s also a thing called <strong>class methods</strong>. As the name suggests, these are methods on the <strong>class</strong> rather than the <em>instance</em>.</p><p></p><p>到目前为止我们说到的所有方法,其实都是实例方法,其实还有另一种类型的方法,叫做类方法。</p><p>顾名思义,实例方法,就是可以对实例调用的方法。</p><p>而类方法,就是可以对类本身调用的方法。</p><p></p><p>Where an instance is a creation or version of the class, methods on the class are only accessible on that class and will always be the same. They’re not bound to any instances. An instance would be 10, “hello”, [1,2], 1..5, false, or else; these are versions or <em>instances</em> of the classes, respectively Fixnum, String, Array, Range, and FalseClass, of which you can have as many as you want. As there are no simple examples of class methods by default, we’ll create our own first. You do everything the way you normally would when creating an instance method in a class, but instead of def <MethodName>, you write def self.<MethodName> (or def <ClassName>.<MethodName>, even).</p><p></p><p>那么如何创建类方法呢?</p><p>很简单,创建类方法和实例方法差不多。</p><p>首先是位置,你需要在类内部创建类方法,这个和创建实例方法是一样的;</p><p>接着,创建类方法和实例方法不同的是,你需要在类方法的名字的前面加一个self.,我们来看下面的这个例子:</p><p></p><p></p><p><span style="color: rgb(44, 130, 201)">Another way to call class methods (which works <u>only</u> with class methods), is ::say_hello rather than .say_hello on the class.</span></p><p></p><p>We would use this on CustomClass itself, and <em>not </em>on its instances:</p><p></p><p>我们来看下面的例子:</p><p></p><p></p><p><em>#<CustomClass> means it’s an <u>instance</u></em><u> of said class (it usually has a 0x0000 (hex) number after it - ignore this)</u></p></blockquote><p></p>
[QUOTE="TAAAAAAA, post: 2356, member: 2783"] [U]Class methods 类方法[/U] All methods we’ve seen so far were [I]instance[/I] methods, but there’s also a thing called [B]class methods[/B]. As the name suggests, these are methods on the [B]class[/B] rather than the [I]instance[/I]. 到目前为止我们说到的所有方法,其实都是实例方法,其实还有另一种类型的方法,叫做类方法。 顾名思义,实例方法,就是可以对实例调用的方法。 而类方法,就是可以对类本身调用的方法。 Where an instance is a creation or version of the class, methods on the class are only accessible on that class and will always be the same. They’re not bound to any instances. An instance would be 10, “hello”, [1,2], 1..5, false, or else; these are versions or [I]instances[/I] of the classes, respectively Fixnum, String, Array, Range, and FalseClass, of which you can have as many as you want. As there are no simple examples of class methods by default, we’ll create our own first. You do everything the way you normally would when creating an instance method in a class, but instead of def <MethodName>, you write def self.<MethodName> (or def <ClassName>.<MethodName>, even). 那么如何创建类方法呢? 很简单,创建类方法和实例方法差不多。 首先是位置,你需要在类内部创建类方法,这个和创建实例方法是一样的; 接着,创建类方法和实例方法不同的是,你需要在类方法的名字的前面加一个self.,我们来看下面的这个例子: [COLOR=rgb(44, 130, 201)]Another way to call class methods (which works [U]only[/U] with class methods), is ::say_hello rather than .say_hello on the class.[/COLOR] We would use this on CustomClass itself, and [I]not [/I]on its instances: 我们来看下面的例子: [I]#<CustomClass> means it’s an [U]instance[/U][/I][U] of said class (it usually has a 0x0000 (hex) number after it - ignore this)[/U] [/QUOTE]
验证
386-151=?
回复帖子
最新帖子
○ 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 类的多态性
顶部