2017-01-22 64 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

相關問題