class 클래스명:
pass # 빈 클래스임을 나타냄
someone = 클래스명()
def __int__(self) : # 기본 생성자
def __int__(self, a, b) : # 전달인자 2개 받는 생성자
class FourCal :
def setdata(self, first, second) : # self변수는 객체명을 받는 용도이며, 생략 불가
self.first = first
self.second = second
a = FourCal() # 객체 생성
a.setdata(3, 4); # 첫 번째 호출 방법
b = FourCal()
FourCal.setdata(b, 5, 6) # 두 번째 호출 방법, b 생략 불가
print(a.first, a.second)
print(b.first, b.second)
class 자식클래스명(부모클래스명):
class Car() :
def exclaim(self) :
print("I'm a car")
class Yogo(Car) :
pass
car = Car()
print(car.exclaim()) # I'm a car 출력
yogo = Yogo()
print(yogo.exclaim()) # I'm a car 출력, 메서드 상속됨
class Person() :
def __init__(self, name):
self.name = name
class MDPerson(Person) :
def __init__(self, name) :
self.name = "Doctor " + name
class JDPerson(Person) :
def __init__(self, name) :
self.name = name +", Esquire"
person = Person('Fudd')
doctor = MDPerson('Fudd')
lawer = JDPerson('Fudd')
print(person.name) # Fudd 출력
print(doctor.name) # Doctor Fudd 출력
print(lawer.name) # Fudd, Esquire 출력
class Person() :
def __init__(self, name):
self.name = name
class EmailPerson(Person) :
def __init__(self, name, email) :
super().__init__(name) # 부모클래스의 __init__() 호출
self.email = email
bob = EmailPerson('박길동', 'bob@frapples.com')
print(bob.name)
print(bob.email)
30. 파이썬 클래스 - part2 (0) | 2023.10.20 |
---|---|
28. 파이썬 예외처리 (0) | 2023.10.19 |
27. File과 Directory (0) | 2023.10.18 |
댓글 영역