ရိုးရိုးလေး ရှင်းပြမယ်
Inheritance ကို ရိုးရိုးလေး ပြောရရင်
Inheritance ဆိုတာ class အသစ်တစ်ခုကို ရှိပြီးသား class တစ်ခုရဲ့ စွမ်းရည်တွေနဲ့ စပြီး ရေးခွင့်ပေးတာပါ။ Ruby မှာ class Car < Vehicle လို့ ရေးလိုက်တာနဲ့ Car တိုင်းက Vehicle လုပ်နိုင်သမျှ အားလုံးကို လုပ်နိုင်ပြီး Car ကိုယ်တိုင် ထပ်ထည့်တာ ဒါမှမဟုတ် ပြောင်းတာတွေလည်း ရှိနိုင်ပါတယ်။ မျှဝေသုံးတဲ့ code က parent class တစ်နေရာတည်းမှာ နေပြီး child တစ်ခုချင်းစီက ကွာခြားတာကိုပဲ ဖော်ပြရပါတယ်။
Inheritance က သင့်တော်မသင့်တော်ကို စစ်ဖို့ "Car တစ်စီးဟာ Vehicle တစ်ခုပဲ" လို့ အသံထွက်ပြီး ပြောကြည့်ပါ။ စာကြောင်းက သဘာဝကျရင် is-a ဆက်ဆံရေးဖြစ်လို့ subclass က ကောင်းတဲ့ကိရိယာပါ။ "Car တစ်စီးဟာ Engine တစ်ခုပဲ" လို့ ပြောရင် ထူးဆန်းနေတယ်ဆိုရင် car မှာ engine ရှိတာပဲဖြစ်လို့ တခြား object ကို ကိုင်ထားတဲ့ field က ပိုကောင်းတဲ့ design ပါ။
Problem → Why → Solution
ပြဿနာ: class တစ်ခုကို copy လုပ်ပြီး ချဲ့ထွင်တာ
ကားဂိုဒေါင် app သေးသေးလေးတစ်ခုက Car class နဲ့ စပါတယ်၊ တစ်ပတ်လောက်အကြာမှာ Truck လိုလာပါတယ်။ အမြန်ဆုံးနည်းက Car ကို copy ကူးပြီး နာမည်ပြောင်းလိုက်တာပါ။ အခုတော့ name၊ constructor နဲ့ start_engine တို့ နှစ်ခုစီ ရှိနေပါပြီ။ Start-up message ပြောင်းရင် copy နှစ်ခုလုံးကို ပြင်ရပြီး မကြာခင်မှာ တစ်ခုကိုပဲ ပြင်မိတဲ့သူ ပေါ်လာပါလိမ့်မယ်။
Copy လုပ်တာက ဆက်ဆံရေးကိုလည်း ဖုံးထားပါတယ်။ Car က Truck လားလို့ မေးရင် Ruby က မတားပါဘူး၊ ဒါပေမယ့် နှစ်ခုလုံး vehicle တွေဖြစ်တယ်ဆိုတာ code ထဲမှာ ဘာမှ မပြောထားလို့ vehicle.is_a?(Vehicle) လို စစ်လို့မရသလို vehicle အားလုံးမှာ ဘယ် method တွေ တူလဲဆိုတာကိုလည်း မမေးနိုင်ပါဘူး။ ထပ်နေတာက မြင်ရတဲ့ ကုန်ကျစရိတ်ဖြစ်ပြီး ဘုံ ancestor မရှိတာက မမြင်ရတဲ့ ကုန်ကျစရိတ်ပါ။
class Car
attr_reader :name
def initialize(name)
@name = name
end
def start_engine
"#{name}: engine started"
end
end
class Truck # a pasted copy of Car with a new name
attr_reader :name
def initialize(name)
@name = name
end
def start_engine
"#{name}: engine started"
end
end
p Car.new("Coupe").is_a?(Truck) # false, and no shared type existsShared behaviour က parent တစ်ခုတည်းမှာ ရှိသင့်တယ်
Parent class က မျှဝေသုံးတဲ့ code ကို အိမ်တစ်လုံးတည်း ပေးပါတယ်။ start_engine ကို အဲဒီမှာ ပြောင်းရင် vehicle အားလုံး လိုက်ပြောင်းပါတယ်။ Object တွေကို ဘုံ type တစ်ခုလည်း ပေးတာမို့ is_a?(Vehicle) က car ရော truck ရော true ဖြစ်ပြီး "vehicle ဘာမဆို" လိုတဲ့ method က နှစ်ခုလုံးကို လက်ခံနိုင်ပါတယ်။
Core concept: superclass, method lookup နဲ့ super
class Car < Vehicle က Vehicle ကို Car ရဲ့ superclass အဖြစ် ကြေညာတာပါ။ Ruby မှာ single inheritance ဖြစ်လို့ class တစ်ခုမှာ superclass တစ်ခုပဲ ရှိနိုင်ပါတယ်၊ မရေးရင် Object ဖြစ်ပြီး Object ရဲ့ parent က BasicObject ပါ။ Car object တစ်ခုမှာ Car class က define လုပ်တာတွေအပြင် Vehicle, Object, Kernel တို့ရဲ့ public နဲ့ private instance method အားလုံး ရှိပါတယ်၊ ဒါကြောင့် ကိုယ်က မသင်ပေးထားတဲ့ object တွေပေါ်မှာလည်း puts, inspect, to_s တွေ အလုပ်လုပ်တာပါ။
car.start_engine လို့ ခေါ်တဲ့အခါ Ruby က method lookup လုပ်ပါတယ်။ Object ရဲ့ class ကနေ စပြီး ancestor chain အတိုင်း class တစ်ခုချင်း တက်သွားကာ အဲဒီနာမည်နဲ့ ပထမဆုံး တွေ့တဲ့ method ကို run ပါတယ်။ Car.ancestors က ဒီ chain ကို print ထုတ်ပြပါတယ်: [Car, Vehicle, Object, Kernel, BasicObject]။ Override လုပ်တယ်ဆိုတာ chain ရဲ့ အောက်ပိုင်းမှာ နာမည်တူ method ကို define လုပ်တာပဲဖြစ်လို့ ၎င်းကို အရင်တွေ့ပြီး parent ရဲ့ version က ကွယ်သွားတာပါ။
Method lookup for car.start_engine
----------------------------------
car.start_engine
|
v
+-----+ not here +---------+ found: run it
| Car | ----------> | Vehicle | ---------------> "Coupe: engine started"
+-----+ +---------+
| (only if still missing)
v
Object -> Kernel -> BasicObject -> NoMethodErrorsuper က chain ထဲက နောက်ထပ် class ကနေ lookup ကို ဆက်လုပ်ပါတယ်။ ပုံစံ သုံးမျိုး ရှိပါတယ်။ super(a, b) က ရေးထားတဲ့ argument တွေကိုပဲ ပို့ပါတယ်။ ကွင်းမပါတဲ့ bare super က method ကိုယ်တိုင်ရဲ့ parameter တွေရဲ့ လက်ရှိတန်ဖိုးတွေကို (keyword argument နဲ့ block ပါ) ပို့ပါတယ်။ ကွင်းလွတ်ပါတဲ့ super() ကတော့ ဘာမှ မပို့ပါဘူး။ ကွာခြားချက်က initialize မှာ အရေးအကြီးဆုံးပါ: parent ရဲ့ setup က သူ့ဘာသာ မ run ဘူးဆိုတော့ initialize ကို define လုပ်တဲ့ child က super ကို ခေါ်ရပါမယ်၊ မခေါ်ရင် parent ရဲ့ instance variable တွေ ဘယ်တော့မှ မသတ်မှတ်ရပါဘူး။
Instance variable တွေက class ကို မဟုတ်ဘဲ object ကို ပိုင်တာမို့ Vehicle ကနေ inherit လုပ်ထားတဲ့ method က object မှာရှိတဲ့ @name ကို အလွယ်တကူ ဖတ်ပါတယ်။ Private method တွေလည်း inherit ဖြစ်ပြီး တခြား language တချို့နဲ့ မတူဘဲ subclass က receiver မပါဘဲ ခေါ်လို့ရပါတယ်။ def self.name နဲ့ define လုပ်တဲ့ class method တွေလည်း inherit ဖြစ်ပါတယ်။ is_a? က class ကိုယ်တိုင်နဲ့ ancestor အားလုံးအတွက် true ပြန်ပြီး instance_of? က အတိအကျ class အတွက်ပဲ true ပြန်ပါတယ်။
- car.describe ကို ခေါ်
- Car ထဲကို အရင်ကြည့်
- မရှိရင် Vehicle ကို တက်
- Object, BasicObject အထိ ဆက်တက်
- တွေ့ရင် run၊ မတွေ့ရင် NoMethodError
Override လုပ်တာ၊ super ခေါ်တာ နှစ်ခုလုံးက ဒီ chain အပေါ်မှာပဲ အလုပ်လုပ်ပါတယ်၊ ဒါကြောင့် ancestors ကို ဖတ်တတ်ရင် ဘာ run မယ်ဆိုတာ ခန့်မှန်းနိုင်ပါတယ်။
အထွေထွေ ပုံစံ
class Parent
def initialize(a) = @a = a
def action = "parent"
end
class Child < Parent
def initialize(a, b)
super(a) # run Parent#initialize
@b = b
end
def action = super + " and child"
endclass Child < Parent က inheritance ပါ။ Child ရဲ့ initialize က super(a) နဲ့ parent ရဲ့ setup ကို အရင် run စေပြီးမှ ကိုယ်ပိုင် field ကို သတ်မှတ်ပါတယ်။ action ကတော့ super ရဲ့ ရလဒ်ကို ယူပြီး ထပ်ဖြည့်လို့ parent ရဲ့ အပြုအမူကို အစားထိုးမယ့်အစား တိုးချဲ့ပါတယ်။
Example: Car နဲ့ Motorbike က Vehicle ကို extend လုပ်တာ
Vehicle က name၊ wheels နဲ့ method နှစ်ခု ပေးပါတယ်။ Car က ဘီး ၄ ခုကို ကိုယ်တိုင်သတ်မှတ်ပြီး တံခါးအရေအတွက်ကို ထပ်ထည့်ကာ describe ကို တိုးချဲ့ပါတယ်။ Motorbike ကတော့ ဘီး ၂ ခုကို ပေးပြီး start_engine ကိုပဲ တိုးချဲ့ပါတယ်။ Car က start_engine ကို လုံးဝ မရေးထားဘူးဆိုတာ သတိထားပါ၊ အဲဒါ chain ကနေ ရလာတာပါ။
class Vehicle
attr_reader :name, :wheels
def initialize(name, wheels)
@name = name
@wheels = wheels
end
def describe
"#{name} has #{wheels} wheels"
end
def start_engine
"#{name}: engine started"
end
end
class Car < Vehicle
def initialize(name, doors)
super(name, 4)
@doors = doors
end
def describe
"#{super} and #{@doors} doors"
end
end
class Motorbike < Vehicle
def initialize(name)
super(name, 2)
end
def start_engine
"#{super} with a kick"
end
end
car = Car.new("Coupe", 2)
bike = Motorbike.new("Vespa")
puts car.describe
puts bike.describe
puts car.start_engine
puts bike.start_engine
p Car.ancestors
puts car.is_a?(Vehicle)
puts car.instance_of?(Vehicle)
puts Car.superclassCode ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Subclass line
class Car < Vehicle< က Vehicle ကို Car ရဲ့ superclass အဖြစ် သတ်မှတ်ပါတယ်။ Car.new ကနေ ရတဲ့ object တိုင်းက Vehicle ရဲ့ method တွေကို chain အတိုင်း ရှာတွေ့ပါတယ်။
- Explicit super
super(name, 4)Car ရဲ့ initialize ဟာ name နဲ့ doors လက်ခံပေမယ့် Vehicle#initialize က name နဲ့ wheels လိုတယ်။ ဒါကြောင့် super(name, 4) လို့ ရိုးရိုးရှင်းရှင်း ပို့ပေးပြီး ပြီးမှ @doors ကို သတ်မှတ်ပါတယ်။
- Extending with super
"#{super} and #{@doors} doors"String ထဲက bare super က Vehicle#describe ကို ခေါ်ပြီး ရလာတဲ့ စာသားကို ယူကာ နောက်မှာ တံခါးအရေအတွက်ကို ဆက်ပါတယ်။ ဒါကြောင့် parent ရဲ့ format ကို ပြန်မရေးဘဲ တိုးချဲ့လို့ ရပါတယ်။
- Partial override
"#{super} with a kick"Motorbike ရဲ့ start_engine က Vehicle ရဲ့ စာသားကို ယူပြီး အပိုစာသား ထပ်ဖြည့်ပါတယ်။ ဒီလို override က parent ရဲ့ အပြုအမူကို မဆုံးရှုံးဘဲ ထပ်ပေါင်းပေးပါတယ်။
- Reading the chain
p Car.ancestors[Car, Vehicle, Object, Kernel, BasicObject] လို့ ထွက်ပါတယ်။ ဘယ်ဘက်ကနေ ညာဘက်ကို lookup လုပ်ပြီး ပထမဆုံး တွေ့တာကို run ပါတယ်။
- is_a? versus instance_of?
puts car.instance_of?(Vehicle)is_a?(Vehicle) က true ဖြစ်ပေမယ့် instance_of?(Vehicle) က false ပါ၊ car ရဲ့ အတိအကျ class က Car ဖြစ်လို့ပါ။ ပုံမှန်အားဖြင့် is_a? ကို သုံးတာ ပိုသင့်တော်ပါတယ်။
Coupe has 4 wheels and 2 doors
Vespa has 2 wheels
Coupe: engine started
Vespa: engine started with a kick
[Car, Vehicle, Object, Kernel, BasicObject]
true
false
Vehicle- bike.start_engine ကို ခေါ်
- Motorbike ထဲမှာ start_engine ရှိလား စစ်
- ရှိတယ်: ၎င်းကို run ပြီး super ကို တွေ့တဲ့အခါ Vehicle ကို တက်
- Vehicle#start_engine ရဲ့ ရလဒ်ကို ယူပြီး " with a kick" ဆက်
Class တစ်ခုခုမှာ တွေ့ရင်
That method runs, and super can reach the next one
Chain အဆုံးထိ မတွေ့ရင်
NoMethodError
super အသေးစိတ်: bare super၊ super() နဲ့ မေ့လျော့တဲ့ super
super ပုံစံ သုံးမျိုးက အပြုအမူ မတူဘဲ ကွာခြားချက်တွေက တိတ်တိတ်လေး bug အဖြစ် ပေါ်လာတတ်ပါတယ်။ အောက်ကောင်ထဲက WithBareSuper က name ကို ပြန် assign လုပ်ပြီးမှ bare super ခေါ်လို့ parent က upcase လုပ်ထားတဲ့ တန်ဖိုးကို ရပါတယ်: bare super က caller ပို့ခဲ့တဲ့ တန်ဖိုးမဟုတ်ဘဲ parameter တွေရဲ့ လက်ရှိတန်ဖိုးကို ပို့တာပါ။ WithEmptyParens က super() ခေါ်လို့ parent က ကိုယ်ပိုင် default တွေကို သုံးပါတယ်။ Broken ကတော့ parent က argument လိုတာကို super() က မပေးတဲ့အခါ ရတဲ့ error ကို ပြပြီး Forgetful က initialize ထဲမှာ super ကို လုံးဝ မရေးတဲ့ ပိုတိတ်ဆိတ်တဲ့ ပျက်ကွက်မှုကို ပြပါတယ်: ဘာမှ မညည်းပေမယ့် parent ရဲ့ instance variable တွေ မသတ်မှတ်ရပါဘူး။
class Parent
def greet(name = "world", punct: "!")
"hello #{name}#{punct}"
end
end
class WithBareSuper < Parent
def greet(name = "kid", punct: "?")
name = name.upcase
super # forwards the CURRENT name and punct
end
end
class WithEmptyParens < Parent
def greet(name = "kid", punct: "?")
super() # forwards nothing: Parent's defaults apply
end
end
puts WithBareSuper.new.greet # hello KID?
puts WithBareSuper.new.greet("amy", punct: ".") # hello AMY.
puts WithEmptyParens.new.greet("amy", punct: ".") # hello world!
class Base
def initialize(id)
@id = id
end
end
class Broken < Base
def initialize(id, label)
super() # Base needs one argument
end
end
begin
Broken.new(1, "x")
rescue ArgumentError => e
puts e.message # wrong number of arguments (given 0, expected 1)
end
class Fixed < Base
def initialize(id, label)
super(id)
@label = label
end
end
p Fixed.new(1, "x").instance_variables # [:@id, :@label]
class Forgetful < Base
def initialize(id, label)
@label = label # forgot super
end
end
p Forgetful.new(1, "x").instance_variables # [:@label]NotImplementedError နဲ့ abstract-style method
Ruby မှာ abstract keyword မရှိပါဘူး။ ထုံးစံအရ parent class က NotImplementedError ကို raise လုပ်တဲ့ method ကို define လုပ်ထားပြီး subclass တစ်ခုချင်းစီက ၎င်းကို override လုပ်ပါတယ်။ အောက်က Shape ရဲ့ describe က template method ပါ: area ကို ခေါ်ပြီး subclass တစ်ခုချင်းစီရဲ့ area version က run ပါတယ်။ NotImplementedError က StandardError မဟုတ်ဘဲ ScriptError ကနေ ဆင်းသက်လာလို့ bare rescue => e က မဖမ်းပါဘူး။ ဒါက ရည်ရွယ်ချက်ရှိတာပါ၊ implementation မရှိတာက ပရိုဂရမ်ရေးသူရဲ့ အမှားမို့ပါ၊ code ရဲ့ ဒုတိယအပိုင်းက အဲဒါကို သက်သေပြပါတယ်။
class Shape
def area
raise NotImplementedError, "#{self.class} must implement area"
end
def describe
"#{self.class.name.downcase} with area #{area}"
end
end
class Square < Shape
def initialize(side)
@side = side
end
def area = @side * @side
end
class Blob < Shape; end
puts Square.new(3).describe # square with area 9
begin
Blob.new.describe
rescue NotImplementedError => e
puts "#{e.class}: #{e.message}" # NotImplementedError: Blob must implement area
end
p NotImplementedError.ancestors.first(3) # [NotImplementedError, ScriptError, Exception]
begin
begin
Blob.new.area
rescue => e
puts "bare rescue caught it"
end
rescue NotImplementedError
puts "only an explicit rescue NotImplementedError catches it"
endInheritance လား တခြားနည်းလား
Subclass ဆိုတာ ပြန်လုပ်လို့ ခက်တဲ့ ကတိတစ်ခုပါ: child ကို parent ရှိတဲ့နေရာတိုင်းမှာ သုံးလို့ရရမယ်၊ parent ရဲ့ method အားလုံးကိုလည်း ဆက်ပိုင်ပါတယ်။ Stack ကို Array ကနေ extend လုပ်ရင် push နဲ့ pop ကိုရတယ်၊ ဒါပေမယ့် unshift လို stack စည်းမျဉ်းကို ချိုးဖောက်တဲ့ method အားလုံးကိုပါ ပေးလိုက်ပါတယ်။ အောက်က ဇယားက အခြေအနေအလိုက် ဘာကို ရွေးမလဲ ပြပါတယ်။
| အခြေအနေ | ပိုသင့်တဲ့ ကိရိယာ |
|---|---|
| Manager က Employee တစ်ယောက်ပဲ၊ အပြုအမူအများစု တူတယ် | Subclass |
| မဆိုင်တဲ့ class တွေက helper method တွေ တူတူ လိုတယ် | Module mixin (နောက် lesson) |
| Car မှာ Engine ရှိတယ် | Object တစ်ခုကို field ထဲ ကိုင်ထားတာ (composition) |
| Array လို အပြုအမူကို လိုတယ်၊ ဒါပေမယ့် method နည်းနည်းပဲ ပြချင်တယ် | Array ကို instance variable ထဲ ထည့်ပြီး ကိုယ့် method တွေ ရေး |
class Stack < Array
def peek = last
end
stack = Stack.new
stack.push(1)
stack.push(2)
p stack.peek # 2
stack.unshift(99) # inherited from Array: slips in at the bottom
p stack # [99, 1, 2]လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Rails app တိုင်းမှာ class ဆက်စပ်မှုတွေ ပြည့်နေပါတယ်: class ApplicationController < ActionController::Base၊ class Post < ApplicationRecord၊ Minitest test တစ်ခုက class CartTest < Minitest::Test ပါ။ Gem တွေက ကိုယ့် library အတွက် error class တွေကို class PaymentError < StandardError လို သတ်မှတ်ပြီး ခေါ်သူတွေက ၎င်းကိုပဲ rescue လုပ်နိုင်စေပါတယ်။ Framework တွေက ကိုယ် override လုပ်ရမယ့် hook တွေကို ပေးထားပြီး initialize လို နေရာမှာ super မခေါ်ရင် framework ရဲ့ setup ပျောက်တတ်တာကြောင့် super ကို မျှော်လင့်ထားတတ်ပါတယ်။
ဒီလို hierarchy တွေက နက်လာလေ ကိုယ့် method က ဘယ်ကလာလဲ ရှာရခက်လေပါ။ ဒါကြောင့် ကျွမ်းကျင်သူတွေက chain ကို အနက်နှစ်ခုလောက်၊ အများဆုံး သုံးခုလောက်ပဲ ထားပါတယ်။ Debug လုပ်တဲ့အခါ obj.method(:name).owner က method ကို ဘယ် class ကနေ ရလာတာလဲ ပြောပြပြီး .super_method.owner က ၎င်းရဲ့ အောက်က version ကို ပြပေးလို့ ancestors နဲ့ တွဲသုံးရင် ပုံကြီးကို မြင်ရပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. initialize ထဲမှာ super မခေါ်တာ
Child ရဲ့ initialize ကို ရေးပြီး super မခေါ်ရင် parent ရဲ့ initialize ကို ကျော်သွားပါတယ်။ Error ဘာမှ မပေါ်ဘဲ parent က သတ်မှတ်ရမယ့် @wheels လို instance variable တွေ nil ပဲ ကျန်လို့ နောက်မှာ describe က "Oops has wheels" လို အဓိပ္ပာယ်မရှိတဲ့ စာသား ပြန်ပါတယ်။ Child ရဲ့ initialize ထဲမှာ အရင်ဆုံး super(...) ကို ခေါ်တာ အကျင့်လုပ်ပါ။
2. super နဲ့ super() ကို မှားသုံးတာ
Bare super က လက်ရှိ parameter တွေအားလုံးကို ပို့ပြီး super() က ဘာမှ မပို့ပါဘူး။ Child ရဲ့ initialize(id, label) ထဲမှာ bare super ရေးရင် parent ဆီကို argument နှစ်ခုပို့မှာမို့ parent က တစ်ခုပဲ လက်ခံရင် ArgumentError ပေါ်ပါတယ်၊ ပြောင်းပြန် super() ရေးရင် parent က လိုတဲ့ argument မရတာမို့ "wrong number of arguments (given 0, expected 1)" ပေါ်ပါတယ်။ ပို့ချင်တာကို super(id) လို ရှင်းရှင်းရေးပါ။
3. is-a မဟုတ်ဘဲ code ပြန်သုံးဖို့ subclass လုပ်တာ
class Stack < Array လို ရေးရင် Stack က Array ရဲ့ method အားလုံးကို ဆက်ခံလို့ unshift နဲ့ stack ရဲ့ အောက်ဆုံးကို ထည့်လို့ရသွားပါတယ်။ ဒီလို အမှားမျိုးက စည်းမျဉ်းကို အပြင်ကနေ ဖျက်လို့ရတာမို့ Array ကို instance variable ထဲ ထည့်ပြီး push, pop, peek ကိုပဲ ပြတာက ပိုလုံခြုံပါတယ်။
4. Override လုပ်ပြီး parent ရဲ့ အလုပ်ကို ဆုံးရှုံးတာ
Subclass က log မှတ်တာ၊ validate လုပ်တာလို parent ရဲ့ အလုပ်ကို အစားထိုးမိပြီး super မခေါ်ရင် အဲဒီအလုပ်တွေ တိတ်တိတ်လေး ပျောက်သွားပါတယ်။ တိုးချဲ့ချင်တာဆိုရင် super ကို ခေါ်ပါ၊ အစားထိုးချင်တာ တကယ်ဆိုရင်တော့ comment နဲ့ ရည်ရွယ်ချက်ကို ရေးထားပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Hierarchy ကို ပါးပါးထားပါ၊ အနက်နှစ်ခု သုံးခုထက် မကျော်ရင် ကောင်းပါတယ်။
- Subclass တစ်ခုချင်းစီထက် ဘုံ interface (describe, area လို method) ကို ပစ်မှတ်ထားပြီး code ရေးပါ။
- တိုးချဲ့ချင်ရင် super ကို သုံးပြီး subclass ကြောင့် parent ရဲ့ အလုပ်တွေ မပျောက်စေပါနဲ့။
- Abstract-style method မှာ "#{self.class} must implement area" လို message ရှင်းရှင်းနဲ့ NotImplementedError ကို raise လုပ်ပါ။
- is-a မဟုတ်ရင် composition ဒါမှမဟုတ် module ကို ရွေးပါ။
Mini challenge
Mini challenge: Shape hierarchy
Abstract-style area ပါတဲ့ Shape ကို parent အဖြစ်ထားပြီး Circle နဲ့ Rectangle ကို subclass တွေအဖြစ် ရေးပါ။ area မရှိတဲ့ Blob ကိုလည်း စမ်းကြည့်ပါ။
- Shape#initialize(name) က နာမည်ကို မှတ်ထားပြီး Shape#area က "#{self.class} must implement area" ဆိုတဲ့ message နဲ့ NotImplementedError raise လုပ်ရမယ်
- Shape#describe က "<name> area=<area ကို ဒသမ ၂ နေရာ ဝိုင်းထားတာ>" ကို ပြန်ရမယ်
- Circle(radius) နဲ့ Rectangle(width, height) က super("Circle") စသဖြင့် parent ကို ခေါ်ပြီး area ကို override လုပ်ရမယ်
- Blob < Shape ကို describe ခေါ်ကြည့်ပြီး NotImplementedError ကို rescue လုပ်ကာ class နဲ့ message ကို print ထုတ်ပါ
class Shape
def initialize(name)
# TODO: remember the name
end
def area
# TODO: signal that subclasses must implement this
end
def describe
# TODO: "<name> area=<area rounded to 2 places>"
end
end
# TODO: class Circle < Shape (radius), class Rectangle < Shape (width, height)
# TODO: class Blob < Shape that never defines area
# TODO: print both shapes, then call describe on a Blob and rescue the errorရလဒ်: Output က ဒီလို ထွက်ရပါမယ်: Circle area=78.54 Rectangle area=12.0 NotImplementedError: Blob must implement area
အရိပ်အမြွက် ကြည့်ရန်
Circle ရဲ့ area က Math::PI * @radius**2 ပါ။ Subclass ရဲ့ initialize ထဲမှာ super("Circle") ကို အရင်ခေါ်ပြီးမှ @radius ကို သတ်မှတ်ပါ။ Blob အတွက် initialize ကိုလည်း super("Blob") နဲ့ ရေးဖို့ မမေ့ပါနဲ့၊ မဟုတ်ရင် Shape#initialize က argument လိုပါတယ်။
အဖြေ ကြည့်ရန်
class Shape
attr_reader :name
def initialize(name)
@name = name
end
def area
raise NotImplementedError, "#{self.class} must implement area"
end
def describe
"#{name} area=#{area.round(2)}"
end
end
class Circle < Shape
def initialize(radius)
super("Circle")
@radius = radius
end
def area
Math::PI * @radius**2
end
end
class Rectangle < Shape
def initialize(width, height)
super("Rectangle")
@width = width
@height = height
end
def area
@width * @height
end
end
class Blob < Shape
def initialize
super("Blob")
end
end
puts Circle.new(5).describe
puts Rectangle.new(3, 4.0).describe
begin
Blob.new.describe
rescue NotImplementedError => e
puts "#{e.class}: #{e.message}"
endQuick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- class Child < Parent က Child ကို Parent ရဲ့ subclass လုပ်ပါတယ်၊ Ruby မှာ class တစ်ခုက superclass တစ်ခုပဲ ရှိနိုင်ပြီး Child.ancestors က lookup chain ကို ပြပါတယ်။
- Method ကို receiver ရဲ့ class ကနေ chain အတိုင်း ရှာပြီး ပထမဆုံး တွေ့တာကို run ပါတယ်၊ override ဆိုတာ chain အောက်ပိုင်းမှာ နာမည်တူ method ရေးတာပါ။
- super(a) က ရေးထားတာကိုပဲ၊ bare super က parameter တွေရဲ့ လက်ရှိတန်ဖိုးတွေကို၊ super() က ဘာမှ မပို့ပါဘူး၊ child ရဲ့ initialize ထဲမှာ super ကို မေ့လို့ မရပါဘူး။
- NotImplementedError က ScriptError ဖြစ်လို့ bare rescue က မဖမ်းပါဘူး၊ is-a မဟုတ်ရင် subclass အစား composition ဒါမှမဟုတ် module ကို ရွေးပါ။
Official Ruby Documentation — Ruby Documentation