#1 ๋ค์ ์ฝ๋์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ฐ์์ค.
class Company:
def __init__(self):
self.work = True
self.name = "Jane"
self.gender = "woman"
def retire(self):
self.work = False
class Employee(Company):
def __init__(self, name, gender):
super().__init__()
self.name = name
self.gender = gender
def introduce(self):
if self.work == True:
print("I got a job at a company")
print("My name is", self.name)
print("I am a", self.gender)
if self.work == False:
print("I left the company")
em = Employee("james", "man")
em.retire()
em.introduce()
Employee("james", "man").introduce()
-> ๋ต :
I left the company
I got a job at a company
My name is james
I am a man
#2 ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ์ ๋, ์คํ ๊ฒฐ๊ณผ๋ก ์๋ง์ ๊ฒ์?
class SoccerPlayer(object):
def __init__(self, name, position, back_number):
self.name = name
self.position = position
self.back_number = back_number
def change_back_number(self, back_number):
self.back_number =back_number
jinhyun = SoccerPlayer("jinhyun", "MF", 10)
print("ํ์ฌ ์ ์์ ๋ฑ๋ฒํธ๋:", jinhyun.back_number)
jinhyun.change_back_number(5)
-> ๋ต:
ํ์ฌ ์ ์์ ๋ฑ๋ฒํธ๋: 10
#3 ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ์ ๋, ์คํ ๊ฒฐ๊ณผ๋ก ์๋ง์ ๊ฒ์?
class Marvel(object):
def __init__(self, name, characteristic):
self.name = name
self.characteristic = characteristic
def __str__(self):
return f"My name is {self.name} and my weapon is {self.characteristic}"
class Villain(Marvel):
pass
first_villain = Villain("Thanos", "infinity gauntlet")
print(first_villain)
-> ๋ต:
My name is Thanos and my weapon is infinity gauntlet
#4 ๋ค์ ์ฝ๋์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ฐ์์ค.
class TV(object):
def __init__(self, size, year, company):
self.size = size
self.year = year
self.company = company
def describe(self):
print(self.company + "์์ ๋ง๋ " + self.year + "๋
ํ" + self.size + "์ธ์น" + "TV")
class Laptop(TV):
def describe(self):
print(self.company + "์์ ๋ง๋ " + self.year + "๋
ํ" + self.size + "์ธ์น" + "๋
ธํธ๋ถ")
LG_TV = TV("32", "2002", "LG")
LG_TV.describe()
samsung_microwave = Laptop("15", "2023", "Samsung")
samsung_microwave.describe()
-> ๋ต:
LG์์ ๋ง๋ 2002๋ ํ32์ธ์นTV
Samsung์์ ๋ง๋ 2023๋ ํ15์ธ์น๋ ธํธ๋ถ
#5 4๋ฒ๊ณผ ๊ฐ์ ์ฝ๋์ ์ค๋ช ์ ์๋์ ๊ฐ๋ค. ๋น ์นธ์ ์๋ง์ ๋จ์ด๋ฅผ ๋ฃ์ผ์์ค.
์ ์ฝ๋์์ Laptop ํด๋์ค๋ TVํด๋์ค๋ฅผ (๊ฐ)ํ์๋ค. ๋ํ, ๊ฐ์ ์ด๋ฆ์ ๋ด๋ถ ๋ก์ง์ ๋ค๋ฅด๊ฒ ์์ฑํ์ผ๋ฏ๋ก (๋)์ ์ฌ๋ก๋ก๋ ๋ณผ ์ ์๋ค.
-> ๋ต : (๊ฐ) - ์์, (๋) - ๋คํ์ฑ
#6 ๋ค์ ์ฝ๋์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ฐ์์ค.
class Person:
def __init__(self, name, age, position):
self.Name = name
self.Age = age
self.Position = position
def show_info(self):
print(f"์ด๋ฆ : {self.Name}")
print(f"๋์ด : {self.Age}")
print(f"์ง์ : {self.Position}")
print(f"์ ๋ ํ๋น๋ํ๊ต {self.Position} {self.Name}์
๋๋ค. ๋์ด๋ {self.Age}์
๋๋ค.")
class Researcher(Person):
def __init__(self, name, age, position, degree):
Person.__init__(self, name, age, position)
self.Degree = degree
def show_info(self):
Person.show_info(self)
print(f"์ ๋ {self.Degree}์
๋๋ค.")
if __name__=="__main__":
researcher_john = Researcher("John", "22", "์ฐ๊ตฌ์", "ํ์ฌ")
researcher_tedd = Researcher("Tedd", "40", "์์ฅ", "๋ฐ์ฌ")
researcher_john.show_info()
print("=" * 50)
researcher_tedd.show_info()
-> ๋ต:
์ด๋ฆ : John
๋์ด : 22
์ง์ : ์ฐ๊ตฌ์
์ ๋ ํ๋น๋ํ๊ต ์ฐ๊ตฌ์ John์ ๋๋ค. ๋์ด๋ 22์ ๋๋ค.
์ ๋ ํ์ฌ์ ๋๋ค.
==================================================
์ด๋ฆ : Tedd
๋์ด : 40
์ง์ : ์์ฅ
์ ๋ ํ๋น๋ํ๊ต ์์ฅ Tedd์ ๋๋ค. ๋์ด๋ 40์ ๋๋ค.
์ ๋ ๋ฐ์ฌ์ ๋๋ค.
#7 ๋ค์ ์ฝ๋์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ฐ์์ค.
class Terran(object):
def __init__(self, mineral):
self.scv = 4
self.marine = 0
self.medic = 0
self.mineral = mineral
def command(self, SCV=False):
self.mineral += 8*self.scv
if SCV:
self.scv += 1
self.mineral -= 10
def barrack(self, Marine = False, Medic=False):
self.mineral +=8*self.scv
if Marine:
self.marine += 1
self.mineral -= 15
if Medic:
self.medic += 1
self.mineral -= 15
def check_source(self):
print("Mineral: "+ str(self.mineral))
User = Terran(50)
User.command(True)
User.barrack(True, True)
User.check_source()
-> ๋ต:
Mineral: 82
#8 ๋ค์ ์ฝ๋์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ฐ์์ค.
class IceCream(object):
def __init__(self, flavor):
self.flavor = flavor
def change_flavor(self, new_flavor):
print("์์ด์คํฌ๋ฆผ์ %s์์ %s๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์."% (self.flavor, new_flavor))
self.flavor = new_flavor
print("์์ด์คํฌ๋ฆผ ๋ง์ %s๋ก ๋ณ๊ฒฝํด๋๋ ธ์ด์."%self.flavor)
ice_cream = IceCream("๋ ์ธ๋ณด์ฐ ์ค๋ฒ ํธ")
ice_cream.change_flavor("๋ฐ๋๊ณผ ํจ๊ป ์ฌ๋ผ์ง๋ค")
-> ๋ต:
์์ด์คํฌ๋ฆผ์ ๋ ์ธ๋ณด์ฐ ์ค๋ฒ ํธ์์ ๋ฐ๋๊ณผ ํจ๊ป ์ฌ๋ผ์ง๋ค๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์.
์์ด์คํฌ๋ฆผ ๋ง์ ๋ฐ๋๊ณผ ํจ๊ป ์ฌ๋ผ์ง๋ค๋ก ๋ณ๊ฒฝํด๋๋ ธ์ด์.
'๐ ์คํฐ๋ > ํ์ด์ฌ ์คํฐ๋ ๊ณผ์ ์ ์ถ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[3ํ/๊น์๊ฒฝ] 8์ฐจ์ ํ์ด์ฌ ๊ณผ์ - ๊ฐ์ฒด์งํฅ (0) | 2023.05.24 |
---|---|
[3ํ/์์์ฐ] ํ์ด์ฌ ๊ณผ์ -๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ (0) | 2023.05.24 |
[2ํ/์ด์ ์ง] 8์ฐจ์ ํ์ด์ฌ ๊ณผ์ - ๊ฐ์ฒด ์งํฅํ ๊ณผ์ (0) | 2023.05.19 |
[2ํ/์ค์ํ] 8์ฐจ์ ํ์ด์ฌ ๊ณผ์ - ๊ฐ์ฒด ์งํฅํ ๊ณผ์ (0) | 2023.05.19 |
[1ํ/์ด๋์ฐ] 7์ฐจ์ ํ์ด์ฌ๊ณผ์ - ์๋ฃ๊ตฌ์กฐ (0) | 2023.05.18 |