Python/クラス

Python
# MyProductクラスを定義
class MyProduct:
    def __init__(self, name, price, stock):
        # 引数をメンバに格納
        self.name = name
        self.price = price
        self.stock = stock
        self.sales = 0
        
# MyProductを呼び出し、オブジェクトproduct_1を作成
product_1 = MyProduct("cake", 500, 20)

print(product_1.stock) #20


class Player:
    def walk(self):
        print("勇者は荒野を歩いていた")

    def attack(self, enemy):
        print("勇者は" + enemy + "を攻撃した")

player1 = Player()
player1.walk()
player1.attack("スライム")


class Player:
    def __init__(self, job):
        self.job = job

    def walk(self):
        print(self.job + "は荒野を歩いていた")

player1 = Player("戦士")
player1.walk()


class Item:
    tax = 1.08

    def __init__(self, price, quantity):
        self.price = price
        self.quantity = quantity

    def total(self):
        return int(self.price * self.quantity * Item.tax)

apple = Item(120, 15)
total = apple.total()
print("合計金額は" + str(total) + "円です")

orange = Item(85, 32)
print("合計金額は" + str(orange.total()) + "円です")


class Player:
    def __init__(self, job, weapon):
        self.job = job
        self.__weapon = weapon

    def walk(self):
        print(self.job + "は荒野を歩いていた")
        self.__attack("スライム")
        
	#privateメソッド
    def __attack(self, enemy):
        print(self.__weapon + "で" + enemy + "を攻撃")


player1 = Player("戦士", "剣")
player1.walk()
# player1.attack("スライム")
# print(player1.__weapon)