class Dog:
# Static member (클래스 변수)
species = "Canis familiaris"
# Constructor (생성자)
def __init__(self, name, age):
# Instance variables (인스턴스 변수)
self._name = name
self._age = age
# Property (속성)
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if isinstance(value, str):
self._name = value
else:
raise ValueError("Name must be a string")
# Instance method (인스턴스 메소드)
def bark(self):
return f"{self.name} says Woof!"
# Static method (정적 메소드)
@staticmethod
def what_species():
return f"I am a {Dog.species}"
# Instance 생성
buddy = Dog("Buddy", 5)
max = Dog("Max", 3)
# Instance variable 사용
print(buddy.name) # 출력: Buddy
print(max.name) # 출력: Max
# Property 사용
buddy.name = "Buddy Jr."
print(buddy.name) # 출력: Buddy Jr.
# Instance method 호출
print(buddy.bark()) # 출력: Buddy Jr. says Woof!
# Static member 접근
print(Dog.species) # 출력: Canis familiaris
# Static method 호출
print(Dog.what_species()) # 출력: I am a Canis familiaris
파이썬은 클래스랑 인공지능쓰려고있는 언어지 ㅇㅅㅇ
댓글 0