Lecture 인스턴스 변수와 클래스 변수

Views 42 • Comments 0 • Last Updated at 3 days ago Full screen  
  • 클래스
  • 클래스

인스턴스 변수와 클래스 변수

인스턴스 변수는 인스턴스에 포함된 변수이고 클래스 변수는 클래스에 포함된 변수이다.

인스턴스와 클래스 변수는 선언할때 만들어진다.

인스턴스 변수는 변수에 접근할 때 self를 붙이고 클래스 변수는 변수에 접근할 때 클래스 명을 붙인다.

클래스 변수는 설계도에 해당하므로 해당 설계도로 생성된 모든 인스턴스에서 같이 사용되는 변수이다.

class Car:
   color =" "
   speed = 0
   count = 0
   def __init__(self):
      self.speed = 0
      Car.count +=1
class Sedan(Car):
   def __init__(self):
      Car.__init__(self)
      
mycar1 = Car()
mycar1.speed = 30
mycar2 = Car()
mycar2.speed = 60
mycar3 = Sedan()

print(mycar1.count, mycar1.speed)
print(mycar2.count, mycar2.speed)
print(Car.count, Car.speed)
previous article
last article
Comments
Feel free to ask a question, answer or comment, etc.