nprogram’s blog

気ままに、プログラミングのトピックについて書いていきます

Python基礎学習 (クラスについて)

クラスについて

Pythonでもクラスはあります。
クラスをインスタンス化して、オブジェクトを生成します。
クラスを生成したときに呼び出されるクラスのコンストラクタ処理(初期化処理)は、'''def init(self):'''の形式で呼び出されます。
__init__()にかかわらず、すべてのメソッドの決まりとして、第一パラメータは、selfと記述します。
これは、メソッド呼び出し時に、呼び出し元のオブジェクトが渡されるというPythonのしきたりによるものです。

また、Pythonでは、C++でいうオーバーロードの仕組みがないため、デフォルト引数を使用してオーバーロードを実現します。

class Car:

    def __init__(self, speed=0, time_span=0, distance=0):
        self.speed = speed
        self.time_span = time_span
        self.distance = distance

    def run(self, speed, time_span):
        self.speed = speed
        self.time_span = time_span
        self.distance = speed * time_span

    def show_speed(self):
        print('Speed : ' + str(self.speed))

    def show_time_span(self):
        print('Speed : ' + str(self.time_span))

    def show_distance(self):
        print('Distance : ' + str(self.distance))


car1 = Car()
car1.run(10, 5)
car1.show_speed()
car1.show_time_span()
car1.show_distance()

car2 = Car()
car2.run(10, 10)
car2.show_speed()
car2.show_time_span()
car2.show_distance()

# 実行結果
# Speed : 10
# Speed : 5
# Distance : 50
# Speed : 10
# Speed : 10
# Distance : 100

クラスの機能を外部から注入する

関数名の付け方についても注意してください。アンダースコアで意味ごとに区切るのと、はじめは動詞から入るといいと思います。

自分の手と相手の手の情報を表示する ⇒ [show] + [your] + [and] + [enemy] + [hand] = show_your_and_enemy_hand

import numpy
import SystemIO

class Janken:
    HANDS = ['グー', 'チョキ', 'パー']
    
    def show_description(self) -> str:
        return "グー : 0, チョキ : 1, パー : 2"
        
        
    def show_your_and_enemy_hand(self, you, enemy):
        return "あなた: {0}, 相手: {1}".format(self.HANDS[you], self.HANDS[enemy])
        
        
    def decide_enemy_hand(self) -> int:
        return numpy.random.randint(0, len(self.HANDS))
    
    def judge_winner(self, you, enemy) -> str:
        result = (you - enemy) % len(self.HANDS)
        
        if result == 0 : 
            return "Draw"
        
        elif result == 1:
            return "Lose"
        else:
            return "Win"


class SystemIO:
    
    def print_message(self, message : str):
        print(message)
        
    
    def input(self) -> str :
        return input()


class GameFlow:
    
    def __init__(self, game, flow_io):
        self.game = game
        self.enemy_hand_number = 0
        self.flow_io = flow_io
        
    
    def set_enemy_hand_number(self, hand_number : int):
        self.enemy_hand_number = hand_number
    
    
    def play(self):
        self.flow_io.print_message(self.game.show_description())
        
        you = int(self.flow_io.input())
        
        enemy = self.game.decide_enemy_hand()
        
        self.flow_io.print_message(self.game.show_your_and_enemy_hand(you, enemy))
            
        self.flow_io.print_message(self.game.judge_winner(you, enemy))
    
    
    def play_fixed(self):
        self.flow_io.print_message(self.game.show_description())
        
        you = int(self.flow_io.input())
        
        self.game.your_and_enemy_hand(you, self.enemy_hand_number)
            
        self.flow_io.print_message(self.game.judge_winner(you, enemy))



game = Janken()
system_io = SystemIO()

flow = GameFlow(game, system_io)

flow.play()