0
在我的子類中,我的實例變量是不同的類型嗎?或者,這是紅寶石和動態打字的美麗的一部分?Ruby + OOP:我的子類期望@shipping_address IV有不同的數據類型嗎?
例:
我有一個超UpsShipping
具有可變的一個實例@shipping_method
這是我的第一個子類:
class UpsShippingQuote < UpsShipping
def ship_to
{
"CompanyName" => shipping_address.company, #<-- expects an object that responds to company, etc
"AttentionName" => shipping_address.name,
"PhoneNumber" => shipping_address.phone,
"Address" => recipient_address,
"ResidentialAddress" => nil
}
end
def recipient_address
{
"AddressLine1" => shipping_address.address,
"AddressLine2" => shipping_address.address_2,
"City" => shipping_address.city,
"StateProvinceCode" => shipping_address.state.code,
"CountryCode" => 'US',
"PostalCode" => shipping_address.postal_code
}
end
和第2個亞類:
class UpsShippingEstimator < UpsShipping
def ship_to
{
"CompanyName" => "Test",
"AttentionName" => "Test",
"PhoneNumber" => "1231231234",
"Address" => recipient_address,
"ResidentialAddress" => nil
}
end
def recipient_address
{
"AddressLine1" => "Test",
"City" => shipping_address['city'], #<-- Expects a hash
"StateProvinceCode" => shipping_address['state'],
"CountryCode" => 'US',
"PostalCode" => shipping_address['zip_code']
}
end
所以在UpsShippingQuote
,它期望噸他shipping_method
是我Address
模型的實例,但在UpsShippingEstimator
,它預計shipping_method
是一個散列的一個實例。
這樣好嗎?或者這是一種代碼味道,我正在做繼承錯誤?或者,我正在做得很好,因爲我正在利用動態輸入?