Ruby 类和对象 面向对象

类和对象

类为属性和函数的组合

声明类

class Customer
   @@no_of_customers=0
end

构造函数

class Customer
   @@no_of_customers=0
   # 构造方法
   def initialize(id, name, addr)
      @cust_id=id
      @cust_name=name
      @cust_addr=addr
   end
end

cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
class Box
   def initialize(w,h)
      @width, @height = w, h
   end
end
创建对象
cust1 = Customer. new
box = Box.new(10, 20)

在这里插入图片描述

类中的成员函数

class Sample
   def hello
      puts "Hello Ruby!"
   end
end
 
# 使用上面的类来创建对象
object = Sample. new
object.hello

# 结果:
Hello Ruby!

实例方法 & 实例变量

class Box
   def initialize(w,h)
  	  # 给实例变量赋值
      @width, @height = w, h
   end
   # 实例方法
   def getArea
      @width * @height
   end
end
 

box = Box.new(10, 20)
 
# 调用实例方法
a = box.getArea()
puts "Area of the box is : #{
      
      a}"

#结果
Area of the box is : 200

类方法 & 类变量

class Box
   # 初始化类变量
   @@count = 0
   def initialize(w,h)
      @width, @height = w, h
      @@count += 1
   end
 
   # 类方法
   def self.printCount()
      puts "Box count is : #@@count"
   end
end
 
box1 = Box.new(10, 20)
box2 = Box.new(30, 100)
 
# 调用类方法
Box.printCount()

# 结果
Box count is : 2

在这里插入图片描述

访问器(getter) & 设置器(setter)方法

class Box
   def initialize(w,h)
      @width, @height = w, h
   end
 
   # 访问器方法
   def getWidth
      @width
   end
   def getHeight
      @height
   end
 
   # 设置器方法
   def setWidth=(value)
      @width = value
   end
   def setHeight=(value)
      @height = value
   end
end
 
# 创建对象
box = Box.new(10, 20)
 
# 使用设置器方法
box.setWidth = 30
box.setHeight = 50
 
# 使用访问器方法
x = box.getWidth()
y = box.getHeight()
 
puts "盒子宽度 : #{
      
      x}"
puts "盒子高度 : #{
      
      y}"

#结果
盒子宽度 : 30
盒子高度 : 50

在这里插入图片描述

由于两种方法非常常用,Ruby 定义了
attr_accessor :variable_name、attr_reader :variable_name、attr_writer :variable_name
三种属性声明方法。其中:accessor=reader+writer。
同时注意:变量名前一定要带 : ,变量名之间要用 , 分割。
在这里插入图片描述

to_s 方法

class Box
   def initialize(w,h)
      @width, @height = w, h
   end
   
   # 定义 to_s 方法
   def to_s
      "(w:#@width,h:#@height)"  # 对象的字符串格式
   end
end
 
# 创建对象
box = Box.new(10, 20)
 
# 自动调用 to_s 方法
puts "String result : #{
      
      box}"

# 结果
String result : (w:10,h:20)

在这里插入图片描述

访问控制

实例方法保护
Public 方法 Public 方法可被任意对象调用 默认情况下,方法都是 public 的,
initialize 方法总是 private 的
Private 方法 只有类方法可以访问私有成员
Protected 方法 Protected 方法只能被类及其子类的对象调用
class Box
   def initialize(w,h)
      @width, @height = w, h
   end
 
   # 实例方法默认是 public 的
   def getArea
      getWidth() * getHeight
   end
 
   # 让实例方法是 protected 的
   def printArea
      @area = getWidth() * getHeight
      puts "Big box area is : #@area"
   end
   protected :printArea
   
   # 定义 private 的访问器方法
   def getWidth
      @width
   end
   def getHeight
      @height
   end
   private :getWidth, :getHeight
 end
 
# 创建对象
box = Box.new(10, 20)
 
# 调用实例方法
a = box.getArea()
puts "Area of the box is : #{
      
      a}"
 
# 尝试调用 protected 的实例方法
box.printArea()

#结果
Area of the box is : 200
test.rb:42: protected method `printArea' called for #
<Box:0xb7f11280 @height=20, @width=10> (NoMethodError)
局部变量 局部变量在方法外是不可用 小写字母或 _ 开始 name
实例变量 可以跨实例中的方法使用 变量名之前放置符号(@) @cust_id=id
类变量 可以跨不同的对象使用 变量名之前放置符号(@@) @@no_of_customers=0
全局变量 可以跨类使用 全局变量总是以美元符号($)开始 $global_variable = 10

猜你喜欢

转载自blog.csdn.net/weixin_37646636/article/details/121003580