Object Oriented Programming » History » Version 8
Amber Herold, 08/02/2011 12:54 PM
1 | 1 | Amber Herold | h1. Object Oriented Programming |
---|---|---|---|
2 | |||
3 | 3 | Amber Herold | h2. Objects |
4 | |||
5 | 5 | Amber Herold | * often models a real-world thing - person, animal, shape, car |
6 | 7 | Amber Herold | * collection of _attributes_ and _behaviors_ |
7 | 8 | Amber Herold | * defined as a _class_ with _member variables_ and _methods_ |
8 | 7 | Amber Herold | |
9 | h2. Encapsulation |
||
10 | |||
11 | |||
12 | h2. Inheritance |
||
13 | |||
14 | * reuse code by basing an object on another object |
||
15 | * terminology |
||
16 | ** Superclass, base class, parent class |
||
17 | ** Subclass, derived class, child class |
||
18 | * examples |
||
19 | ** parent: Animal, children: Person, Cat, Fish |
||
20 | ** parent: MotorVehicle, children: Car, Truck, Bus, Tractor |
||
21 | ** parent: Shape, children: Ellipse, Rectangle, Triangle, Cone |
||
22 | 1 | Amber Herold | * _Child classes_ inherit attributes and behaviors from _parent classes_ |
23 | 8 | Amber Herold | * child classes may _override_ behaviors inherited from parent classes by providing it's own implementation of a method. |
24 | |||
25 | 1 | Amber Herold | |
26 | h2. Polymorhism |
||
27 | |||
28 | h3. PHP example |
||
29 | |||
30 | <pre> |
||
31 | interface IAnimal { |
||
32 | function getName(); |
||
33 | function talk(); |
||
34 | } |
||
35 | |||
36 | abstract class AnimalBase implements IAnimal { |
||
37 | protected $name; |
||
38 | |||
39 | public function __construct($name) { |
||
40 | $this->name = $name; |
||
41 | } |
||
42 | |||
43 | public function getName() { |
||
44 | return $this->name; |
||
45 | } |
||
46 | } |
||
47 | |||
48 | class Cat extends AnimalBase { |
||
49 | public function talk() { |
||
50 | return 'Meowww!'; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | class Dog extends AnimalBase { |
||
55 | public function talk() { |
||
56 | return 'Woof! Woof!'; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | $animals = array( |
||
61 | new Cat('Missy'), |
||
62 | new Cat('Mr. Mistoffelees'), |
||
63 | new Dog('Lassie') |
||
64 | ); |
||
65 | |||
66 | foreach ($animals as $animal) { |
||
67 | echo $animal->getName() . ': ' . $animal->talk(); |
||
68 | } |
||
69 | </pre> |
||
70 | 2 | Amber Herold | |
71 | h3. Python Example |
||
72 | |||
73 | <pre> |
||
74 | class Animal: |
||
75 | def __init__(self, name): # Constructor of the class |
||
76 | self.name = name |
||
77 | def talk(self): # Abstract method, defined by convention only |
||
78 | raise NotImplementedError("Subclass must implement abstract method") |
||
79 | |||
80 | class Cat(Animal): |
||
81 | def talk(self): |
||
82 | return 'Meow!' |
||
83 | |||
84 | class Dog(Animal): |
||
85 | def talk(self): |
||
86 | return 'Woof! Woof!' |
||
87 | |||
88 | animals = [Cat('Missy'), |
||
89 | Cat('Mr. Mistoffelees'), |
||
90 | Dog('Lassie')] |
||
91 | |||
92 | for animal in animals: |
||
93 | print animal.name + ': ' + animal.talk() |
||
94 | |||
95 | # prints the following: |
||
96 | # |
||
97 | # Missy: Meow! |
||
98 | # Mr. Mistoffelees: Meow! |
||
99 | # Lassie: Woof! Woof! |
||
100 | </pre> |