본문 바로가기

Python_Beginer/Study

Python Calss Basic Study - 직원 관리 Class

반응형

1. Class

- 구현화 대상(Object) : 직원(Employee)

- 구현화 대상의 속성(Data) : 사번(pob) / 이름(name) / 부서(dep) / 연봉(salary)

- 구현화 속성의 기능(Fucntion) : 부서 변경(changeDep) / 연봉 변경(changeSalary)


2. Class Sample Code

- Class 정의

class Employee:
def __init__(self, pob, name, dep, salary):
self.pob = pob
self.name = name
self.dep = dep
self.salary = salary

def __str__(self):
return '(주)웅이전자 직원 명부 -> 사번: %d, 이름: %s, 부서: %s, 연봉: %f' % (self.pob, self.name, self.dep, self.salary)

def changeDept(self, newDept):
self.dep = newDept

def changeSalary(self, percent):
self.salary = (1 + percent) * self.salary

- Instance 호출(Class 호출)

* 사원 홍길동 생성(사번 : 1 / 이름 : 홍길동 / 부서 : 총무부 / 연봉 : 2000)

emp1 = Employee(1, '홍길동', '총무부', 2000)

* 사원 홍길동 정보 조회

print(emp1)

(주)웅이전자 직원 명부 -> 사번: 1, 이름: 홍길동, 부서: HR, 연봉: 2200.000000


Process finished with exit code 0

* 사원 홍길동 사번 조회

print(emp1.pob)

1


Process finished with exit code 0

* 사원 홍길동 부서 조회

print(emp1.dep)

총무부


Process finished with exit code 0

* 사원 홍길동 부서 발령(총무부 -> HR)

emp1.changeDept('HR')

* 사원 홍길동 부서 발령 후 부서 조회

print(emp1.dep)

HR


Process finished with exit code 0

* 사원 홍길동 연봉 조회

print(emp1.salary)

2000


Process finished with exit code 0

* 사원 홍길동 연봉 협상(10% 인상 / 연봉 변경)

emp1.changeSalary(0.1)

* 사원 홍길동 연봉 협상 후 조회

print(emp1.salary)

2200.0


Process finished with exit code 0

* 사원 홍길동 직원 관리 변경 후 정보 재 조회

print(emp1)

(주)웅이전자 직원 명부 -> 사번: 1, 이름: 홍길동, 부서: HR, 연봉: 2200.000000


Process finished with exit code 0

* 사원 이순신 입사(사번 : 1 / 이름 : 이순신 / 부서 : IT / 연봉 : 3000)

emp2 = Employee(101, '이순신', 'IT', 3000)

* 사원 이순신 정보 조회

print(emp2)

(주)웅이전자 직원 명부 -> 사번: 101, 이름: 이순신, 부서: IT, 연봉: 3000.000000


Process finished with exit code 0

반응형