Introduction to Computer Science and Programming Using Python(5)

 

Week Five: Object Oriented Programming

1. Object Oriented Programming

Object

OOP

Power of OOP

2. Class,Instances,Method

  • Object Oriented Programming: Classes and Instances

    • it provides the blueprint to create individual instances that share the same structure but have their own set of characteristics

    • class methods instance object type
      集合(抽象概念) 集合属性/参数/动作 具体例子 所有实体 实体概念概念归属
      House house attributs: bedrooms, gardens, price your house bricks, woods, beams 硬装,水电,家具
    1. What is Class?

      Relationship between class, methods, instance

    2. init

      __init__

    3. instance

      instance

      instance attributes

      instance example

    4. You are an example

      Human

  • Method:procedural attribute, like a function that works only with this class. Object Oriented Programming: Methods

    • What are methods?

      what are methods

    • How to call methods?

      how to call methods

  • class Coordinate(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y
        def distance(self, other):
            x_diff_sq = (self.x - other.x)**2
            y_diff_sq = (self.y - other.y) ** 2
            return (x_diff_sq + y_diff_sq)** 0.5
        def __str__(self): #print()打印函数
            return "I am glad to see you!"
    
  • Special Operators:Basic Customization

  • Example 1:Fraction

    ```
    # two intergers: numerator;denominator
    # goals: 1.print representation; 2. add,subtract; 3. convert to a float
    ```
    class fraction(object):
        def __init__(self, numer, denom):
            self.numer = numer
            self.denom = denom
        def __str__(self):
            return str(self.numer) + ' / ' + str(self.denom)
        def getNumer(self):
            return self.numer
        def getDenom(self):
            return self.denom
        def __add__(self, other):
            numerNew = other.getDenom() * self.getNumer() \
                       + other.getNumer() * self.getDenom()
            denomNew = other.getDenom() * self.getDenom()
            return fraction(numerNew, denomNew)
        def __sub__(self, other):
            numerNew = other.getDenom() * self.getNumer() \
                       - other.getNumer() * self.getDenom()
            denomNew = other.getDenom() * self.getDenom()
            return fraction(numerNew, denomNew)
        def convert(self):
            return self.getNumer() / self.getDenom()
    
  • Example 2: A set of integers

    ```
    # a collection of integers
    # a particular integer appears only once in a set
    ```
      
    

3. Hierarchies

graph BT
Person--> Animal
Cat --> Animal
Rabbit --> Animal
Student --> Person

Class and OOP

4. Generators

Generators: any procedure or method with yield statement called a generator.

Why Generators?

def genFib():
    fibn_1 = 1
    fibn_2 = 0
    while True:
        next = fibn_1 + fibn_2
        yield next #相当于range的功能
        fibn_2 = fibn_1
        fibn_1 = next

        
>>> fib = genFib()
>>> fib.__next__()
1
>>> fib.__next__()
2
>>> fib.__next__()
3
>>> fib.__next__()
5
>>> fib.__next__()
8
>>> fib.__next__()
13