2016-12-14 2 views
0

감사합니다. 부모, 자녀 및 모듈이 두 개의 개별 파일에 있습니다. player_fabrizio은 의 하위 클래스 인 Player 클래스의 인스턴스입니다. 인스턴스 player_fabrizio은 인스턴스 new_settings@totalmoney 변수를 사용해야합니다.Ruby - Mixins을 사용하여 모듈에서 계산 된 부모 변수를 얻으십시오

require_relative "modules_test" 

class Settings 
    include Variables 
    attr_reader :totalmoney 
end 

class Player < Settings 
    def initialize(kingdom, king) 
     @playerData = [kingdom, king] 
    end 
    def calculate 
     print @totalmoney 
    end 
end 

new_settings = Settings.new 
new_settings.globalSettings(100, 2) 

player_fabrizio = Player.new("MyDinasty", "Fabrizio") 
player_fabrizio.calculate    # no output 

이것은 modules_test.rb 파일입니다. new_settingglobalSettingsmodule Variables이고 @totalmoney 변수를 설정합니다.

module Variables 
    def globalSettings(totalmoney, nplayer) 
     @totalmoney = totalmoney 
     return @totalmoney 
    end 
end 

player_fabrizio.calculate의 결과는 100이어야하지만 대신 출력이 제공되지 않습니다. 포럼을 검색했지만 질문을 찾을 수 없었습니다. 비슷한 문제가있는 질문을 읽었지만 답을 찾을 수 없었습니다.

고마워요 당신은 파브리 지오

(Ruby, mixin instance variables and methods)

+0

여기서 부모 클래스로부터 변수를 가져 오려고하지 않습니다. 대신 부모 클래스 –

+0

'@ total_money'의 다른 객체로부터 가져 오기를 시도하면 클래스 자체가 아니라 인스턴스화 된 객체에 저장됩니다. 'new_settings'는'Settings' 클래스의 인스턴스이고'player_fabrizio'는'new_settings' 객체를 알지 못하는'Player' 클래스의 인스턴스입니다. – lcguida

답변

1

나는 다음과 같이 Game-Settings 이름을 변경하고 그것을 할 것이다 (당신이 어떤 이름을 자유롭게 사용할 수 있습니다하지만 당신을 맞는)에 대한 도움말 :

class Game 
    include Variables 
    attr_reader :totalmoney 
end 

class Player 
    attr_reader :game 

    def initialize(game, kingdom, king) 
    @game = game 
    @playerData = [kingdom, king] 
    end 

    def calculate 
    print game.totalmoney 
    # rest of the logic here 
    end 
end 

game = Game.new 
game.globalSettings(100, 2) 

player_fabrizio = Player.new(game, "MyDinasty", "Fabrizio") 
player_fabrizio.calculate 

도움이되는지 알려주세요.

+0

@ Chowlett의 솔루션도 좋아 보인다. 그것은 당신이 창조 할 모든 선수들을위한 글로벌 세팅을 가지고 있다면 작동 할 것이고 (그리고 더 나은 해결책입니다). 대신 다른 설정으로 여러 게임을 만들 수 있다면이 게임을 사용할 수 있습니다. –

+0

놀라운 도움을 주신 모든 분들께 감사드립니다. 나는 이것을 혼자 이해할 수 없었다. 감사합니다. Fabrizio –

1

짧은 답변 - 당신은 너무 player_fabrizio은에 액세스 할 수 없습니다, new_settings예를 멤버 변수 인 @totalmoney을 설정하고 있습니다. 클래스 멤버 변수를 Settings으로 설정하려고합니다.

좀 더 자세하게 -이 모듈은 여기서 약간 문제가됩니다. VariablesSettings에 포함 시키면 Variables 코드를 Settings에 붙여 넣을 수 있습니다. 그래서 당신은 무엇을이하는 것과 같습니다

이 말까지
class Settings   
    attr_reader :totalmoney 

    def globalSettings(totalmoney, nplayer) 
     @totalmoney = totalmoney 
     return @totalmoney 
    end 
end 

class Player < Settings 
    def initialize(kingdom, king) 
     @playerData = [kingdom, king] 
    end 
    def calculate 
     print @totalmoney 
    end 
end 

new_settings = Settings.new 
new_settings.globalSettings(100, 2) 

player_fabrizio = Player.new("MyDinasty", "Fabrizio") 
player_fabrizio.calculate    # no output 

, 당신은 @totalmoney 세트 100로, new_settings라는 Settings 개체가; player_fabrizio이라는 Player 개체 (Settings)가 있으며 @totalmoney이 전혀 설정되어 있지 않습니다.

class Settings   
    def self.globalSettings(totalmoney, nplayer) 
     @@totalmoney = totalmoney 
    end 
end 

class Player < Settings 
    def initialize(kingdom, king) 
     @playerData = [kingdom, king] 
    end 
    def calculate 
     print @@totalmoney 
    end 
end 

Settings.globalSettings(100, 2) 

player_fabrizio = Player.new("MyDinasty", "Fabrizio") 
player_fabrizio.calculate 

주의점 클래스 멤버로 나타내는 @@totalmoney 앞의 @@ :

은 당신이 할 수 있습니다하면 다음과 같다. 또한 globalsettings을 클래스 메서드로 만들었으므로 Settings 개체가 필요 없으므로 전역 설정을 변경할 수 있습니다.

그러나, 나는 조금 상속에 대해 조금 의심 스럽습니다. Player에서 Settings으로 - 플레이어는 특별한 설정 유형이 아닙니다!개인적으로, 나는 아마도 더 비슷한 것을 할 것입니다 :

class Settings 
    def self.globalSettings(totalmoney, nplayer) 
     @@totalmoney = totalmoney 
    end 

    def self.totalmoney 
     @@totalmoney 
    end 
end 

class Player 
    def initialize(kingdom, king) 
     @playerData = [kingdom, king] 
    end 
    def calculate 
     print Settings.totalmoney 
    end 
end 

Settings.globalSettings(100, 2) 

player_fabrizio = Player.new("MyDinasty", "Fabrizio") 
player_fabrizio.calculate 
관련 문제