MapleStory Finger Point

πŸ’Ž Python/πŸ“šνŒŒμ΄μ¬ for Beginner (2판)

(κ°œλ…) 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ° - 파이썬 for Beginner (2판)

HYEJU01 2024. 3. 30. 04:33

· 객체지ν–₯ ν”„λ‘œκ·Έλž˜λ° OOP 

 

 

· 클래슀 Class

객체λ₯Ό μ •μ˜ν•˜λŠ” ν‹€ λ˜λŠ” 섀계도

ν˜„μ‹€ μ„Έκ³„μ˜ 사물을 컴퓨터 μ•ˆμ—μ„œ κ΅¬ν˜„ν•˜κΈ° μœ„ν•΄ κ³ μ•ˆλœ κ°œλ…

class 클래슀λͺ… :

 

· ν•„λ“œ Field

객체의 속성을 λ³€μˆ˜λ‘œ 생성

class Car:
    color = ""
    speed = 0

 

· λ©”μ„œλ“œ Method

객체의 κΈ°λŠ₯은 ν•¨μˆ˜(λ©”μ„œλ“œ)둜 κ΅¬ν˜„

def upSpeed(self, value):
	self.speed += value
    
def downSpeed(self, value):
	self.speed -= value

 

· self 
클래슀 자기 μžμ‹ μ„ 가리킨닀.
λ©”μ„œλ“œ μ•ˆμ—μ„œ ν•„λ“œμ— μ ‘κ·Όν•˜κΈ° μœ„ν•΄μ„œ μž‘μ„±

 

· μΈμŠ€ν„΄μŠ€ Instance = 객체

μΈμŠ€ν„΄μŠ€ 생성

myCar1 = Car()
myCar2 = Car()

 

ν•„λ“œμ— κ°’ λŒ€μž…

myCar1.color = "red"
myCar1.speed = 0

 

 λ©”μ„œλ“œ 호좜

myCar1.upSpeed(30)

 

 


βœ… self study 12-1 

class Car:

    color = ''
    speed = 0

    def upSpeed(self,value) :
        if self.speed <= 150 :
            self.speed = 150
        else :
            self.speed += value

    def downSpeed(self,value) :
        self.speed -= value


myCar1 = Car()
myCar1.color = 'red'
myCar1.speed = 0

myCar2 = Car()
myCar2.color = 'red'
myCar2.speed = 0

myCar1.upSpeed(10)
myCar2.upSpeed(160)
print("car speed : %d, color:%s" %(myCar2.speed, myCar2.color))


· μƒμ„±μž Constructor

μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜λ©΄ 무쑰건 ν˜ΈμΆœλ˜λŠ” λ©”μ„œλ“œ

class 클래슀λͺ…:
	def __init__(self):
    	#μ½”λ“œ
class Car :
	color = ""
    speed = 0
    
    def __init__(self):
    	self.color = "λΉ¨κ°•"
        self.speed = 0
        
myCar1 = Car()  #μžλ™μœΌλ‘œ μƒμ„±μž 호좜

κΈ°λ³Έ μƒμ„±μž  : μžλ™μœΌλ‘œ 값이 μ΄ˆκΈ°ν™” λœλ‹€.

class Car:
	color = ""
    speed = 0
    
    
    def __init__(self) :
        self.color = 'red'
        self.speed = 0
        
myCar1 = Car()

 

λ§€κ°œλ³€μˆ˜κ°€ μžˆλŠ” μƒμ„±μž : λ§€κ°œλ³€μˆ˜μ—†μ΄ μΈμŠ€ν„΄μŠ€ μƒμ„±μ‹œ 였λ₯˜!!

class Car:
	color = ""
    speed = 0
    
    
    def __init__(self,value1,value2) :
        self.color = value1
        self.color = value2
        
myCar1 = Car("red", 30)
myCar2 = Car("black", 20)

 

getSpeed λ©”μ„œλ“œλ₯Ό λ§Œλ“€μ–΄μ„œ 값을 λ°›μ•„μ˜€

def getSpeed(self):
	return self.speed


car1,car2 = None, None

car1 = Car("a",1)
car2 = Car("b",2)


car1.getName()
car1.getSpeed()

 

 

· μΈμŠ€ν„΄μŠ€ λ³€μˆ˜

ν•„λ“œ : μ‹€μ œ 곡간이 ν• λ‹Ή 된게 X

μΈμŠ€ν„΄μŠ€ 생성

ν•„λ“œ > μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λ‘œ 별도 곡간이 생성됨

 

· 클래슀 λ³€μˆ˜

클래슀 μ•ˆμ— 곡간이 ν• λ‹Ήλœ λ³€μˆ˜ (μΈμŠ€ν„΄μŠ€μ— 별도 곡간 ν• λ‹Ή x 클래슀 λ³€μˆ˜ 곡간 ν•¨κ»˜ μ‚¬μš©)

클래슀λͺ….ν΄λž˜μŠ€λ³€μˆ˜λͺ…

μΈμŠ€ν„΄μŠ€.ν΄λž˜μŠ€λ³€μˆ˜λͺ…

μ ‘κ·Όν•˜λŠ” ν•„λ“œλŠ” 클래슀 μžμ²΄μ— 곡간이 생성

class Car:
    color = ""
    speed = 0
    count = 0 #클래슀 λ³€μˆ˜
    
    def __inif__(self):
    	self.speed = 0 #
        Car.count +=1 
        
myCar1, myCar2 = None, None


myCar1 = Car()
myCar1.speed = 30
print("μžλ™μ°¨ 속도 %d , μžλ™μ°¨ 총 %dλŒ€" % (myCar.speed, myCar.count))

 

μ–΄λ–€ 것이 클래슀 λ³€μˆ˜ 인지 μ•Œ 수 μžˆλŠ”κ°€?

 => μΈμŠ€ν„΄μŠ€ λ³€μˆ˜, 클래슀 λ³€μˆ˜ μ„ μ–Έ μ‹œμ μ—μ„  ꡬ뢄 λΆˆκ°€λŠ₯ν•˜μ§€λ§Œ , μ ‘κ·Όμ‹œ λΆ™μ—¬μ§€λŠ” λ³€μˆ˜μ— 따라 ꡬ뢄됨.

 

ν•„λ“œμ— μ ‘κ·Όν•  λ•Œ, self 뢙이면 = μΈμŠ€ν„΄μŠ€ λ³€μˆ˜

클래슀λ₯Ό 뢙이면 = 클래슀 λ³€μˆ˜ 생성

 

μΈμŠ€ν„΄μŠ€.μΈμŠ€ν„΄μŠ€λ³€μˆ˜ => μΈμŠ€ν„΄μŠ€ λ³€μˆ˜ μ ‘κ·Ό

μΈμŠ€ν„΄μŠ€.ν΄λž˜μŠ€λ³€μˆ˜ => 클래슀 λ³€μˆ˜ μ ‘κ·Ό

 

 

· 상속 Inheritance

κΈ°μ‘΄ 클래슀의 ν•„λ“œ, λ©”μ„œλ“œλ₯Ό κ·ΈλŒ€λ‘œ λ¬Όλ € λ°›λŠ” μƒˆλ‘œμš΄ 클래슀 λ§Œλ“€κΈ°.

class μ„œλΈŒν΄λž˜μŠ€(슈퍼클래슀) :
	#μ½”λ“œ

 

· super() 

μ„œλΈŒ 클래슀 λ‚΄μ—μ„œ 슈퍼클래슀의 λ©”μ„œλ“œ,속성 μ‚¬μš©μ‹œ

super().carMethod() #μ΄λŸ°μ‹μœΌλ‘œ μ‚¬μš©ν•˜λ©΄ λœλ‹€.!

 

 

 

 

· λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ”© Overriding

μƒμœ„ 클래슀의 λ©”μ„œλ“œλ₯Ό μ„œλΈŒ ν΄λž˜μŠ€μ—μ„œ μž¬μ •μ˜

class Car:
    speed = 0
    def upSpeed(self,value) :
        self.speed += value

        print("speed : %d" % self.speed)


class Sedan(Car) :
    def upSpeed(self,value) : #μž¬μ •μ˜
        self.speed += value

        if self.speed>150:
            self.speed = 150
        print("ν˜„μž¬ 속도(μ„œλΈŒν΄λž˜μŠ€): %d" % self.speed)


class Truck(Car):
    psss

sedan1, truck1 = None, None

truck1 = Truck()
sedan1. Sedan()

print("truck --> :" end="")
truck.upSpeed(200) #슈퍼클래슀 λ©”μ„œλ“œ 호좜

print("sedan --> :" end="")
sedan1.upSpeed(100) #μž¬μ •μ˜ 된 λ©”μ„œλ“œ 호좜

 

 


βœ… self study 12-2

sonata 상속 클래슀 (슈퍼클래슀λ₯Ό Sedan 으둜 μž‘μ•„μ€€λ‹€.)

class Car:
    speed = 0
    def upSpeed(self,value) :
        self.speed += value

        print("speed : %d" % self.speed)


class Sedan(Car) :
    def upSpeed(self,value) : #μž¬μ •μ˜
        self.speed += value

        if self.speed>150:
            self.speed = 150
        print("ν˜„μž¬ 속도(μ„œλΈŒν΄λž˜μŠ€): %d" % self.speed)


class Truck(Car):
    pass


class Sonata(Sedan):
    pass
    

sedan1, truck1, sonata1 = None, None, None

truck1 = Truck()
sedan1 = Sedan()
sonata1 = Sonata()

print("truck --> :", end="")
truck1.upSpeed(200) #슈퍼클래슀 λ©”μ„œλ“œ 호좜

print("sedan --> :", end="")
sedan1.upSpeed(200) #μž¬μ •μ˜ 된 λ©”μ„œλ“œ 호좜

print("sonata --> :", end="")
sonata1.upSpeed(200) #μž¬μ •μ˜ 된 λ©”μ„œλ“œ 호좜


· 클래슀의 νŠΉλ³„ν•œ λ©”μ„œλ“œ

 

__del__ ()

 

Destructor μ†Œλ©Έμž λ©”μ„œλ“œ , μΈμŠ€ν„΄μŠ€ μ‚­μ œμ‹œ μžλ™μœΌλ‘œ ν˜ΈμΆœλœλ‹€.

μΈμŠ€ν„΄μŠ€κ°€ μ‚­μ œλ λ•Œ μž‘μ—…μ΄ ν•„μš”ν•˜λ©΄ ν•΄λ‹Ή λ©”μ„œλ“œ μ•ˆμ— μ½”λ“œλ₯Ό μ‚½μž…ν•˜λ©΄ λœλ‹€.

del(μΈμŠ€ν„΄μŠ€) 

 

 

__repr__()

μΈμŠ€ν„΄μŠ€λ₯Ό print()문으둜 좜λ ₯ν• λ•Œ μ‹€ν–‰λ˜λŠ” λ©”μ„œλ“œ

print(μΈμŠ€ν„΄μŠ€) 

 

__add__()

μΈμŠ€ν„΄μŠ€ 사이에 λ§μ…ˆμž‘μ—… μ‹œ μ‹€ν–‰

μΈμŠ€ν„΄μŠ€ μ‚¬μ΄μ˜ λ§μ…ˆ μž‘μ—…μ‹œ μ‚¬μš©

 

 

__it__()

__le__()

__gt__()

__ge__()

__eq__()

__ne__()

μΈμŠ€ν„΄μŠ€ μ‚¬μ΄μ˜ 비ꡐ μ—°μ‚°μž

 

 

 

· 좔상 λ©”μ„œλ“œ

μΆ”μƒν΄λž˜μŠ€μ—μ„œ μƒμ„±λœ λ©”μ„œλ“œ / κ΅¬ν˜„μ—†μ΄ μ„ μ–Έλ§Œ λ˜μ–΄μžˆμŒ.

μ„œλΈŒ ν΄λž˜μŠ€μ—μ„œ λ©”μ„œλ“œλ₯Ό μ˜€λ²„λΌμ΄λ”©ν•΄μ„œ μ‚¬μš©ν•˜λ €λ©΄

μŠˆνΌν΄λž˜μŠ€μ—μ„œλŠ” 빈껍질 λ©”μ„œλ“œλ§Œ λ§Œλ“€κ³  pass 둜 μ±„μš°λ©΄ λœλ‹€.

class SuperClass:
    def method(self):
        pass

class SubClass1 (SuperClass):
    def method(self):
        print('μ„œλΈŒμ—μ„œ λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ”©')

class SubClass2 (SuperClass):
        pass


sub1 = SubClass1()
sub2 = SubClass2()

sub1.method()
sub2.method()

μŠˆνΌν΄λž˜μŠ€μ—μ„œ 상속받은 μ„œλΈŒν΄λž˜μŠ€λŠ”

method() λ©”μ„œλ“œλ₯Ό λ°˜λ“œμ‹œ! μ˜€λ²„λΌμ΄λ”© ν•΄μ•Ό 였λ₯˜ λ°œμƒ μ•ˆν•¨

 

 

· λ©€ν‹° μŠ€λ ˆλ“œ

Thread ν”„λ‘œκ·Έλž¨ ν•˜λ‚˜μ—μ„œ μ—¬λŸ¬κ°œλ₯Ό λ™μ‹œμ— μ²˜λ¦¬ν•  수 μžˆλ„λ‘ μ œκ³΅ν•˜λŠ” κΈ°λŠ₯

일반 ν”„λ‘œκ·Έλž¨ : μž‘μ—…1 > μž‘μ—… 2 >μž‘μ—… 3

μŠ€λ ˆλ“œ : μž‘μ—…1,2,3 λ™μ‹œμ— 처리

 

일반

import time 

class RacingCar:
	carName = ''
    def __init__(self,name) :
		self.carName = name
    
    def runCar(self):
    	for _ in range(0,3):
        	carStr = self.carName + '~~λ‹¬λ¦½λ‹ˆλ‹€'
            print (carStr, end="")
            time.sleep(0.1)

car1 = RacingCar('@μžλ™μ°¨1')

car1.runCar()

 

μŠ€λ ˆλ“œ

threading.Thread(target=λ©”μ„œλ“œorν•¨μˆ˜, args=λ§€κ°œλ³€μˆ˜) ν˜•μ‹

import threading
import time 

class RacingCar:
	carName = ''
    def __init__(self,name) :
		self.carName = name
    
    def runCar(self):
    	for _ in range(0,3):
        	carStr = self.carName + '~~λ‹¬λ¦½λ‹ˆλ‹€'
            print (carStr, end="")
            time.sleep(0.1)

car1 = RacingCar('@μžλ™μ°¨1')

th1.threading.Thread(target = car1.runCar) #μŠ€λ ˆλ“œ 생성

th1.start()

 

 

·  λ©€ν‹° ν”„λ‘œμ„Έμ‹±

μŠ€λ ˆλ“œ = cpu 1

μŠ€λ ˆλ“œ = cpu μ—¬λŸ¬κ°œ (λ©€ν‹°ν”„λ‘œμ„Έμ‹±) cpu μ½”μ–΄ μ—¬λŸ¬κ°œ μ‚¬μš© , λŒ€λŸ‰μ²˜λ¦¬ ν•„μš”μ‹œ 속도 ν–₯상

(μžλ°”μ—μ„œλŠ” λ©€ν‹°μŠ€λ ˆλ“œκ°€ λ™μ‹œμ— cpu μ—¬λŸ¬κ°œ μ‚¬μš©/ λ©€ν‹°μŠ€λ ˆλ“œ-λ©€ν‹°ν”„λ‘œμ„Έμ‹± ꡬ뢄 μ—†μŒ)

 

import multiprocessing
import time 

class RacingCar:
	carName = ''
    def __init__(self,name) :
		self.carName = name
    
    def runCar(self):
    	for _ in range(0,3):
        	carStr = self.carName + '~~λ‹¬λ¦½λ‹ˆλ‹€'
            print (carStr, end="")
            time.sleep(0.1)


if __name__ == "__main__"
	car1 = RacingCar('@μžλ™μ°¨1')

    mp1 = multiprocessing.Process(target = car1.runCar)

    mp1.start()
    
    mp1.join()