40 likes | 171 Views
OOP and Graphics. Object Oriented Programming. The ‘classic’ point of view of a programmer was that the program instructions were the active part, the data just sat there and was manipulated. A new paradigm became popular in the 80’s: OOP
E N D
Object Oriented Programming • The ‘classic’ point of view of a programmer was that the program instructions were the active part, the data just sat there and was manipulated. • A new paradigm became popular in the 80’s: OOP • The idea is that programs should be considered as interactions of objects. An object was a structure that “knew things” (i.e. had data values) and “did things” (i.e. had operations to do on the data) (There’s quite a bit more to the paradigm than that, but that will do for now.)
Objects in Graphics • The graphics library is set up as a collection of object templates • Point, Circle, Rectangle, Line and so on • These templates are called “classes”. They are the “pattern” for what a particular object should be. • Example: a Point class would have data about the position of the point (x, y) and functions that could do things with that information • You call a function called a “constructor” to make a new object from the class. Constructor methods are almost always the same name as the name of the class. (Objects are also called “instances”.)
Objects in Graphics • Once you have an object constructed, it has a name. You can give the object commands by calling the methods (functions) that it knows about. • Example: p = Point(200, 300) # p is now an object from class Point p.draw(win) p.move(0, 5) • Note the syntax: “dot notation”. The statement or expression starts with the object name, followed by a dot, then the name of the method and its arguments, if any.