#이름
SmartPhone
#속성(변수)
owner
number
battery
appList
#기능(함수)
call(phoneNUm)
hwCall(phoneNum)
getBatteryStatus()
class 클래스_이름:
# 생성자
def __init__(self, 매개_변수_리스트):
self.객체_변수_이름 = 클래스_매개_변수_리스트 # 인스턴스 변수 정의(초기화)
#메소드
def 이름(self, 매개_변수_리스트):
코드_블록
class Person:
def __init__(self, name, age):
self.name = name # 인스턴스 변수 name을 초기화
self.age = age # 인스턴스 변수 age를 초기화
#메서드 생성
def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
# 인스턴스 생성
person1 = Person("Alice", 30)
person2 = Person("Bob", 25)
# 메서드 호출
person1.greet() >> Hello, my name is Alice and I am 30 years old.
person2.greet() >> Hello, my name is Bob and I am 25 years old.
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def car(self):
print(f"{self.make} {self.model}")
result = Car('Kia','K3')
result.car()
✔ 다형성: 동일한 함수가 다르게 작동하는 것
✔ 상속
super().__init__(make,model) # 부모 클래스의 생성자 호출
super().car() # 부모 클래스의 func() 호출
✔ 오버라이딩(Overriding): 부모 클래스의 소메소드를 자식 클래스에 재정의하며 새로운 동작 구현
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
def speak(self):
return super().speak() + " Woof"
my_dog = Dog()
print(my_dog.speak())
>> Some sound Woof
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def start(self):
print(f"{self.make} {self.model} is starting")
class ElectricCar(Car):
def __init__(self, make, model, battery_size):
super().__init__(make, model)
self.battery_size = battery_size
def start(self):
print(f"{self.make} {self.model} with a {self.battery_size} battery is starting")
my_electric_car = ElectricCar('Tesla', 'Model S', '75kWh')
my_electric_car.start()
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
class ElectricCar(Car):
def __init__(self, make, model, batterysize):
super().__init__(make,model)
self.batterysize = batterysize
def my_Car():
my_car = ElectricCar('Kia','K3','75kWh')
print(f"{my_car.make} {my_car.model} {my_car.batterysize}")
my_Car()
import math
class Rectangle:
def __init__(self, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def calcArea(self):
width = abs(self.x2 - self.x1)
height = abs(self.y2 - self.y1)
return width * height
class Circle:
def __init__(self, x, y, r):
self.x = x
self.y = y
self.r = r
def calcArea(self):
return math.pi * (self.r ** 2)
shapeList = []
for i in range(3):
s = input("도형 모양을 입력하세요 (1:사각형 0:원): ")
if s == "1":
x1 = int(input("왼쪽 상단의 x좌표를 입력: "))
y1 = int(input("왼쪽 상단의 y좌표를 입력: "))
x2 = int(input("오른쪽 하단의 x좌표를 입력: "))
y2 = int(input("오른쪽 하단의 y좌표를 입력: "))
shapeList.append(Rectangle(x1, y1, x2, y2))
elif s == "0":
x = int(input("원의 중심 x 좌표를 입력: "))
y = int(input("원의 중심 y 좌표를 입력: "))
r = int(input("원의 반지름을 입력: "))
shapeList.append(Circle(x, y, r))
else:
print("유효하지 않은 입력입니다. 0 또는 1을 입력해주세요.")
for s in shapeList:
print(f"면적: {s.calcArea()}")
import math
class Rectangle:
def __init__(self):
self.x1 = 0
self.y1 = 0
self.x2 = 0
self.y2 = 0
def input_data(self):
self.x1 = int(input("왼쪽상단 x 좌표: "))
self.y1 = int(input("왼쪽상단 y 좌표: "))
self.x2 = int(input("오른쪽하단 x 좌표: "))
self.y2 = int(input("오른쪽하단 y 좌표: "))
def calcArea(self):
width = abs(self.x2 - self.x1)
height = abs(self.y2 - self.y1)
return width * height
class Circle:
def __init(self):
self.x = 0
self.y = 0
self.r = 0
def input_data(self):
self.x = int(input("원 중심 x 좌표: "))
self.y = int(input("원 중심 y 좌표: "))
self.r = int(input("원 반지름 길이: "))
def calcArea(self):
return math.pi * (self.r ** 2)
shapeList = []
for i in range(3):
s = input("도형 모양을 입력하세요 (1: 사각형 0:원): ")
if s == '1':
shape = Rectangle()
elif s == '0':
shape = Circle()
else:
print("0 또는 1을 입력하세요")
continue
shape.input_data()
shapeList.append(shape)
for shape in shapeList:
print(f"도형의 면적: {shape.calcArea()}")

class Point:
def __init__(self,x, y):
self.x = x
self.y = y
def setX(self):
self.x = int(input("변수 x값 내용을 입력하세요: "))
def setY(self):
self.y = int(input("변수 y값 내용을 입력하세요: "))
def getX(self):
return self.x
def getY(self):
return self.y
point = Point(0,0) #인스턴스 생성
point.setX() #메서드 호출
point.setY()
print(f"x값: {point.getX()}")
print(f"y값: {point.getY()}")

class Car:
def __init__(self, company, year, color):
self.company = company
self.year = year
self.color = color
def __str__(self):
return f"자동차 회사: {self.company}, 년식: {self.year}, 색상: {self.color}"
def __eq__(self, other):
if isinstance(other, Car):
return self.company == other.company and self.year == other.year and self.color == other.color
return False
mycar = Car('ABC', 2020, '검정')
yourcar = Car('DEF',2021, '백색')
print(mycar)
print(yourcar)
print(mycar == yourcar)

class Problem:
def __init__(self,students):
self.students = students
def subproblem1(self):
max = 0
yea = 0
for i in range(2011,2022):
if self.students[i-1] - self.students[i] >= max:
max = self.students[i-1] - self.students[i]
year = self.students[i]
print(f"(1) 전해에 비해 가장 급격하게 학생수가 줄어든 해는 {year: .1f}입니다")
def subproblem2(self):
year = 0
for i in range(2010,2022):
if self.students[i] < 30 :
year = self.students[i]
print(f"(2)학급당 학생수가 30명 미만으로 떨어진 해는 {year}입니다")
def subproblem3(self):
sum = 0
for i in range(2011,2022):
sum += self.students[i-1]-self.students[i]
print(f"(3)2010년부터 2021년사이 평균적으로 감소한 학생수는 {sum /11: .2f}입니다")
d = {2010:33.7, 2011:33.1, 2012:32.5, 2013:31.9, 2014:30.9, 2015:30.0, 2016:29.3, 2017:28.2, 2018:26.2, 2019:24.5, 2020:23.4, 2021:23}
result = Problem(d)
result.subproblem1()
result.subproblem2()
result.subproblem3()
