2017-01-22 49 views
0
class Place 
    @description = "Default place" 

    def initialize(x : Int32, y : Int32, description : String) 
    @x = x 
    @y = y 
    @description = description 

    puts "Description of this place is: #{description}" 
    end 
end 



require "./browser-game/*" 
require "./places/*" 

module Browser::Game 
    # TODO Put your code here 
    place = Place.new 2, 3, "Yay new description" 

    puts place.description 
    puts "End of the program" 
end 

我收到此错误:获得实例属性,而不是未定义的方法

Error in browser-game.cr:8: undefined method 'description' for Place

puts place.description 
      ^~~~~~~~~~~ 
+0

我喜欢你的编码设置 –

+0

@LucaAngioloni谢谢!强烈推荐用于Mac的Cathode终端应用程序,带有来自Alien的声音(第一部电影) – idchlife

回答

0

这样写:

class Place 
    getter :description 
    @description = "Default place" 

    def initialize(x : Int32, y : Int32, description : String) 
    @x = x 
    @y = y 
    @description = description 

    puts "Description of this place is: #{description}" 
    end 
end 

getter是用来给从实例访问读取属性。 setter用于设置。如果没有这个,编译器将尝试访问该方法,因为您没有授予对该属性的访问权限。

+1

您可以直接编写'getter description =“默认位置” – bew

相关问题