【发布时间】:2014-01-22 20:24:07
【问题描述】:
我的子类中的实例变量具有不同类型是不好的做法吗?或者这就是 ruby 和动态类型的美妙之处?
例如:
我有一个超类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
和第二个子类:
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 是一个哈希实例。
这样好吗?或者这是我做错继承的代码味道?或者我做得很好,因为我正在利用动态类型?
【问题讨论】: