class 클래스명:
클래스의 특징이나 성격을 정의
함수 등을 정의 #클래세으 정의된 함수를 메서드라고 함
인스턴스 만들기
인스턴스명 = 클래스명()
클래스 메서드 이용하기
인스턴스.메서드()
class block_factory:
def __init__(self):
pass
def함수 뒤에 self는 관용적으로 첫번째에 무조건적으로 씀.
class block_factory:
def __init__(self,company):
self.company = company
block = block_company('Gole')
company 매개변수가 인자 'Gole'을 전달하여 block_factory()로부터 객체를 생성 함!
즉, company 매개변수가 인자를 전달받아 값을 저장한 후, company에 Gole을 저장한 후, block에 객체를 연결해줌.
init 생성자는 외부에서 전달되는 초깃값들을 모두 받을 수 있게 됨.
여러 개의 객체를 생성할 때는 이 방법이 훨씬 편리 ~~!
class Korea:
def say(self):
print('I'm from Korea')
======south Korea 클래스 하나 더 생성해서 korea 클래스로부터 메서드 say를 상속받도록=====
class Korea:
def say(self):
print('I'm from Korea')
class South_korea(Korea): # 자식 클래스 : 부모로부터 상속받기(부모 클래스의 이름을 괄호에 기재)
pass
a=Korea()
b = South_Korea()
a.say()
> I'm from Korea
b.say()
> I'm from Korea
>> Korea클래스로부터 South_Korea가 메서드를 상속받았고, say()메서드를 그대로 쓸 수 있게 됨