Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

RUBY

클래스와 객체 생성 본문

프로그래밍 언어/Python

클래스와 객체 생성

ruby-jieun 2023. 1. 9. 19:42

 

 

클래스와 객체 생성

 


 

클래스는 class 키워드와 속성(변수) 그리고 기능(함수)를 이용해서 만든다.

 

객체는 클래스의 생성자를 호출한다.

class Car:
    
    def __init__(self, color, length):
        self.color = color
        self.length = length
        
    def doStop(self):
        print('STOP!!')
        
    def doStart(self):
        print('START!!')

객체 2개 생성

car1 = Car('red', 200)
car2 = Car('blue', 300)
class Car:

    def __init__(self, color, length):
        self.color = color
        self.length = length

    def doStop(self):
        print('STOP!!')

    def doStart(self):
        print('START!!')

    def printCarInfo(self):
        print(f'self.color: {self.color}')
        print(f'self.length: {self.length}')


car1 = Car('red', 200)
car2 = Car('blue', 300)

car1.printCarInfo()
car2.printCarInfo()

'프로그래밍 언어 > Python' 카테고리의 다른 글

객체와 메모리  (0) 2023.01.09
객체 속성 변경  (0) 2023.01.09
객체지향 프로그래밍  (0) 2023.01.09
자주 사용하는 모듈  (0) 2023.01.09
실행(메인) 파일, 패키지  (0) 2023.01.07
Comments