Python 기초09

Doya·2025년 2월 17일

ESTSOFT_AI개발7기

목록 보기
10/43

개요

  • 2월 17일 교육 내용
  • 클래스 내용 보충

클래스

객체 지향 언어의 특징

  1. 캡슐화(Encapsulation)
  • 객체를 캡슐로 싸서 내부를 보호하고 은닉하는 것
  • 외부의 접근으로 부터 안전하게 보호
  • 클래스라는 캡슐을 사용, 필드(맴버 변수)와 메소드(멤버 함수)로 구성
  1. 상속(Inheritance)
  • 자식 클래스가 부모 클래싀 속성으 물려받고 기능을 추가하여 확장(Extends)하는 개념
  • 필드와 메소드를 물려받아 코드를 재사용함
  • 코드 작성에 드는 시간과 비용을 줄임
  1. 다형성(Polymorphism)
  • 같은 이름의 메소드가 클래스 혹은 객체에 따라 다르게 동작하도록 구현되는 것
  • 부모 클래스에서 구현된 메소드를 서브 클래스에서 동일한 이름으로 자신의 특징에 맞게 다시 구현이 가능함 -> 오버라이딩
  • 클래스내 이름이 같지만 서로 다르게 동작하는 메소드를 여러 개 만들 수 있음 -> 메소드 오버로딩

실습

class EMP:
  name = ' '

  def insert(self, name):
    print('EMP insert함수 ',name)
    self.name = name
  
  def printAll(self):
    print('Emp PrintAll함수', self.name)
    print()

ob = EMP()
ob.insert('길동')
ob.printAll()
#  2교시 실습 
class Employee:
  def __init__(self, emp_id, name, pay):
    self.emp_id = emp_id
    self.name = name
    self.pay = pay
  
  def display(self):
    print(f'아이디: {self.emp_id} 이름: {self.name} 급여: {self.pay}')
  
  def update_pay(self, new_pay):
    self.pay = new_pay
    print(f'{self.name}님 월급이 {self.pay}로 변경되었습니다 ')


object1 = Employee(2025101, "김연아",  7000)
object2 = Employee(2025102, "손흥민",  4500)

object1.display()
object2.display()

# 월급수정
object1.update_pay(9700)
object2.update_pay(10000)
print()
# Class Employ를 활용 
class EmployeeManager:
  mylist = [ ]

  def add_employee(self, emp_id, name, pay):
    employee = Employee(emp_id, name, pay)
    self.mylist.append(employee)
    print(f'{name} 사원님 추가되었습니다')

  def display_all_employees(self):
    if not self.mylist:
      print('현재 직원이 없습니다.')
      return
    print('\n전체직원출력')
    for emp in self.mylist:
      emp.display()
  
  def find_employees_by_id(self, emp_id):
    for emp in self.mylist:
      if emp.emp_id == emp_id:
        print('\n------ 검색된 직원 정보 -------')
        emp.display()
        return
    print(f'\n{emp_id}사번의 직원을 찾을 수 없습니다\n')

# 클래스 객체화
manager = EmployeeManager()

manager.add_employee(101,  '페이커',  5000)
manager.add_employee(102,  '고길동',  4000)
manager.add_employee(103,  '유재석',  3500)

emp_id_num = 0


#전체 출력
manager.display_all_employees()

#검색 
emp_id_num = int(input("사원번호를 입력하세요: "))
manager.find_employees_by_id(emp_id_num)

실습

1. 문자열 암호화

# 과제 
# 문자열에서 암호화 
# http://, https:// 제외한 도메인이름시작 3글자 접두사 가져오기 
# 도메인이름 글자수 , e라는 포함 갯수, 
# 권장 변수 domain, index, pw1, pw2, pw3, password
# strUrl = 'http://kakao.com' kak50!
#  strUrl = 'http://google.com' kak61!
# strUrl = 'http://naver.com' kak51!
# strUrl = 'http://estsoft.com' est71!
# 클래스 X

strUrl1 = 'http://kakao.com' 
strUrl2 = 'http://google.com'
strUrl3 = 'http://naver.com'
strUrl4 = 'http://estsoft.com'

def url_encryption(strUrl):
  domain = strUrl.replace('http://','').replace('.com','')
  index = sum(ord(char) * 33 for char in domain)
  index_count = domain.count('o')
  return f'{index}{index_count}#'


result1 = url_encryption(strUrl1)
result2 = url_encryption(strUrl2)
result3 = url_encryption(strUrl3)
result4 = url_encryption(strUrl4)

print(result1)
print(result2)
print(result3)
print(result4)

2. 클래스 사용해 보기

# 과제
# house1 = KBHouse('제주', '아파트', '매매', '2억', '2025년')
# house2 = KBHouse('독도', '오피스텔', '전세', '1억', '2025년')
# house3 = KBHouse('서울', '단독빌라', '월세', '500/30', '2025년')
# 클래스 반드시 사용 



class KBHouse:
  deal_list = []
  
  def __init__(self, location, house_type, deal_type, price, year):
    self.location = location  
    self.house_type = house_type  
    self.deal_type = deal_type  
    self.price = price 
    self.year = year  
  
  def view_info(self):
    print(f"{self.year}  {self.location}  {self.house_type}  {self.deal_type}  {self.price}")
  

  

house1 = KBHouse('제주', '아파트', '매매', '2억', '2025년')
house2 = KBHouse('독도', '오피스텔', '전세', '1억', '2025년')
house3 = KBHouse('서울', '단독빌라', '월세', '500/30', '2025년')

house1.view_info()
house2.view_info()
house3.view_info()

profile
안녕하세요. 도야입니다

0개의 댓글