# -*- encoding: utf-8 -*-
#运算类
class Operation
attr_accessor :number_a,:number_b
def initialize(number_a = nil, number_b = nil)
@number_a = number_a
@number_b = number_b
end
def result
0
end
end
#加法类
class OperationAdd < Operation
def result
number_a + number_b
end
end
#减法类
class OperationSub < Operation
def result
number_a - number_b
end
end
#乘法类
class OperationMul < Operation
def result
number_a * number_b
end
end
#除法类
class OperationDiv < Operation
def result
raise '除数不能为0' if number_b == 0
number_a / number_b
end
end
#工厂类
class OperationFactory
def self.create_operate(operate)
case operate
when '+'
OperationAdd.new()
when '-'
OperationSub.new()
when '*'
OperationMul.new()
when '/'
OperationDiv.new()
end
end
end
oper = OperationFactory.create_operate('/')
oper.number_a = 1
oper.number_b = 2
p oper.result
# -*- encoding: utf-8 -*- #运算类 class Operation attr_accessor :number_a,:number_b def initialize(number_a = nil, number_b = nil) @number_a = number_a @number_b = number_b end def result 0 end end #加法类 class OperationAdd < Operation def result number_a + number_b end end #减法类 class OperationSub < Operation def result number_a - number_b end end #乘法类 class OperationMul < Operation def result number_a * number_b end end #除法类 class OperationDiv < Operation def result raise '除数不能为0' if number_b == 0 number_a / number_b end end module FactoryModule def create_operation end end #加法工厂 class AddFactory include FactoryModule def create_operation OperationAdd.new end end #减法工厂 class SubFactory include FactoryModule def create_operation OperationSub.new end end #乘法工厂 class MulFactory include FactoryModule def create_operation OperationMul.new end end #除法工厂 class DivFactory include FactoryModule def create_operation OperationDiv.new end end factory = AddFactory.new oper = factory.create_operation oper.number_a = 1 oper.number_b = 2 p oper.result
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有