Ruby03 - Class语法

#! usr/bin/env ruby

class Student # CamelCase --constant
  
  attr_accessor :x # getter and setter
  attr_reader :y # getter

  def initialize(x = 0, y = 0) #constructor and default value
    # @x instance variable
    # @@x class variable
    # $x global variale
    # x local variable
    @x, @y = x, y
  end

  def say_hi # instance method
    system "Say 你好"
  end

  def +(p2) # instance method 
    system "Say 继续说"
  end
end

学习视频:https://www.bilibili.com/video/BV1RJ411Y7A4?p=9&spm_id_from=pageDriver

posted @ 2021-10-31 17:06  lvable  阅读(28)  评论(0编辑  收藏  举报