INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

16
INLS 560 – OBJECTS Instructor: Jason Carter

Transcript of INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

Page 1: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

INLS 560 – OBJECTS

Instructor: Jason Carter

Page 2: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

OBJECTSNatural Objects

Manufactured Objects

Page 3: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

PROGRAM OBJECTS ~ MANUFACTURED OBJECTS

manufacturedby perform

Operations

accelerate

brake

ClassProgram Object

Program Object

instance of

Methods

accelerate

brake

execute

invoke call

Page 4: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

ANATOMY OF A CLASS

Class Header

Class Constructo

r

Class Method

The name of the class file should be the name of the class

Page 5: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

CLASS HEADER

The class header is the first line of a class. It declares the name of the class. In our previous example, the name of the class

was MyPoint

Page 6: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

CLASS CONSTRUCTOR

The first method __init__() is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class.

Page 7: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

SELF – IMPLICIT PARAMETER

self must be the first parameter to any object method

It refers to the object

Page 8: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

CLASS METHODS

self must be the first parameter to any object method

Use self to access the object's fields

Other is a point object It has x and y values

Page 9: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

USING A CLASS

import class client programs must import the classes they use

Page 10: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

USING A CLASS (CONT’D)

Page 11: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

USING A CLASS- OUTPUT

Page 12: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

USING CLASS METHODS

Page 13: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

USING CLASS METHODS OUTPUT

Page 14: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

PRACTICE

Write a Fraction class to represent rational numbers like 1/2 and -3/8.

The function class should be able to add fractions Create a method named add that takes a fraction as

a parameter, adds the two fractions, and returns the sum.

Create a method named sum that takes a fraction as a parameter, subtracts the two fractions, and returns the difference.

Create a method named multiply that takes a fraction as a parameter, multiplies the two fractions, and returns the product.

Page 15: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

CAN WE USE OBJECTS WITH DATABASES? How?

Database that held company reviews Table: review Columns:

user_id review_id stars date text type business_id votes_funny votes_useful votes_cool

Page 16: INLS 560 – O BJECTS Instructor: Jason Carter. O BJECTS Natural Objects Manufactured Objects.

OBJECTS REPRESENT DATABASE TABLE