'''
점프 투 파이썬
05-1 Class
'''
class Class1:
num1 = 1
def print1(self):
print(self.num1)
def printParam(self, param):
print(param)
def setNum(self, num):
self.num = num
def printNum(self):
print(self.num)
c = Class1()
print(c.num1)
c.print1()
c.printParam(2)
'''
c.printNum()
초기화 안된 num 변수를 출력하는 함수 호출했더니 에러
Traceback (most recent call last):
File "C:/Users/Maria/PycharmProjects/JumpToPython/05-1 Class/class1.py", line 16, in <module>
c.printNum()
File "C:/Users/Maria/PycharmProjects/JumpToPython/05-1 Class/class1.py", line 10, in printNum
print(self.num)
AttributeError: 'Class1' object has no attribute 'num'
'''
c.setNum(3)
c.printNum()
init
'''
init
'''
class Class1:
num1 = 1
def __init__(self):
self.num = 5
def print1(self):
print(self.num1)
def printParam(self, param):
print(param)
def setNum(self, num):
self.num = num
def printNum(self):
print(self.num)
c = Class1()
c.printNum()
'''
param이 있는 init
'''
class Class1:
num1 = 1
def __init__(self, num):
self.num = num
def print1(self):
print(self.num1)
def printParam(self, param):
print(param)
def setNum(self, num):
self.num = num
def printNum(self):
print(self.num)
'''
c = Class1()
init param을 안넣으면 에러
Traceback (most recent call last):
File "C:/Users/Maria/PycharmProjects/JumpToPython/05-1 Class/class3.py", line 16, in <module>
c = Class1()
TypeError: __init__() missing 1 required positional argument: 'num'
'''
c = Class1(7)
c.printNum()
'''
init 2개 이상 >> 안됨
'''
class Class1:
num1 = 1
def __init__(self):
self.num = 5
def __init__(self, num):
self.num = num
def print1(self):
print(self.num1)
def printParam(self, param):
print(param)
def setNum(self, num):
self.num = num
def printNum(self):
print(self.num)
c = Class1()
c.printNum()
c2 = Class1(7)
c2.printNum()
'''
Traceback (most recent call last):
File "C:/Users/Maria/PycharmProjects/JumpToPython/05-1 Class/class4.py", line 23, in <module>
c = Class1()
TypeError: __init__() missing 1 required positional argument: 'num'
'''
class Class1:
def __init__(self):
num = 1
self.num = 2
def printnum(self):
print(self.num)
c = Class1()
c.printnum() # 2
'''
c = Class1
class 에 괄호 안해서 생김
Traceback (most recent call last):
File "C:/Users/Maria/PycharmProjects/JumpToPython/05-1 Class/class_variable_init.py", line 11, in <module>
c.printnum() # 2
TypeError: printnum() missing 1 required positional argument: 'self'
'''
class Class1:
num = 1
def setnum(self, num):
self.num = num
def printnum(self):
print(self.num)
c = Class1()
print(c.num) # 1
c.printnum()
c.setnum(2)
print(c.num) # 1
c.printnum()
c.num = 3
print(c.num) # 1
c.printnum()
class HousePark:
lastname = '박'
fullname = ''
def setname(self, name):
self.fullname = self.lastname + name
def travel(self, travel):
print(travel)
pey = HousePark()
print(pey.lastname)
pey.setname('응용')
print(pey.fullname)
pey.travel('부산')
# .py
'''
Operator Overloading
연산자 오버로딩
연산자를 클래스 객체끼리 사용할 수 있다.
ClassA + ClassB
'''
class Class1:
num = 1
def __add__(self, other):
print(str(self.num) + '+' + str(other.num) + '=' + str(self.num + other.num))
def __sub__(self, other):
print('%d - %d = %d' % (self.num, other.num, self.num-other.num))
'''
Traceback (most recent call last):
File "C:/Users/Maria/PycharmProjects/JumpToPython/05-1 Class/OperatorOverloading.py", line 22, in <module>
c1 + c2
File "C:/Users/Maria/PycharmProjects/JumpToPython/05-1 Class/OperatorOverloading.py", line 15, in __add__
print(self.num + ' ' + other.num)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Process finished with exit code 1
'''
class Class2:
num = 2
c1 = Class1()
c2 = Class2()
c1 + c2
c1 - c2
class Calculator:
def add(self, a, b):
print(a + b)
def sub(self, a, b):
print(a - b)
def mul(self, a, b):
print(a * b)
def div(self, a, b):
print(a / b)
c = Calculator()
c.add(1, 2)
class Calculator:
def setData(self, a, b):
self.a = a
self.b = b
def add(self):
print(self.a + self.b)
c = Calculator()
c.setData(1, 2)
c.add()
- END -