2
0

静的メンバ

Posted at

class Ghest():
    def __init__(self):
        #初期化
        self.sum = 0

    def take_food(self, price):
        self.sum += int(price)
    def take_softdrink(self, price):
        self.sum += int(price)
    #ここでデフォルト引数追加
    def take_alcohol(self, price=500):
        pass
    def get_sum(self):
        return self.sum

class Adult(Ghest):
    def __init__(self):
        super().__init__()
        #初期化
        self.alcohol = False
    def take_food(self, price):
        if self.alcohol:
            self.sum += int(price) - 200
        else:
            self.sum += int(price)
    def take_alcohol(self, price=500):
        self.sum += int(price)
        self.alcohol = True

N, K = map(int,input().split())
ghests = [None] * N

for i in range(N):
    age = int(input())
    if age >= 20:
        ghests[i] = Adult()
    else:
        ghests[i] = Ghest()

#注文リスト(0=number,1=order,2=price)
count = 0
for _ in range(K):
    order = input().split()
    if order[1] == "0":
        ghests[int(order[0])-1].take_alcohol()
    elif order[1] == "A":
        print(ghests[int(order[0])-1].get_sum())
        count += 1
    else:
        if order[1] == "food":
            ghests[int(order[0])-1].take_food(int(order[2]))
        elif order[1] == "softdrink":
            ghests[int(order[0])-1].take_softdrink(int(order[2]))
        else:
            ghests[int(order[0])-1].take_alcohol(int(order[2])) 
            
print(count)

静的メンバ。。。やってませんね。。。
えっと静的メンバというのはクラスで使えるカウントできるやつでしたね。。。
まあPHPやってたのでstaticを指すものだということぐらいはわかるのですが
とりあえずやってみます

class Ghest():
    #静的メンバ追加
    n = 0
    def __init__(self):
        #初期化
        self.sum = 0

    def take_food(self, price):
        self.sum += int(price)
    def take_softdrink(self, price):
        self.sum += int(price)
    def take_alcohol(self, price=500):
        pass
    #get_sumメソッドをaccountingメソッドに変更
    def accounting(self):
        Ghest.n += 1
        return self.sum
        
class Adult(Ghest):
    def __init__(self):
        super().__init__()
        #初期化
        self.alcohol = False
    def take_food(self, price):
        if self.alcohol:
            self.sum += int(price) - 200
        else:
            self.sum += int(price)
    def take_alcohol(self, price=500):
        self.sum += int(price)
        self.alcohol = True

N, K = map(int,input().split())
ghests = [None] * N

for i in range(N):
    age = int(input())
    if age >= 20:
        ghests[i] = Adult()
    else:
        ghests[i] = Ghest()

#注文リスト(0=number,1=order,2=price)
count = 0
for _ in range(K):
    order = input().split()
    if order[1] == "0":
        ghests[int(order[0])-1].take_alcohol()
    elif order[1] == "A":
        print(ghests[int(order[0])-1].accounting())
    else:
        if order[1] == "food":
            ghests[int(order[0])-1].take_food(int(order[2]))
        elif order[1] == "softdrink":
            ghests[int(order[0])-1].take_softdrink(int(order[2]))
        else:
            ghests[int(order[0])-1].take_alcohol(int(order[2])) 

#メンバ変数表示          
print(Ghest.n)

こんな感じで使いました
どんな時にこう使うというのが慣れていないと難しいですね

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0