#python #anaconda #jupyter-notebook #visual-code #html #css #javascript #http

제목[re]2020-1 기말고사 기출문제 질문2021-11-26 00:38
작성자

__init__()은 반드시 첫 번째 인수로 self를 지정해야 합니다. self에는 인스턴스 자체가 전달되고. 최과 메소드 내에 인스턴스 변수를 작성하거나, 참고하는 것이 가능해집니다. 클래스를 생성할 때에 지정한 인수는 초기화 메소드의 2 번째부터 작성해 나가면 됩니다.  


ex)

class MyStatus: 
  def __init__(self,age,name,height,weight): 
    self.age = age
    self.name = name 
    self.height = height
    self.weight = weight 
  def print_name(self): 
    print(self.name) 
  def print_age(self): 
    print(self.age) 
  def print_height(self): 
    print(self.height) 
  def print_weight(self): 
    print(self.weight) 
a = MyStatus(27,"Paul",170,71)
a.print_name()
댓글