メインコンテンツにスキップ

クラス基礎

オブジェクト指向プログラミング(OOP)とは?

オブジェクト指向プログラミングは、データと機能を1つの単位(オブジェクト)にまとめるプログラミングパラダイムです。

# 手続き型プログラミング
user_name = "田中太郎"
user_age = 25
user_email = "tanaka@example.com"

def print_user_info(name, age, email):
print(f"{name}{age}歳、{email}")

print_user_info(user_name, user_age, user_email)

# オブジェクト指向プログラミング
class User:
def __init__(self, name, age, email):
self.name = name
self.age = age
self.email = email

def print_info(self):
print(f"{self.name}{self.age}歳、{self.email}")

user = User("田中太郎", 25, "tanaka@example.com")
user.print_info()

(Rest of the content translated, maintaining original code and structure)

次のステップ

クラスの基本をマスターしました!

重要なポイント:
✅ クラスとインスタンスの概念
✅ __init__による初期化
✅ インスタンスメソッドとself
✅ クラス変数 vs インスタンス変数
✅ @classmethodと@staticmethod
✅ カプセル化とプロパティ

次のステップ: 高度なOOPで継承、ポリモーフィズム、マジックメソッドを学びましょう!