Thuta Learning
Ruby
AdvancedProgrammingbeginner

Classes & Objects

Class သည် object အတွက် blueprint ဖြစ်ပြီး class keyword နဲ့ရေးပါတယ်။ Object ကို .new နဲ့ class ကနေဖန်တီးပါတယ်။

ruby
class Car
  def drive
    puts "The car is moving."
  end
end

my_car = Car.new
my_car.drive

Car class ထဲမှာ drive method ရှိပါတယ်။ Car.new က Car object အသစ်တစ်ခုဖန်တီးပြီး my_car.drive က method ကိုခေါ်ပါတယ်။

You should see
The car is moving.

Info

Ruby class name ကို အမြဲ စာလုံးအကြီးနဲ့စရေးပါ။ ဥပမာ Car, UserProfile။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • car လို စာလုံးအသေးနဲ့ class define လုပ်ရင် syntax error ဖြစ်နိုင်ပါတယ်။ Class name က constant naming rule ကိုလိုက်ပါတယ်။
Classes & Objects | Thuta Learning