| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- Linux
- 밑바닥부터 시작하는 딥러닝2
- Python
- CPP
- C++
- Multimedia
- Operating System
- computer vision
- ROS2
- SQLD
- deep learning
- Baekjoon
- Robocup@Home 2026
- Gentoo2
- Data Science
- System Call
- Seoul National University
- Optimization
- cs231n
- BFS
- On-memory file system
- DFS
- Machine Learning
- file system
- Humble
- paper review
- CNN
- RNN
- do it! 알고리즘 코딩테스트: c++편
- Process
Archives
- Today
- Total
newhaneul
[Advanced Python Programming] Lecture 6. Object 본문
4. University Study/Advanced Python Programming
[Advanced Python Programming] Lecture 6. Object
뉴하늘 2026. 4. 12. 22:34728x90
포스팅은 인하대학교 허혜선 교수님의 [202601-EEC3408-001] 고급파이썬프로그래밍을 수강하고 공부한 내용을 정리하기 위한 포스팅입니다.
- private 속성
- 객체 지향 프로그래밍에서는 객체의 속성과 메서드를 하나의 캡슐처럼 묶어서 관리하는 캡슐화(encapsulation)가 핵심
- 객체 내부에 구현된 세부 사항을 숨기고 외부에는 객체의 속성과 메서드에 접근하는 인터페이스만 제공하는 것
- 속성을 private으로 설정하여 접근하지 못하도록 설정
__속성이름
LAB 원 클래스와 객체
import math
class Circle:
def __init__(self, radius):
self.__radius = radius
def get_radius(self):
return self.__radius
def set_radius(self, radius):
self.__radius = radius
def get_area(self):
return math.pi * self.__radius ** 2
def get_circumference(self):
return 2 * math.pi * self.__radius
c1 = Circle(7)
print('넓이: ', c1.get_area())
print('둘레: ', c1.get_circumference())
c1.set_radius(10)
print('넓이: ', c1.get_area())
print('둘레: ', c1.get_circumference())
넓이: 153.93804002589985
둘레: 43.982297150257104
넓이: 314.1592653589793
둘레: 62.83185307179586
LAB 학생 클래스와 객체
class Student:
def __init__(self, name, math, computer):
self.__name = name
self.__math = math
self.__computer = computer
def get_name(self):
return self.__name
def set_math(self, math):
self.__math = math
def set_computer(self, computer):
self.__computer = computer
def get_average(self):
return (self.__math + self.__computer) / 2
s1 = Student('hanbit', 95, 89)
print(s1.get_name(), s1.get_average())
s1.set_computer(97)
print(s1.get_name(), s1.get_average())
hanbit 92.0
hanbit 96.0
LAB 도형 클래스를 상속받는 원과 직사각형 클래스의 객체 생성
class Figure:
def __init__(self, area, perimeter):
self.area = area
self.perimeter = perimeter
def get_area(self):
return self.area
def get_perimeter(self):
return self.perimeter
class Circle(Figure):
def __init__(self, radius):
area = math.pi * radius ** 2
perimeter = 2 * math.pi * radius
super().__init__(area, perimeter)
self.radius = radius
def set_radius(self, radius):
self.radius = radius
self.area = math.pi * radius ** 2
self.perimeter = 2 * math.pi * radius
def get_radius(self):
return self.radius
class Rectangle(Figure):
def __init__(self, width, height):
area = width * height
perimeter = 2 * width + 2 * height
super().__init__(area, perimeter)
self.width = width
self.height = height
def set_height(self, height):
self.height = height
self.area = self.height * self.width
self.perimeter = 2 * self.height + 2 * self.width
def set_width(self, width):
self.width = width
self.area = self.height * self.width
self.perimeter = 2 * self.height + 2 * self.width
def get_height(self):
return self.height
def get_width(self):
return self.width
f1 = Circle(7)
print(f1.get_area(), f1.get_perimeter())
f1.set_radius(10)
print(f1.get_area(), f1.get_perimeter())
f2 = Rectangle(5, 4)
print(f2.get_area(), f2.get_perimeter())
f2.set_width(7)
print(f2.get_area(), f2.get_perimeter())
153.93804002589985 43.982297150257104
314.1592653589793 62.83185307179586
20 18
28 22
[플러스 예제] 자동차 클래스와 객체
Car:
def __init__(self, model, fuelefficiency):
self.__model = model
self.__fuelefficiency = fuelefficiency
def get_model(self):
return self.__model
def get_fuelefficiency(self):
return self.__fuelefficiency
def set_fuelefficiency(self, fuelefficiency):
self.__fuelefficiency = fuelefficiency
def get_fuelamount(self, distance):
return distance / self.__fuelefficiency
car1 = Car('GV70', 12)
print('모델명: ', car1.get_model())
print('연비: ', car1.get_fuelefficiency())
print('연료량: ', car1.get_fuelamount(240))
모델명: GV70
연비: 12
연료량: 20.0728x90
'4. University Study > Advanced Python Programming' 카테고리의 다른 글
| [Advanced Python Programming] Lecture 8. Module and Package (0) | 2026.04.16 |
|---|---|
| [Advanced Python Programming] Lecture 7. Special Methods (1) | 2026.04.12 |
| [Advanced Python Programming] Lecture 5. Function (2) | 2026.04.12 |
| [Advanced Python Programming] Lecture 4. Applications of Lists and Tuples (0) | 2026.04.12 |
| [Advanced Python Programming] Lecture 3. Dictionary (0) | 2026.04.12 |