我正在编写的程序是将元素存储在grid
类型的Position => LivingBeing | Thing
哈希中。这个grid
存储在Map
上,我希望这个Map
能够返回Apple
类的元素的位置,这是Thing
的子类。
然而,当使用typeof()
获得该类时,我得到LivingBeing | Thing
而不是子类Apple
这是Map
类:
class Map
@@grid = {} of Position => LivingBeing | Thing
def initialize()
end
# Add an entity to the grid
def add_entity(new_entity : LivingBeing | Thing)
@@grid[new_entity.position] = new_entity
end
# Return the position of an object of class "something"
def self.where_is?(something : Class)
# First attempt was to get the key by the value
# @@grid.key(something)
@@grid.each do |position, thing|
# Returns "thing #<Apple:0x55f1772085c0> at Position(@x=1, @y=2) is (LivingBeing | Thing)"
puts "thing #{thing} at #{position} is #{typeof(thing)}"
position if typeof(thing) == something
end
end
这里的Thing
类:
abstract class Thing
getter position
@name = "Unkown object"
def initialize(@position : Position)
end
end
class Apple < Thing
@name = "Apple"
end
这里是Position
结构:
struct Position
getter x, y
def initialize(@x : Int32, @y : Int32)
end
end
这是我尝试通过的测试:
it "gives a random thing location based on its class" do
world = Map.new()
apple = Apple.new(Position.new(1, 2))
puts "Apple type : #{typeof(apple)}" # Returns "Apple type : Apple"
world.add_entity(apple)
position = Map.where_is?(Apple)
position.should eq Position.new(1, 2)
end
是否有一些类方法或函数可以给Apple
类?还是设计问题?
谢谢您的回答 !
您可以使用forall
来解决此问题:
# Return the position of an object of class "something"
def self.where_is?(something : T.class) forall T
@@grid.each do |position, thing|
return position if thing.is_a?(T)
end
end
并按照您的意愿使用Map.where_is? Apple
调用它。
这是有效的,因为类型变量T
(使用forall T
引入)可以通过传递与Apple
类型限制匹配的常量Apple
推断为T.class
。那么T
就是你可以和is_a?
一起使用的常数。
我有一个解决方案就是我的功能:
# Return the position of an object of class "something"
def self.where_is?(something)
@@grid.each do |position, thing|
return position if thing.is_a?(typeof(something))
end
end
这个测试:
it "gives a random thing location" do
world = Map.new(4)
apple = Apple.new(Position.new(1, 2))
world.add_entity(apple)
position = Map.where_is?(Apple.new(Position.new(0, 0)))
position.should eq Position.new(1, 2)
end
如果没有其他解决方案,我会像这样使用它。但我希望能够直接搜索类Apple
而不是创建Apple
的实例
我希望能够做position = Map.where_is?(Apple)
而不是position = Map.where_is?(Apple.new(Position.new(0, 0)))
正如@ RX14所说,看起来你想检查运行时“类型”,即.class
。这是一个例子:
class Apple
@name = "Apple"
end
def check(obj : Object)
obj.class == Apple
end
a=Apple.new
p check(a)