Test/Python(20220101~)

Days17. Create our own 'Class'

kiostory 2022. 2. 13. 18:42

* in main.py

 

class User:         #User라는 class를 만들꺼야. User가 가진것, User가 할수 있는 것을 명시
    pass            #별 정의없이 이번에는 그냥 넘어가자
user_1 = User()

class 명은 모든 단어의 첫 번째 글자가 대문자여야 함.

 

ex) class CarCamshaftPulley:                   # << 파스칼케이스(PascalCase)  cf. camelCase,    snake_case

 

Attribute(속성)는  Object(객체)와 관련된 변수

user_1.id = "001"
user_1.username = "kioeom"
print(user_1.id, user_1.username)

----------------------

001 kioeom

 

 

Object가 많아지면 그 Attribute 입력은 다 개별로 하나?

 

Constructor,  Initialize

    - constructor  :  init 함수를 통해 만들 수 있음(__init__)

    - initialize : to set(variables, counters, switches, etc.) to their starting values at the beginning of a program or subprogram 

 

 

class Car:

    def __init__(self):   #initialize attributes

 

 

class User:
    def __init__(self,user_id,username):
        print("New user being created ...")
        self.id = user_id
        self.username = username
user_1 = User("001", "kioeom")
print(user_1.id, user_1.username)