# Module
# Namespace
#
module Chinese
class HelloWorld
def initialize(msg)
@msg = msg
end
def say_hello()
puts "Chinese:" + @msg
end
end
end
module English
class HelloWorld
def initialize(msg)
@msg = msg
end
def say_hello
puts "English:" + @msg
end
end
end
ch = Chinese::HelloWorld.new("大家好!")
ch.say_hello
eh = English::HelloWorld.new("Hello World!")
eh.say_hello
# Namespace
#
module Chinese
class HelloWorld
def initialize(msg)
@msg = msg
end
def say_hello()
puts "Chinese:" + @msg
end
end
end
module English
class HelloWorld
def initialize(msg)
@msg = msg
end
def say_hello
puts "English:" + @msg
end
end
end
ch = Chinese::HelloWorld.new("大家好!")
ch.say_hello
eh = English::HelloWorld.new("Hello World!")
eh.say_hello
本文介绍了一个使用Ruby语言实现的多语言模块化示例,通过定义不同的命名空间来支持中文和英文两种语言输出。该示例展示了如何创建模块并定义类与方法来实现不同语言环境下相同的问候语。
4243

被折叠的 条评论
为什么被折叠?



