主页
论坛
新帖
最新消息
新帖
最新动态
制作互助群
登录
注册
Toggle sidebar
Toggle sidebar
菜单
安装应用
安装
回复主题
【进站必读】宝可饭堂社区全局规定v1.10
关于坚决维护同人创作社群秩序与共识的公告
论坛
独立开发
进阶教程
【TA】【ESS】07 类的多态性
禁用JavaScript。为了获得更好的体验,请在运行之前启用浏览器中的JavaScript。
您正在使用一款已经过时的浏览器!部分功能不能正常使用。
请尝试升级或使用
其他浏览器
。
信息
<blockquote data-quote="TAAAAAAA" data-source="post: 2355" data-attributes="member: 2783"><p><u>Writing your own classes 新建一个类</u></p><p></p><p>You’re not limited to just the existing, internal classes like String, Array and the like. You can also create your own. A class has a name, and this name is a constant - this means that it starts with an uppercase letter and cannot contain any special characters or operators like !@#$%^&*() (and a lot more, probably).</p><p></p><p>除了本来就有的内置的类外,我们也可以新建类。</p><p>一个类需要有一个名字,类的名字是一个常量,也就是说,类的名字以大写字母开头,并且,类的名字不能包含有特殊字符,同时,类是唯一的,不存在两个名字相同的类。</p><p></p><p>To <strong>create</strong> or <strong>define</strong> a class, you use the class keyword, followed by the name. Just like for, def, and if, class has to be closed with end. In the example below we’ll create a class named CustomClass with nothing in it:</p><p></p><p>你可以使用class关键词新创建一个类,就跟def定义新方法一样,定义新类你也需要在结尾加上end。</p><p>我们来看下面这个例子:</p><p></p><p></p><p>Now, if you type CustomClass in your code, you’re referring to this newly created class. If you do this <strong>before</strong> the class is actually defined, it won’t exist yet, and using it will cause errors. But now you can also use this in methods like something.is_a?(CustomClass), or something.class == CustomClass.</p><p></p><p>现在,你可以使用is_a?(CustomClass)来检查一个对象是否是CustomClass类了。</p><p></p><p>We now have a class, but we don’t have any instances of it yet. To initialize an instance of CustomClass, you write CustomClass.new. This returns an object, which is an instance of CustomClass. You should store this in a variable somewhere so you can use it again later.</p><p></p><p>现在,我们有了一个类,但是我们还没有这个类的任何实例。</p><p>要创建一个这个类的实例,我们需要这样写CustomClass.new,也就是说,我们对这个类调用new(新的)方法,这样就会创建一个这个类的实例。</p><p>创建了实例之后,我们应该把它和变量关联起来,这样我们就能随时使用这个实例了。</p><p></p><p>When you do [1,2,3].size #=> 3, you’re calling the <strong>instance method</strong> called size on an <strong>instance</strong> of an array (hence it’s called <em>instance</em> method, duh). You can very easily add similar instance methods to your class by simply defining a method like you normally would.</p><p></p><p>目前,我们的新类中还并没有任何的实例方法,那我们就根据我们之前学过的内容,新建一个实例方法吧,请看下面的例子:</p><p></p><p></p><p>To test this out, we create an instance of the class with CustomClass.new, and then call .say_hello on it:</p><p></p><p>我们来看下面的这个例子:</p><p></p><p></p><p>This might be a bit confusing when you’re used to the special 1..6, “hello”, [1,2,3] formats and the like to create an instance of a class. These are actually just very special ways to create an instance of those classes - normally, for any class, you would use <ClassName>.new. (And no, there’s no way to make one of these kind of formats yourself, unfortunately. You’re stuck with .new).</p></blockquote><p></p>
[QUOTE="TAAAAAAA, post: 2355, member: 2783"] [U]Writing your own classes 新建一个类[/U] You’re not limited to just the existing, internal classes like String, Array and the like. You can also create your own. A class has a name, and this name is a constant - this means that it starts with an uppercase letter and cannot contain any special characters or operators like !@#$%^&*() (and a lot more, probably). 除了本来就有的内置的类外,我们也可以新建类。 一个类需要有一个名字,类的名字是一个常量,也就是说,类的名字以大写字母开头,并且,类的名字不能包含有特殊字符,同时,类是唯一的,不存在两个名字相同的类。 To [B]create[/B] or [B]define[/B] a class, you use the class keyword, followed by the name. Just like for, def, and if, class has to be closed with end. In the example below we’ll create a class named CustomClass with nothing in it: 你可以使用class关键词新创建一个类,就跟def定义新方法一样,定义新类你也需要在结尾加上end。 我们来看下面这个例子: Now, if you type CustomClass in your code, you’re referring to this newly created class. If you do this [B]before[/B] the class is actually defined, it won’t exist yet, and using it will cause errors. But now you can also use this in methods like something.is_a?(CustomClass), or something.class == CustomClass. 现在,你可以使用is_a?(CustomClass)来检查一个对象是否是CustomClass类了。 We now have a class, but we don’t have any instances of it yet. To initialize an instance of CustomClass, you write CustomClass.new. This returns an object, which is an instance of CustomClass. You should store this in a variable somewhere so you can use it again later. 现在,我们有了一个类,但是我们还没有这个类的任何实例。 要创建一个这个类的实例,我们需要这样写CustomClass.new,也就是说,我们对这个类调用new(新的)方法,这样就会创建一个这个类的实例。 创建了实例之后,我们应该把它和变量关联起来,这样我们就能随时使用这个实例了。 When you do [1,2,3].size #=> 3, you’re calling the [B]instance method[/B] called size on an [B]instance[/B] of an array (hence it’s called [I]instance[/I] method, duh). You can very easily add similar instance methods to your class by simply defining a method like you normally would. 目前,我们的新类中还并没有任何的实例方法,那我们就根据我们之前学过的内容,新建一个实例方法吧,请看下面的例子: To test this out, we create an instance of the class with CustomClass.new, and then call .say_hello on it: 我们来看下面的这个例子: This might be a bit confusing when you’re used to the special 1..6, “hello”, [1,2,3] formats and the like to create an instance of a class. These are actually just very special ways to create an instance of those classes - normally, for any class, you would use <ClassName>.new. (And no, there’s no way to make one of these kind of formats yourself, unfortunately. You’re stuck with .new). [/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 类的多态性
顶部