2017-01-22 3 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 용 음극 터미널 앱을 강력하게 추천합니다. – 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 = "Default place"' – bew