[자료구조] Ch2. Object-Oriented Programming

김규원·2024년 3월 12일
0

자료구조

목록 보기
2/14
post-thumbnail

▶ Terminology

  • instance variables = data member
  • method = member function

▶ Goals

좋은 코드의 조건

  • Robustness(안정성)
  • Adaptability(유연성)
  • Reusability(재사용가능성)

▶ Object-Oriented Design Principles

  • Modularity(모듈화)
  • Abstraction(추상화)
  • Encapsulation(캡슐화)

Abstract Data Types

Duck Typing

  • 덕 타이핑은 객체가 어떤 타입에 걸맞은 변수와 메소드를 지니면 객체를 해당 타입에 속하는 것으로 간주

만약 어떤 새가 오리처럼 걷고, 헤엄치고, 꽥꽥거리는 소리를 낸다면 나는 그 새를 오리라고 부를 것이다.

Abstract Base Cdlasses

Encapsulation

▶ Design Patterns

Object-Oriented Software Design

Responsibilities

Independence

Behaviors

UML(Unified Modeling Language)

▶ Class Definitions

self Identifier

CreditCard Example

class CreditCard:
	def __init__(self, customer, bank, acnt, linit):
    	self._customer = customer
        selr._bank = bank
        self._account = acnt
        self._limit = limit
        self._balance = 0
        
	def get_customer(self):
    	return self._customer
    def get_bank(self):
    	return self._bank
    def get_account(self):
    	return self._account
    def get_limit(self):
    	return self._limit
    def get_balance(self):
    	return self._balance
    def charge(self, price):
    	if price + self._balance > self._limit:
        	return False
        else:
        	self._balance += price
            	return True
    def make_payment(self, amount):
    	self._balance -= amount

Constructors

cc = CreditCard('John Doe`, `1st Bank', '5391 0375 9387 5309', 1000)

▶ Operator Overloading

  • the syntax a + b invokes
    addition for numeric types, yet
    concatenation for sequence types
  • When defining a new class, we must
    consider whether a syntax like a + b
    should be defined when a or b is an
    instance of that class

Iterators

Automatic Iterators

Inheritance


A Geometric Progression Class

A Fibonacci Progression Class

class FibonacciProgression(Progression):
	def __init__(self, first = 0, second = 1):
    	super().__init__(first)
        self._prev = seconde - first
        
    def _advance(self):
    	self._prev, self._current = self._current, self._prev + self._current

profile
행복한 하루 보내세요

0개의 댓글