Download presentation
Presentation is loading. Please wait.
Published byŌἈβαδδών Βιτάλη Modified over 5 years ago
1
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 Objects (again)
2
Variables, Lists, and Objects
Objects — review Reading assignment:– Chapter 4 Needed for HW #3 And everything else we do in this course! Object: computational abstraction that includes Data Methods (a. k. a. Functions) P. 81:– Objects “know” stuff Objects “do” stuff Objects can refer to other objects Objects can interact with other objects An object may be assigned to a variable Or, equivalently, to an element of a list CS-1004, A-Term 2014 Variables, Lists, and Objects
3
Objects — review (continued)
Objects are organized into Classes All objects in a class have the same kind of information All objects in a class have the same set of methods (a.k.a. functions) Classes are discussed in Chapter 10 Including how to define new classes How to construct objects of a class How to define methods of a class How to define data elements of a class object Probably will not get to this chapter in this course CS-1004, A-Term 2014 Variables, Lists, and Objects
4
Object Notation — review
If X is an object of class C, … … and if class C has a method/function named f, … … then the notation X.f() means “Apply the function f (and its arguments) to the object X” I.e., f may cause the object X to DO something Or f may cause the object X to change what it knows CS-1004, A-Term 2014 Variables, Lists, and Objects
5
Creating Objects — review
If C is a class, then the constructor function for that class is also named C V = C(arg1, arg2, …) creates a new object of class C and assigns it to variable V All of the methods associated with class C are now available for object V Examples CS-1004, A-Term 2014 Variables, Lists, and Objects
6
Assigning Objects — review
V = SomeClass(<arguments> ) W = V causes W and V to now refer to the very same object Not to copies of each other, but exactly the same object CS-1004, A-Term 2014 Variables, Lists, and Objects
7
Questions? CS-1004, A-Term 2014 Objects (again)
8
How do we create new classes?
See Chapter 10 Keyword class followed by class name … followed by method functions … all indented under class … the first parameter representing object CS-1004, A-Term 2014 Objects (again)
9
Textbook example Multi-sided die class MSDie:
def __init__(self, sides): self.sides = sides self.value = 1 def roll(self): self.value = randrange(1, self.sides + 1) def getValue(self): return self.value def setValue(self, value): self.value = value CS-1004, A-Term 2014 Objects (again)
10
Textbook example (continued)
Multi-sided die class MSDie: def __init__(self, sides): self.sides = sides self.value = 1 def roll(self): self.value = randrange(1, self.sides + 1) def getValue(self): return self.value def setValue(self, value): self.value = value Note colon after class name Note indentation of method definitions Note 2nd indentation of method bodies CS-1004, A-Term 2014 Objects (again)
11
Textbook example (continued)
Multi-sided die class MSDie: def __init__(self, sides): self.sides = sides self.value = 1 Name __init__() is function called to create object of class I.e., when application says MSDie(6) … … Python sets aside memory for one MSDie object … and invokes __init__ with self referring to that memory Arguments of className assigned to additional parameters of __init__ Special function name CS-1004, A-Term 2014 Objects (again)
12
Textbook example (continued)
Multi-sided die class MSDie: def __init__(self, sides): self.sides = sides self.value = 1 'Self' could be any name Refers to the object Remember: Parameters act variables assigned at time function/method is called All class methods must have first parameter referring to object ‘self’ is just a parameter name CS-1004, A-Term 2014 Objects (again)
13
Textbook example (continued)
Multi-sided die class MSDie: def __init__(self, sides): self.sides = sides self.value = 1 def roll(self): self.value = randrange(1, self.sides + 1) def getValue(self): return self.value def setValue(self, value): self.value = value All class methods must have first parameter referring to object Other parameters CS-1004, A-Term 2014 Objects (again)
14
Textbook example (continued)
Multi-sided die class MSDie: def __init__(self, sides): self.sides = sides self.value = 1 They make it possible of object “to know stuff” Called “fields” in Java Called “members” in C/C++ Part of every object of this class “Dot” notation to access with methods These are ‘instance variables’ CS-1004, A-Term 2014 Objects (again)
15
Textbook example (continued)
Multi-sided die class MSDie: def __init__(self, sides): self.sides = sides self.value = 1 def roll(self): self.value = randrange(1, self.sides + 1) def getValue(self): return self.value def setValue(self, value): self.value = value __init__ initializes instance variables Other methods use and/or manipulate them CS-1004, A-Term 2014 Objects (again)
16
Questions? CS-1004, A-Term 2014 Objects (again)
17
Example Game of Life CS-1004, A-Term 2014 Objects (again)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.