Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

クラスとオブジェクト 演習問題解答例

Last updated at Posted at 2018-03-11

1. 生徒のテストの点数を管理するクラスを作ってください。

コンストラクタには、scoreという変数をクラス内に定義し、scoreは辞書型で管理してください。
例) {"english": 99, "math": 49}

2. 会員情報を保持するUserクラスを以下の指示にしたがって作成してください

クラス名: User
属性
会員ID(user_id)
ユーザーネーム(username)
メールアドレス(email)

3. 2で作成したクラスに、get_userinfo()というメソッドを追記して呼び出してください。

このメソッドを呼び出したら、usernameとemailを出力してください。

sample.py
# 1
class UserScore:
    def __init__(self, score):
        self.score = score

u = UserScore({"english": 100, "math": 49})
u1 = UserScore({"english":80, "math":83})

print(u.score)
print(u1.score)

# 2
class User:
    def __init__(self, user_id, username, email):
    	self.user_id  = user_id
    	self.username = username
    	self.email    = email
    # 3
    def get_userinfo(self):
    	print(self.username)
    	print(self.email)


user1 = User(1, "test", "test@gmail.com")
user2 = User(2, "test2", "test2@gmail.com")

# 3
user1.get_userinfo()
user2.get_userinfo()
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?