[Python] ν΄λμ€ μμ μ½λ
- -
lucete1230-cyberpolice.tistory.com/229?category=876734
μ λ²κΈμμ ν΄λμ€μ κ°λ μ μ΅νκ³ μλ μμ λ₯Ό μ€μ΅ν΄λ³΄μΈμ~
* μ κ³΅λΆ λͺ©μ μ©λλ‘ μ€μ΅ ν κ²μ΄λΌ λ΄μ©μ΄ μ‘°κΈ λΆμ‘± ν μ μμ΅λλ€.
#μμ 1
class Student:
def start(self):
print('μλ
νμΈμ')
def printName(self,name):
print('μ΄λ¦μ {0} μ
λλ€'.format(name))
stu = Student()
Student.start(stu)
stu.printName('νκΈΈλ')
#μμ 2
class Student:
schoolName = 'Korea'
stu1 = Student() # κ°μ²΄ μμ±
stu2 = Student()
#id () : κ°μ²΄μ μ£Όμλ₯Ό λλ €μ£Όλ λ΄μ₯ν¨μ
print('stu1μ μ£Όμ : {0}'.format(id(stu1)))
print('stu2μ μ£Όμ : {0}'.format(id(stu2)))
#μμ 2κ°μ κ°μ²΄λ κ°κ° μμ±μλ₯Ό νΈμΆν΄μ λ§λ€μ΄μ§ κ²μ΄λ―λ‘ μλ‘ λ€λ₯Έ μμ μ°¨μ§νκ³ λ§λ¬
Student.schoolName='Seoul'
print("\nStudentμ νκ΅ : ", Student.schoolName)
print("stu1μ νκ΅ : ", stu1.schoolName)
print("stu2μ νκ΅ : ", stu2.schoolName)
'''
schoolNameμ λ©μλ μΈλΆμμ λ§λ€μ΄μ§ κ²μ΄λ―λ‘
classλ³μκ° λκ³ ν΄λμ€ λ³μλ 1κ°λ§ λ§λ€μ΄μ
ν΄λμ€μ ν΄λμ€λ‘λΆν° λ§λ€μ΄μ§ κ°μ²΄ λͺ¨λκ° κ³΅μ ν΄μ μ¬μ©
λ°λΌμ μμ 3κ°μ μΆλ ₯λ¬Έμ λͺ¨λ λμΌν κ°μ μΆλ ₯
'''
#μμ 3
class Student:
schoolName ='Korea'
def setName(self, name):
self.name = name
def getName(self):
return self.name
stu1 = Student()
stu2 = Student()
stu1.setName('νκΈΈλ')
stu2.setName('μ€λμ£Ό')
#νΈμΆν λ selfμμ. λ©μλ μμμλ§ self μ¨μ λ€λ₯Έκ³³μμλ κ°μ Έλ€ μΈ μ μμ.
print("stu1μ μ΄λ¦ : {0}".format(stu1.getName()))
print("stu2μ μ΄λ¦ : {0}".format(stu2.getName()))
#μΌλ° ν¨μμ ν΄λμ€ μ°¨μ΄
#μΌλ° ν¨μ λ°©λ²
result1 = 0
result2 = 0
def add1(num):
global result1
result1 += num
return result1
def add2(num):
global result2
result2 += num
return result2
print(add1(3))
print(add1(4))
print(add2(3))
print(add2(7))
#μλλ ν΄λμ€λ‘ λ³ννκ±°
class Calculator:
def __init__(self):
self.result = 0
def add(self, num):
self.result += num
return self.result
cal1 = Calculator()
cal2 = Calculator()
print(cal1.add(3))
print(cal1.add(7))
print(cal2.add(3))
print(cal2.add(10))
#ν΄λμ€ μμ± μμ : ball
class Ball:
# κ°μ μ attributeλ₯Ό μ§μ
size = 0
color = ''
direction = ''
def bounce(self):#κ°μ²΄μ methodλ₯Ό μ§μ
if self.direction == 'down' :
self.direction == 'up'
else :
self.direction = 'down'
myBall = Ball()
yourBall = Ball()
myBall.color = 'red' #myball κ°μ²΄μ color λ©€λ² λ³μκ° μ§μ
yourBall.color = 'blue' #yourball κ°μ²΄μ color λ©€λ² λ³μκ° μ§μ
print(myBall.color, ' ', yourBall.color)
#κ°μ²΄ μ΄κΈ°ν μ€μ΅
class Ball:
def __init__(self, size, color, direction):
self.size = size
self.color = color
self.direction = direction
def bounce(self):
if self.direction =='down':
self.direction = 'up'
else:
self.direction = 'down'
print("Bounce to",self.direction)
myBall = Ball('small', 'Red', 'down')#κ°μ²΄μ μ΄κΈ°κ°μ μ
λ ₯νμ¬ μμ±
yourBall = Ball('small', 'Red', 'up')
for i in range(0,3):#곡 νκΈ°κΈ°
myBall.bounce()
yourBall.bounce()
κ°μ²΄ μ 보 μΆλ ₯
-> κ°μ²΄μ μ ννλ μ 보λ₯Ό κ°λ°μκ° μ λ ₯ κ°λ₯
-> __str__() λ©μλλ₯Ό μ¬μ©
#κ°μ²΄ μ 보 μΆλ ₯ ~ μμ (μ€μ΅ 4)
class Ball:
def __init__(self, size, color, direction):
self.size = size
self.color = color
self.direction = direction
def __str__(self):
msg = 'Hi, l\'m a ' + self.size + ' ' + self.color + ' ball.'
return msg
myBall = Ball('small','Red','down') #κ°μ²΄μ μ΄κΈ°κ° μ
λ ₯
print(myBall) #κ°μ²΄ μ λ³΄κ° μΆλ ₯
μμ±μμ μλ©Έμ
μ΄κΈ°ν λ©μλ : __init__()
• κ°μ²΄κ° μμ±λ ν κ°μ₯ λ¨Όμ νΈμΆλλ©° μλμΌλ‘ νΈμΆλλ λ©μλ
• “μ΄κΈ°ννλ€”λ λ»μ initializeλ₯Ό μ€μ¬μ λΆμ¬μ§ μ΄λ¦
• 첫λ²μ§Έ 맀κ°λ³μλ self μ΄λ©° μ΄νμ 맀κ°λ³μ μΆκ° κ°λ₯
• self μ΄μΈμ 맀κ°λ³μκ° μλ μ΄κΈ°ν λ©μλλ₯Ό λ§λ€λ©΄ μΈμ€ν΄μ€λ₯Ό μμ±ν λ 맀κ°λ³μλ₯Ό λ겨주μ΄μΌ ν©λλ€.
• μΈμ€ν΄μ€ λ³μλ₯Ό μ΄κΈ°ν νκΈ° μν λͺ©μ μΌλ‘ μ£Όλ‘ μμ±
μλ©Έμ :__del__()
• κ°μ²΄κ° μλ©Έλ λ μλμΌλ‘ νΈμΆλλ λ©μλ
• μΈλΆ μμμ μ¬μ©νλ κ²½μ° ν΄μ νλ μ½λλ₯Ό μ£Όλ‘ μμ±
• self μ΄μΈμ 맀κ°λ³μλ₯Ό λ°μ§ μμ΅λλ€.
μλλ μμ±μ μλ©Έμ μμ ) κ° μμ λ²νΈλ λ€λ₯Έ μμ μ΄λ λ°λ‘ μ€μ΅ν΄λ³΄μΈμ
#μμ±μμ μλ©Έμ μμ
# μμ±μ μμ 1
class Book:
def __init__(self, bookName):
self.name = bookName
print("κ°μ²΄κ° μμ±λμμ΅λλ€. \nμ±
μ μ΄λ¦μ " + bookName + "μ
λλ€.")
objectBook = Book("λλ₯Ό μκ·Ήνλ νμ΄μ¬")
#μμ±μ μμ 2
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def aboutME(self):
print("μ μ΄λ¦μ " + self.name + "μ΄λ©°, " + str(self.age)+"μ΄ μ
λλ€.")
adult = Person('νκΈΈλ',20)
adult.aboutME()
# μλ©Έμ μμ
class IceCream:
def __init__(self, name, price):
self.name = name
self.price = price
print(name+"μ κ°κ²©μ"+ str(price)+"μ μ
λλ€.")
def __del__(self):
print(self.name +"κ°μ²΄κ° μλ©Έν©λλ€.")
ice_cream = IceCream('λΆλΌλ³΄μ½',1000)
del ice_cream
ice_cream = IceCream('λΆλΌλ³΄μ½2',1000)
λ§μ§λ§ μ΄ μ 리 μμ
#μμ±μμ μλ©Έμ μμ 3
from time import ctime
class Student:
#μμ±μ
def __init__(self, name = 'noname'):
print('{0}μ κ°μ²΄κ° μμ±λμμ΅λλ€'.format(ctime()))
self.name = name
schoolNAme = 'korea'
def setName(self, name):
self.name = name
def getName(self):
return self.name
#μλ©Έμ
def __del__(self):
print('{0}μ κ°μ²΄κ° μλ©Έλμμ΅λλ€'.format(ctime()))
stu1 = Student("μ€μ")
stu2 = Student()
print("stu1μ μ΄λ¦ : {0}".format(stu1.getName()))
print("stu2μ μ΄λ¦ : {0}".format(stu2.getName()))
del stu1
del stu2
#############################################################
#ν΄λμ€μ λ©μλ μΆκ°νκΈ° 1
class BusinessCard:
def set_info(self, name, email, addr):
self.name = name
self.email = email
self.addr = addr
member1 = BusinessCard()
print(member1)
#############################################################
# ν΄λμ€μ λ©μλ μΆκ°νκΈ° 2
class BusinessCard:
def set_info(self, name, email, addr):
self.name = name
self.email = email
self.addr = addr
def print_info(self):
print("------------------")
print("Name : ", self.name)
print("Email : ", self.email)
print("Address : ", self.addr)
print("------------------")
member1 = BusinessCard()
member1.set_info("Yuna Kim","yunaKim@naver.com","Seoul")
#############################################################
#μμ±μ - μλνΈμΆ, κ°μ²΄ μ΄κΈ°ν
#μΈμ€ν΄μ€ μμ±κ³Ό λμμ μλμΌλ‘ νΈμΆλλ λ©μλ μ¬μ© : μμ±μ
class BusinessCard:
def __init__(self, name, email, addr):
self.name = name
self.email = email
self.addr = addr
def print_info(self):
print("---------------------------------------")
print("Name : ", self.name)
print("Email : ", self.email)
print("Address : ", self.addr)
print("---------------------------------------")
member1 = BusinessCard("Yuna Kim","yunaKim@naver.com","Seoul")
member1.print_info()
'Languages > Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Python] λ€μ€μμ, μΆμν΄λμ€ (0) | 2020.11.24 |
---|---|
[Python] ν΄μκ° κ΅¬νκΈ°(SHA-256) (0) | 2020.11.04 |
[Python] ν΄λμ€ μμ보기(λ³μ, λ©μλ,ν΄λμ€) (0) | 2020.11.03 |
[Python] μ¬κ·ν¨μ (0) | 2020.10.13 |
μμ€ν κ³΅κ° κ°μ¬ν©λλ€