08-classes-objects.ppt
-
Upload
karthickklr32 -
Category
Documents
-
view
216 -
download
0
Transcript of 08-classes-objects.ppt
-
8/17/2019 08-classes-objects.ppt
1/16
Unit 8
Classes and Objects; Inheritance
Special thanks to Roy McElmurry, John Kurkoski, Scott Shacro!t, Ryan "ucker, #aul $eck !ortheir ork%
E&cept here otherise noted, this ork is licensed under'http'((creati)ecommons%or*(licenses(by+nc+sa(%-
-
8/17/2019 08-classes-objects.ppt
2/16
2
OOP, Defning a Class
. #ython as built as a procedural lan*ua*e/ OO# e&ists and orks 0ne, but !eels a bit more 1tacked
on1
/ Ja)a probably does classes better than #ython 2*asp3
. 4eclarin* a class'
class name: statements
-
8/17/2019 08-classes-objects.ppt
3/16
3
Fields
name = value
/ E&le'
class Point: x = 0
y = 0# mainp1 = Point()p1.x = 2p1.y = -5
/ can be declared directly inside class 2as shon here3or in constructors 2more common3
/ #ython does not really ha)e encapsulation or pri)ate 0elds. relies on caller to 1be nice1 and not mess ith objects5 contents
point.py
12
3
class Point: x = 0
y = 0
-
8/17/2019 08-classes-objects.ppt
4/16
4
Using a Class
import class/ client pro*rams must import the classes they use
point_main.py
123456789
10
from Point import *
# mainp1 = Point()p1.x = 7p1.y = -3...
# Python objects are dynamic can add fie!ds any time"p1.name = "Tyler Dr!en"
-
8/17/2019 08-classes-objects.ppt
5/16
5
Object Methods
!e name(sel$ parameter$ ...$ parameter): statements
# sel must be the 0rst parameter to any object method. represents the 1implicit parameter1 2t$is in Ja)a3
/ must access the object5s 0elds throu*h the sel re!erence
class Point:
def trans!atese!f$ d%$ dy& se!f.x %= !x se!f.y %= !y ...
-
8/17/2019 08-classes-objects.ppt
6/16
6
!mplicit Parameter"se!f#
. Ja)a' t$is, implicitp&lic 'oi! translate(int !x int !y)
x %= !x* '' this(% ) d%+ y %= !y* '' this(y ) dy++
. #ython' sel, e&plicit!e translate(se!f !x !y): se!f(% %= !x se!f(y %= !y
/ E&ercise' 6rite !istance, set,location, and!istance,rom,oriin methods%
-
8/17/2019 08-classes-objects.ppt
7/167
$%ercise &ns'er
point.py
12345
6789
1011121314151617
rom mat$ import
class Point: x = 0 y = 0
!e set,location(sel x y): sel.x = x sel.y = y
!e !istance,rom,oriin(sel): retrn s/rt(sel.x sel.x % sel.y sel.y)
!e !istance(sel ot$er): !x = sel.x - ot$er.x !y = sel.y - ot$er.y retrn s/rt(!x !x % !y !y)
-
8/17/2019 08-classes-objects.ppt
8/168
Calling Methods
. 7 client can call the methods o! an object in to ays'/ 2the )alue o! sel can be an implicit or e&plicit
parameter3
83 object.method(parameters) or
93 Class.method(object parameters)
. E&le'p = Point(3 -)
p(trans!ate(1 5)Point(trans!ate( p 1 5)
-
8/17/2019 08-classes-objects.ppt
9/169
Constr(ctors
!e ,,init,, (sel, parameter$ ...$ parameter): statements
/ a constructor is a special method ith the name ,,init,,
/ E&le'class Point:
!e ,,init,,(sel x y):
sel.x = x
sel.y = y
...
. :o ould e make it possible to construct aPoint() ith no parameters to *et 2-, -3
-
8/17/2019 08-classes-objects.ppt
10/1610
to-trin. and ,,str,,
!e ,,str,, (sel): retrn string
/ e
-
8/17/2019 08-classes-objects.ppt
11/1611
Complete Point Class
point.py
123456
7891011121314
15161718192021
rom mat$ import
class Point: !e ,,init,,(sel x y): sel.x = x sel.y = y
!e !istance,rom,oriin(sel): retrn s/rt(sel.x sel.x % sel.y sel.y)
!e !istance(sel ot$er): !x = sel.x - ot$er.x !y = sel.y - ot$er.y retrn s/rt(!x !x % !y !y)
!e translate(sel !x !y): sel.x %= !x sel.y %= !y
!e ,,str,,(sel): retrn "(" % str(sel.x) % " " % str(sel.y) % ")"
-
8/17/2019 08-classes-objects.ppt
12/16
12
Operator O)erloading
. operator o)erloading' =ou can de0ne !unctions sothat #ython5s built+in operators can be used ithyour class%
. See also' http'((docs%python%or*(re!(customi>ation%htmlOperator Class Method
- ,,ne,,(sel ot$er)
% ,,pos,,(sel ot$er)
,,ml,,(sel ot$er)
,,tre!i',,(sel ot$er)Unary Operators
- ,,ne,,(sel)
% ,,pos,,(sel)
Operator Class Method
== ,,e/,,(sel ot$er)
= ,,ne,,(sel ot$er)
4 ,,lt,,(sel ot$er)
,,t,,(sel ot$er)
4= ,,le,,(sel ot$er)
= ,,e,,(sel ot$er)
http://docs.python.org/ref/customization.htmlhttp://docs.python.org/ref/customization.htmlhttp://docs.python.org/ref/customization.html
-
8/17/2019 08-classes-objects.ppt
13/16
13
$%ercise
. E&ercise' *rite a /raction class to represent rationalnumbers like 8(9 and +(?%
. @ractions should alays be stored in reduced !orm; !ore&le, store A(89 as 8( and B(+ as +9(%
/ :int' 7 DC4 2*reatest common di)isor3 !unction mayhelp%
. 4e0ne a!! and mltiply methods that accept another
6raction as a parameter and modi!y the e&istin*6raction by addin*(multiplyin* it by that parameter%
. 4e0ne %, , ==, and 4 operators%
-
8/17/2019 08-classes-objects.ppt
14/16
14
+enerating $%ceptions
raise $%ceptionype("message")
/ use!ul hen the client uses your object improperly
/ types' rit$metic8rror, ssertion8rror, 9n!ex8rror,ame8rror, yntax8rror, Type8rror, ;ale8rror
/ E&le'
class
-
8/17/2019 08-classes-objects.ppt
15/16
15
!nheritance
class name(s(perclass): statements
/ E&le'class Point3D(Point): # Point3 e%tends Point
> = 0 ...
. #ython also supports multiple inheritance
class name(s(perclass$ ...$ s(perclass): statements
(if > 1 superclass has the same eld/method, conicts are resolved in left-to-right order)
C lli - l
-
8/17/2019 08-classes-objects.ppt
16/16
16
Calling -(perclassMethods
. methods' class.method(objectparameters)
. constructors' class.,,init,,(parameters)
class Point3D(Point):
> = 0
!e ,,init,,(sel x y >):
Point(,,init,,se!f$ %$ y sel.> = >
!e translate(sel !x !y !>):
Point(trans!atese!f$ d%$ dy sel.> %= !>