Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Lecture#3

.pdf
Скачиваний:
4
Добавлен:
23.02.2015
Размер:
55.16 Кб
Скачать

Classes inheritance

class Primitive(object): field = 5.

def get_field(self):

return self.field

class NewPrimitive(Primitive): def get_field(self):

print self.field return self.field

Classes inheritance

NewPrimitive can be used instead of Primitive

Child classes can

add functionality to parent class change functionality of parent class

NewPrimitive is Primitive

Classes

multiple inheritance

class Primitive(object): field = 5.

class AnotherPrimitive(object): anotherfield = 10.

class Derived(Primitive, AnotherPrimitive): def get_fields(self):

print field

print anotherfield

Classes

multiple inheritance

No conflicts - no problems

Classes polymorphism

class LinearAccelerator(object): def collide(self):

print “I use steady target” class Collider(object):

def collide(self):

print “I use two beams”

Both can collide. But differently.

Classes encapsulation

Refer to:

hiding data inside classes (i.e. private methods and attrs)

bundling data and operations on it inside classes

Classes encapsulation

class Cl(object):

_field = 5. #indicates “for internal use” __priv = 10. #magled, i.e. __Cl_priv

Encapsulation is mostly bundling in Python

Classes composition

class P(object):

pass

class R(object):

def set_P(self, P): self.P = P

R “has” or “is composed of” P

Modules

A way to structure code #a.py:

class P(object): pass

#b.py import a p = a.P()

Modules import syntax

import <module>

import <module> as <newname>

from <module> import <name1>, <name2> from <module> import *

from <module> import <name> as <name2>

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]