ruby笔记《二》类定义

简介:
 
  1. # 定义类方法 
  2.  
  3. class Demo 
  4.   @instvariable=100   #实例变量 
  5.     

  6.   # 类变量,需要在使用之前初始化,否则会报错,也就是在get之前要先set,可以写方法set,也可以在    # initialize中做初始化的工作
  7.   @@classvariable=200
  8.  
  9.   def initialize 
  10.    
  11.   end 
  12.  
  13.   # class method 
  14.   def Demo.method1 
  15.   end 
  16.    
  17.  
  18.   # class method 
  19.   def self.method2 
  20.   end 
  21.  
  22.  
  23.   # class method 
  24.   class << self 
  25.     def method3 
  26.  
  27.     end 
  28.   end 
  29.  
  30.   # instance method 
  31.   def instmethod 
  32.  
  33.   end 
  34.  
  35.   def pubmethod 
  36.  
  37.   end 
  38.  
  39.   def primethod 
  40.  
  41.   end 
  42.  
  43.   def protmethod 
  44.  
  45.   end 
  46.  
  47.   public  :pubmethod 
  48.   private :primethod 
  49.   protected :protmethod 
  50.  
  51. end 
  52.  
  53. demo=Demo.new 
  54.  
  55. # list all method, include class method and instance method 
  56. Demo.methods 
  57.  
  58. # list all method which user defined, include class method and instance mehtod 
  59. Demo.methods(false
  60.  
  61. # list all instance method 
  62. Demo.instance_methods 
  63.  
  64. # list all instance method which user defined 
  65. Demo.instance_methods(false
  66.  
  67. # list all instance variable 
  68. Demo.instance_variables 
  69.   # list all class variables Demo.class_variables
  70.  
  71. singleton 
  72.  
  73. class MyLogger 
  74.   private_class_method :new #将new这个类方法私有化 
  75.   @@logger=nil 
  76.   def self.create 
  77.     @@logger =new unless @@logger 
  78.  
  79.     @@logger 
  80.   end 
  81. end 
  82.  
  83. MyLogger.create 

 

实例变量访问器instance variable accessor

 
  1. class Demo 
  2.  
  3.   attr_access :name 
  4.  
  5. end 
  6.  
  7. demo=Demo.new 
  8.  
  9. demo.name="andyshi" 
  10.  
  11. puts demo.name 

 

读写访问器

 
  1. attr_reader :name 
  2.  
  3. attr_writer :name 

相当于下面的两个方法。

 
  1. def name 
  2.  
  3.   @name 
  4.  
  5. end 
  6.  
  7. def name=(name) 
  8.  
  9.   @name=name 
  10.  
  11. end 

 

 

类变量访问器class variable accessor

 
  1. class Demo 
  2.  
  3.   class << self 
  4.  
  5.     attr_accessor :name 
  6.  
  7.   end 
  8.  
  9.   @name="andy" 
  10.  
  11. end 
  12.  
  13. puts Demo.name  # andy 

 

类变量访问器class variable accessor

 
  1. class Demo 
  2.  
  3.   class << self; attr_accessor :name end 
  4.  
  5.   @name="virus" 
  6.  
  7. end 
  8.  
  9. puts Demo.name # virus 

 

别名alias

 
  1. class Demo 
  2.   def longmethod 
  3.   end 
  4.   alias shortmethod longmethod 
  5. end 
  6.  
  7. demo=Demo.new 
  8. demo.longmethod 
  9. demo.shortmethod 

 




本文转自 virusswb 51CTO博客,原文链接:http://blog.51cto.com/virusswb/1051274,如需转载请自行联系原作者

目录
相关文章
|
开发者 编解码
界面适应奥秘:从自适应布局到图片管理,Xamarin响应式设计全解析
【8月更文挑战第31天】在 Xamarin 的世界里,构建灵活且适应性强的界面是每位开发者的必修课。本文将带您探索 Xamarin 的响应式设计技巧,包括自适应布局、设备服务协商和高效图片管理,帮助您的应用在各种设备上表现出色。通过 Grid 和 StackLayout 实现弹性空间分配,利用 Device 类检测设备类型以加载最优布局,以及使用 Image 控件自动选择合适图片资源,让您轻松应对不同屏幕尺寸的挑战。掌握这些技巧,让您的应用在多变的市场中持续领先。
103 0
|
开发者 测试技术 Android开发
Xamarin 开发者的五大常见问题及解决方案:从环境搭建到性能优化,全面解析高效跨平台应用开发的技巧与代码实例
【8月更文挑战第31天】Xamarin 开发者常遇问题及解决方案覆盖环境搭建至应用发布全流程,助新手克服技术难关。首先需正确安装配置 Visual Studio 及 Xamarin 支持,设置 iOS/Android 测试环境。利用 Xamarin.Forms 和 XAML 实现高效跨平台开发,共享 UI 和业务逻辑代码。针对性能优化,采取减少 UI 更新、缓存计算结果等措施,复杂问题则借助 Xamarin Profiler 分析。
155 0
|
Ruby
|
Ruby