Ruby中,在方法中定义类方法或实例方法的举例
2013-09-03 00:09
387 查看
class C def instance_method p "instance_method" #在实例方法中定义的另一个实例方法 def another_instance_method p "another_instance_method" end another_instance_method #在实例方法中定义的类方法 def C.another_class_method #这里不能用self.another_class_method,因为在instance_method里,self是instance p "another_class_method" end #another_class_method#error,不能使用instance调用class method C.another_class_method end def self.class_method def yet_another_instance_method p "yet_another_instance_method" end #yet_another_instance_method#error,在class_method里,self是class,而不是instance #在实例方法中定义的类方法 def self.yet_another_class_method #这里就可以使用self.yet_another_class_method,因为在class_method里,self是class p "another_class_method" end another_class_method C.another_class_method end end c=C.new #c.another_class_method#error,类方法,不能使用实例调用 c.instance_method p "=================" c.another_instance_method C.another_class_method C.class_method
相关文章推荐
- Ruby中的各种方法定义和调用(函数式方法|实例方法|类方法)
- PowerShell定义函数参数的2种方法和传参方法实例
- Ruby之旅(二) Ruby的基本语法以及对象的使用,方法的定义及调用,属性的定义及使用
- 在js对象中定义属性和方法的实例。
- js入门实例の构造函数\方法的定义和调用\原型对象
- 实现中等难度通讯录。需求: 1、定义联系人类AddressContact。实例变量:姓名(拼音,首字母大写)、性别、电话号码、住址、分组名称、年龄。方法:自定义初始化方法(姓名、电话号码)、显示联系人
- 普通(实例)方法和实例方法的定义和调用
- js静态方法与实例方法定义,js回调方法定义
- Ruby 类的属性、变量、常量和方法定义
- 微信小程序methods中定义的方法互相调用的实例代码
- js入门实例の构造函数\方法的定义和调用\原型对象
- Python二叉树定义与遍历方法实例分析
- javascript面向对象之定义成员方法实例分析
- 定义一个长方形类,定义 求周长和面积的方法实例
- js入门实例の构造函数/方法的定义和调用/原型对象
- 关于类方法和实例方法的定义区分
- //实现中等难度通讯录。需求: //1、定义联系人类Contact。实例变量:姓名(拼音,首字母大写)、性别、电话号码、住址、分组名称、年龄。方法:自定义初始化方法(姓名、电话号码)、显示联系人信息
- [Python] 类方法、实例方法和静态方法的定义方式与区别
- 定义一个计算机类:属性:品牌;价格;颜色; 方法:(1)编程的功能 (2)上网的功能 实例化两个对象:“lenovo”;“hasee”
- Visual C#类的定义及实现方法实例解析