主页
论坛
新帖
最新消息
新帖
最新动态
制作互助群
登录
注册
Toggle sidebar
Toggle sidebar
菜单
安装应用
安装
回复主题
【进站必读】宝可饭堂社区全局规定v1.10
关于坚决维护同人创作社群秩序与共识的公告
论坛
独立开发
进阶教程
【TA】【ESS】07 类的多态性
禁用JavaScript。为了获得更好的体验,请在运行之前启用浏览器中的JavaScript。
您正在使用一款已经过时的浏览器!部分功能不能正常使用。
请尝试升级或使用
其他浏览器
。
信息
<blockquote data-quote="TAAAAAAA" data-source="post: 2357" data-attributes="member: 2783"><p><u>The constructor 构造器</u></p><p></p><p>Looking at this, you should think “But isn’t CustomClass.new also a class method?”, and you’d be right. This might not seem very interesting, but Class::new (just like Class#method refers to an instance method, Class::method refers to a class method in speech) is a very special kind of class method. It creates an instance of itself and allocates the memory needed, and then immediately after creation, it calls an instance method called initialize if it exists - this is called the <strong>constructor</strong> of the class, which just means it’s called upon creation. We can implement this and add code to it, if we want:</p><p></p><p>到这里你可能会想,我们在创建类的新的实例的时候,是对类调用了new方法,比如CustomClass.new,那是不是就是说new其实也是一个类方法?</p><p>对的!</p><p>new是一个非常特殊的类方法,它实际上存在于Class(类类)类中,任何类其实都是Class类的子类,所以你就算没在你的类里定义new方法,你依然可以调用父类中的new方法。</p><p></p><p>在实例被创建出来之后,如果类中有一个叫做initialize(初始化)的实例方法的话,会立即调用这个方法,这个方法被称为这个类的构造器,或者你也可以直接说初始化方法,另外需要注意的是,这个方法的名字是固定的,不可修改。</p><p>初始化方法,顾名思义,就是对你新创建的实例的各项属性进行一个初始的设置,或者说默认的设置。</p><p></p><p>还是可以拿人来举例,人在出生时,相当于创建了一个人类的实例,而人出生时都是小孩子,这个就是初始化,默认设置出生的是小孩,而不是大人。</p><p></p><p>我们可以随意修改initialize方法来设置一个实例的初始状态以符合我们的需求,我们来看下面的这个例子:</p><p></p><p></p><p>We can even pass arguments via Class::new (the constructor). You just need to make sure Class#initialize matches those arguments.</p><p></p><p>我们也可以在new创建新的实例的时候给初始化方法传递参数,但是要确保你传递的参数是可被接受的,请看下面的这个例子:</p><p></p><p></p><p>So in short: Class::new calls Class#initialize, and their arguments are matched up.</p><p></p><p>Class::new is internal and shouldn’t be changed, but Class#initialize can be overriden and is called upon creation of the object. If you typo def initialize, it won’t be called. This can cause you a severe headache if you don’t notice it if it doesn’t cause any errors, so make sure you spell it right.</p><p></p><p>There’s another catch with the constructor though - its return value is disregarded. No matter <strong>what</strong> you return in def initialize, it’s going to return the newly created object. You can’t change that.</p><p></p><p>关于初始化方法,还有一点你需要注意,在初始化方法中使用返回的时候,返回值会被忽略,也就是说,你使用new时必定会得到这个类的实例,请看下面的例子:</p><p></p><p></p><p>Normally, you’d expect var to be 7 and var.class to be Fixnum because the return value of def initialize is 7, but this return value is disregarded. It does still cancel the rest of the method, though, so “Goodbye!” is never printed.</p></blockquote><p></p>
[QUOTE="TAAAAAAA, post: 2357, member: 2783"] [U]The constructor 构造器[/U] Looking at this, you should think “But isn’t CustomClass.new also a class method?”, and you’d be right. This might not seem very interesting, but Class::new (just like Class#method refers to an instance method, Class::method refers to a class method in speech) is a very special kind of class method. It creates an instance of itself and allocates the memory needed, and then immediately after creation, it calls an instance method called initialize if it exists - this is called the [B]constructor[/B] of the class, which just means it’s called upon creation. We can implement this and add code to it, if we want: 到这里你可能会想,我们在创建类的新的实例的时候,是对类调用了new方法,比如CustomClass.new,那是不是就是说new其实也是一个类方法? 对的! new是一个非常特殊的类方法,它实际上存在于Class(类类)类中,任何类其实都是Class类的子类,所以你就算没在你的类里定义new方法,你依然可以调用父类中的new方法。 在实例被创建出来之后,如果类中有一个叫做initialize(初始化)的实例方法的话,会立即调用这个方法,这个方法被称为这个类的构造器,或者你也可以直接说初始化方法,另外需要注意的是,这个方法的名字是固定的,不可修改。 初始化方法,顾名思义,就是对你新创建的实例的各项属性进行一个初始的设置,或者说默认的设置。 还是可以拿人来举例,人在出生时,相当于创建了一个人类的实例,而人出生时都是小孩子,这个就是初始化,默认设置出生的是小孩,而不是大人。 我们可以随意修改initialize方法来设置一个实例的初始状态以符合我们的需求,我们来看下面的这个例子: We can even pass arguments via Class::new (the constructor). You just need to make sure Class#initialize matches those arguments. 我们也可以在new创建新的实例的时候给初始化方法传递参数,但是要确保你传递的参数是可被接受的,请看下面的这个例子: So in short: Class::new calls Class#initialize, and their arguments are matched up. Class::new is internal and shouldn’t be changed, but Class#initialize can be overriden and is called upon creation of the object. If you typo def initialize, it won’t be called. This can cause you a severe headache if you don’t notice it if it doesn’t cause any errors, so make sure you spell it right. There’s another catch with the constructor though - its return value is disregarded. No matter [B]what[/B] you return in def initialize, it’s going to return the newly created object. You can’t change that. 关于初始化方法,还有一点你需要注意,在初始化方法中使用返回的时候,返回值会被忽略,也就是说,你使用new时必定会得到这个类的实例,请看下面的例子: Normally, you’d expect var to be 7 and var.class to be Fixnum because the return value of def initialize is 7, but this return value is disregarded. It does still cancel the rest of the method, though, so “Goodbye!” is never printed. [/QUOTE]
验证
我们通常会将Pokemon Essentials简称为哪3个字母?
回复帖子
最新帖子
○ 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 类的多态性
顶部