The methods define the objects behavior each object

This preview shows 10 out of 15 pages.

The methods define the object’s behavior . Each object is an instance of a class. A new object of a class named ClassName is created by calling the class’s constructor also named ClassName . Example : The line of code pt = Point(2,5) binds the variable pt to a new instance of the class Point . W. M. Farmer and S. Smith ENG 1D04 Fall 2010: 09 Classes 10/17
Image of page 10

Subscribe to view the full document.

Class Definitions A class is a template for a type of objects. The instances of the class are the members of the type. A simple class definition has the form: class ClassName : MethodDefinition 1 . . . MethodDefinition n MethodDefinition 1 is usually the definition of the init method. W. M. Farmer and S. Smith ENG 1D04 Fall 2010: 09 Classes 11/17
Image of page 11
Methods A method in a class definition is a function definition of the form: def MethodName (self , a 2 , . . . , a n ): Statements The first parameter, which is traditionally named self , is bound to the object to which the method belongs. Example : The method def getX(self): return self.x is a component of the class Point which is applied to an instance pt of the class Point as pt.getX() W. M. Farmer and S. Smith ENG 1D04 Fall 2010: 09 Classes 12/17
Image of page 12

Subscribe to view the full document.

The init Method The init method of a class is a special method that is usually used to: 1. Define the fields ( instance variables ) of an object. 2. Initialize the fields of an object. This method is called when the constructor for a class is called. Example : Here is the init method for the Point class: def init (self, x, y): GraphicsObject. init (self, [...]) self.setFill = self.setOutline self.x = x self.y = y W. M. Farmer and S. Smith ENG 1D04 Fall 2010: 09 Classes 13/17
Image of page 13
Benefits of the Object-Oriented Paradigm 1. Objects are modular units of data and operators.
Image of page 14

Subscribe to view the full document.

Image of page 15
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern