Languages/Python
[Python] λ€μ€μμ, μΆμν΄λμ€
μ 보보μπ
2020. 11. 24. 12:24
λ°μν
λ€μ€ μμ
• μ¬λ¬ κ°μ ν΄λμ€λ‘λΆν° μμλ°λ κ²½μ°
νμ΄μ¬μ λ€μ€ μμμ μ§μνκ³ λΆλͺ¨ ν΄λμ€μ λμΌν λ©μλλ μμ±μ΄ μμ λλ μΌμͺ½μμλΆν° μ°μ κΆ μ λΆμ¬ νλ€.
μμ )
#1 μμ
class Person:
def greeting(self):
print('μλ
νμΈμ.')
class University:
def manage_credit(self):
print('νμ κ΄λ¦¬')
class Undergraduate(Person, University):
def study(self):
print('곡λΆνκΈ°')
sunja = Undergraduate()
sunja.greeting()
sunja.manage_credit()
sunja.study()
#2 μμ
class Person:
def sleep(self):
print('sleep')
class Student(Person):
def study(self):
print('Study hard')
def play(self):
print('play with friends')
class Worker(Person):
def work(self):
print('Work hard')
def play(self):
print('drinks alone')
##λ€μ€ μμ##
class PartTimer(Student, Worker):
def find_job(self):
print('Find a job')
parttimer1 = PartTimer
parttimer1.study()
parttimer1.work()
parttimer1.play()
μΆμ ν΄λμ€
- λ©μλμ μ΄λ¦λ§ κ°μ§κ³ μλ€κ° μμλ°λ ν΄λμ€μμ λ©μλ ꡬν μ κ°μ νκΈ° μν΄ μ¬μ©
- μμμ ν΅ν΄μ μμ ν΄λμ€μμ μΈμ€ν΄μ€λ₯Ό μμ±ν΄μΌν¨
- μΆμν΄λμ€μ νΉμ§
• μμ μ μΈμ€ν΄μ€λ₯Ό μμ±ν μ μμ
• μΆμ ν΄λμ€λ μ€μ§ μμμμλ§ μ¬μ©
• κ°κ°μ λ 립λ 곡ν΅μ μΈ κΈ°λ₯μ΄ κ°μ 곡μ νλ©΄ μλλ―λ‘ κ΅¬ννμ§ μμ λ©μλλ₯Ό μ¬μ©
μμ 1)
from abc import *
class AbstractCountry(metaclass=ABCMeta):
name = 'κ΅κ°λͺ
'
population = 'μΈκ΅¬'
capital = 'μλ'
def show(self):
print("κ΅κ° ν΄λμ€μ λ©μλ μ
λλ€.")
@abstractmethod
def show_capital(self):
super().show_capital()
class Korea(AbstractCountry):
def __init__(self, name, population, capital):
self.name = name
self.population = population
self.capital = capital
def show_name(self):
print("κ΅κ° μ΄λ¦μ : ", self.name)
def show_capital(self):
print("{0} μλλ {1} μ
λλ€.".format(self.name, self.capital))
a = Korea("λνλ―Όκ΅", 50000000, "μμΈ")
a.show_name()
a.show_capital()
μμ 2)
#abc_naver_news_crawler.py
from abc_crawlerBase import CrawlerBase
class NaverNewsCrawler(CrawlerBase):
def run(self):
print("naver run")
def parse_html(self, text):
pass
if __name__ == '__main__':
naver_news_crawler = NaverNewsCrawler()
naver_news_crawler.run()
# abc_daum_news_crawler.py
from abc_crawlerBase import CrawlerBase
class DaumNewsCrawler(CrawlerBase):
def run(self):
print('daum run')
def parse_html(self, text):
pass
if __name__ == '__main__':
daum_news_crawler = DaumNewsCrawler()
daum_news_crawler.run()
# abc_crawlerBase.py
from abc import *
class CrawlerBase(metaclass=ABCMeta):
@abstractmethod
def run(self):
pass
@abstractmethod
def parse_html(self, text):
pass
λ°μν