• 클래스 : 변수&매서드(함수)를 정해 놓는 틀

 

  • 클래스를 사용 하는방법

1. class 를 입려하고,

2. 대문자로 시작하는 클래스 이름 설정( 관례적인 약속) 

3. 안에 들어갈 함수와 변수 설정  


class Mingcal:

  def setdate(self,first,second):

    self.first=first

    self.second=second

a=Mingcal()

a.setdate(1,2)

print(a.first)

print(a.second)

 

1

2

 

데이타를 셋업함

 

데이타 셋업을 통하여 계산을 해보았다. 


class Mingcal:

  def setdate(self,first,second):

    self.first=first

    self.second=second

  def add(self):

    result=self.first+self.second

    return result

a=Mingcal()

a.setdate(4,2)

print(a.add())

 

6


여기서 왜 setdate 를 굳이 하는 지 궁금했음 왜냐면, 바로 date를 아래와 같이 셋업을 동시에 할 경우 짧게 코드를 짤수 있다.

 


class MingCal:

  def add(self,firstsecond):

    self.first = first

    self.second = second

    result= self.first+self.second

    return result

a=MingCal()

print(a.add(1,2))


근데 이렇게 짜지 않는 이유는 이렇게 한다면 class 를 쓸 필요가 없을거같음. 다른 함수를 함께 쓸때도 다시 self.somthing 변수 매번정의 해야하는 코드를 짜야 하기 때문이다. 그렇기 때문에 아래와 같이 setdate를 해놓고 아래에 다른 *를 계산하는 함수도 함께 하여 구조체를 만들수 있음.


class Mingcal:

  def setdata(self,first,second):

    self.first=first

    self.second=second

  def add(self):

    result=self.first+self.second

    return result

  def mul(self):

    result=self.first*self.second

    return result

a=Mingcal()

a.setdata(1,2)

print(a.add())

print(a.mul())

3

2


클라스를 사용 하면서, 가장 먼저 실행 해야는걸 __init__ 이라고 한다. 

그럼 __init__을 사용하였을때와 안하였을때의 차이는 멀까?


class Mingcal:

  def __init__(self,first,second):

    self.first=first

    self.second=second

  def setdata(self,first,second):

    self.first=first

    self.second=second

  def add(self):

    result=self.first+self.second

    return result

a=Mingcal(1,2)

print(a.add())

3


가장먼저 __init__ 를실행 하게 된다. 그렇기 때믄 a=Mingcal 을 정의 할때 Mingcal() 변수를 공란으로 두는것이 아닌(someting, something) 을 기입 하라고 오류가 발생함. 위와 같은 예시로 바로 a.setdate(1,2) 과정없이 a를 정의할때 변수를 기재하여야함 

 

 

  • 클래스의 상속 

 

정의한 클래스(부모)를 또다른 클래스(자식)에서 불러 올수있다. 즉, 부모에서 짜놓은 클래스를 자식이 그래로 가져와서 자식 클래스에서 사용할수있는것!변형 또한 가능하다.


class Mingcal:

  def __init__(self,first,second):

    self.first=first

    self.second=second

  def setdata(self,first,second):

    self.first=first

    self.second=second

  def add(self):

    result=self.first+self.second

    return result

class MoreMingcal(Mingcal):

  pass

a=MoreMingcal(1,2)

print(a.add())

3


 부모 class 에 없는 매서드(함수) 를 추가 할수 있다. 부모의 클라스에 ** 제곱을 추가해보자


class Mingcal:

  def __init__(self,first,second):

    self.first=first

    self.second=second

  def setdata(self,first,second):

    self.first=first

    self.second=second

  def add(self):

    result=self.first+self.second

    return result


class MoreMingcal(Mingcal):

  def pow(self):

    result= self.first**self.second

    return result

a=MoreMingcal(1,2)

print(a.pow())

1


부모의 클라스에서 변형도 가능 하다. div 매서드를 변형해보자 


class Mingcal:

  def __init__(self,first,second):

    self.first=first

    self.second=second

  def setdata(self,first,second):

    self.first=first

    self.second=second

  def add(self):

    result=self.first+self.second

    return result

  def div(self):

    result=self.first/self.second

    return result

class MoreMingcal(Mingcal):

  def div(self):

    if self.second==0:

      return 0

    else:

       self.first/self.second

a=MoreMingcal(4,0)

print(a.div())

0


마지막으로 클라스변수, 객체 변수 를 알아보자. 위와같이 변수를__init__을 사용하여 self.first 는 something 으로 한후, a=Mincal(1,2) 이렇게 줄수 있지만, 바로 클라스의 변수를 줄수 있다.


class Mingcal:

  first=1

  second=6

  # def __init__(self,first,second):

  #   self.first=first

  #   self.second=second

  def setdata(self,first,second):

    self.first=first

    self.second=second

  def add(self):

    result=self.first+self.second

    return result

a=Mingcal()

b=Mingcal()

print(a.first)

print(b.first)

1

1


클라스를 정의한 a,b 모두 first =1 이라는 값을 내는 것을 알수 있다. 그럼 클라스의 변수 값을 바꿀수 있을까 ? 


class Family:

  lastname="킴"

Family.lastname="박"

print(Family.lastname)

 

a=Family()

b=Family()

print(a.lastname)

print(b.lastname)


위와같이 클라스 자체의 로직을 바꿀수있다.

+ Recent posts