2017-01-22 7 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ありがとうございました! Alien(最初のムービー)のサウンドでMac用のCathode端末アプリを強くお勧めします – 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

関連する問題