Making our own Classes and objects As in real life, were now creating classes of objects. a class...

21

description

Like creating our own Types  Think of lists:  Lists are a type.  They have methods (functions) associated with them  E.g.,  alist.append(x)  alist.index(x)  alist.pop()  Append(), index(), and pop() are examples of methods (functions) associated with lists.  You can’t use the pop() method (function) with something of type int, right?

Transcript of Making our own Classes and objects As in real life, were now creating classes of objects. a class...

Page 1: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.
Page 2: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

Making our own Classes and objects

As in real life, we’re now creating classes of objects. a class that defines basic characteristics and functions that

apply to all objects of that class Think of a class as a set of functions and variables that

belong together to define a particular thing. E.g., Savings Account classes would have:

A balance #variable, or attribute An owner’s name #variable, or attribute The ability to add money to the account #function, or method The ability to withdraw money from the account #function, or

method An individual’s Savings Account might have:

$542.79 as a balance Roderick Feckelbocker as the owner’s name

Page 3: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

Like creating our own Types Think of lists:

Lists are a type. They have methods (functions) associated with them E.g.,

alist.append(x) alist.index(x) alist.pop()

Append(), index(), and pop() are examples of methods (functions) associated with lists.

You can’t use the pop() method (function) with something of type int, right?

Page 4: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

If we were to write this:class OurList(object):

def __init__(self, alist ):

self.thisList = alist # a property!

def ourPop(self): # Notice this is still part of the OurList class!

x = self.thisList[len(self.thisList) – 1]self.thisList = self.thisList[0:len(self.thisList)-1]

return(x)

Page 5: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

To create something of the class OurList:class OurList(object):

def __init__(self, alist ):

self.thisList = alist # a property!

def ourPop(self): x = self.thisList[len(self.thisList) – 1]self.thisList = self.thisList[0:len(self.thisList)-1]

return(x)

ls = OurList([3,2,4,1,5]) # Note that now we have to tell the computer what Class type we’re creatingy = ls.ourPop()

Page 6: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

Let’s make a class for Rectangles:What methods do we want our rectangle class to have?What properties?

Class Rectangle(object):def __init__(self,h,w):

self.height = hself.width = w

def area(self):return(self.height * self.width)

Page 7: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

Let’s make a class for Rectangles:What methods do we want our rectangle class to have?What properties?

Class Rectangle(object):def __init__(self,h,w):self.height = hself.width = w

def area(self):return(self.height * self.width)

rect1 = Rectangle(10, 10) rect2 = Rectangle(20, 5)

print(rect1.area())

Page 8: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

class Rectangle(object): def __init__(self, width, height):

#[CONSTRUCTOR]self.width = widthself.height = height

def area(self): return self.width * self.height def boxvol(self,x):

return self.width * self.height * x

rectangle1 = Rectangle(10, 10) #[EXAMPLES]

rectangle2 = Rectangle(20, 5)

print(rectangle1.area())print(rectangle2.boxvol(2))print(Rectangle(6,8).area())

Page 9: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

Classes Does the problem involve data that is related to a single

concept? (e.g., a student, an airline ticket, a car) Write down the kinds of data in the problem and organize

them into classes (some problems may need multiple classes).

Write: class definition a list of properties belonging to objects of that class a constructor (__init__) An example function some example class instances.

Page 10: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

Employee Class

class Employee:

def __init__(self, n, s): self.name = n self.salary = s

def displayEmployee(self): print("Name : "+self.name+", Salary: "+ self.salary)

x = Employee(“bob”,32000)x.displayEmployee()

Page 11: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

Employee Classclass Employee: def __init__(self, n, s): self.name = n self.salary = s

def displayEmployee(self): print("Name : "+self.name+", Salary: "+ self.salary)

def getRaise(self,x): self.salary = self.salary + x

x = Employee(“bob”,32000)x.displayEmployee()x.getRaise(2000)x.displayEmployee()

Page 12: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

Student Class?

class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project def getgrade(self): x= self.lab * 0.25 + self.exam * 0.5 + self.project * 0.25 return(x)

Page 13: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

Write a class for a circle?

Functions? Properties?

Page 14: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

class Circle(object): """ A circle is a shape with radius, a circumference, and an area

radius - number circumference – number (2*pi*radius) area – number (pi*radius**2)

""" def __init__(self, x,y,z):

self.radius = xself.area=y

self.circumference=zx= Circle(3,7,8)

Page 15: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

class Circle(object): def __init__(self,x): self.radius = x self.circumference = self.getcirc()

self.area = 7

def getcirc(self): return(self.radius * 2 * pi)

def area(self): return(self.radius **2 * pi)

def changerad(self,x): self.radius = x self.circumference = self.getcirc()

circ1 = Circle(3)Print(circ1.area)circ1.changerad(4)Print(circ1.area())

Can we include in the class a function that checks if this circle is bigger than another circle?

What type should it return?

Page 16: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

from math import *class Circle(object): def __init__(self,x): self.radius = x self.circumference = self.getcirc() self.area = self.getarea()

def getcirc(self): return(self.radius * 2 * pi)

def getarea(self): return(self.radius **2 * pi)

def isbigger(self, circ2): return(self.area > circ2.area)

firstcirc = Circle(3)secondcirc = Circle(4)thirdcirc = Circle(2)print(firstcirc.area)print(firstcirc.isbigger(secondcirc))print(firstcirc.isbigger(thirdcirc))

Page 17: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

Student – what if we wanted a grade property?

class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project

self.grade = self.getgrade() def getgrade(self): x= self.lab * 0.25 + self.exam * 0.5 + self.project * 0.25 return(x)

Now make a student. Print his/her grade.

Page 18: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

Printing a student object? class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project self.grade = self.getgrade()

def getgrade(self): x= self.lab * 0.25 + self.exam * 0.5 + self.project * 0.25 return(x)

def __str__(self): return(self.lastname + ","+self.firstname+": \n\tLab: "+\ str(self.lab) + "\n\tExam: " + str(self.exam) +\ "\n\tProject: "+str(self.project)+"\n\tGrade: "+str(self.grade))x = Student('Bill','Williams',88,76,94)print(x)y = Student('Anne','Stewart',97,67,73)print(y)

Page 19: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

What about comparing students?

class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project self.grade = self.getgrade()

def getgrade(self): x= self.lab * 0.25 + self.exam * 0.5 + self.project * 0.25 return(x)

def __str__(self): return(self.lastname + ","+self.firstname+": \n\tLab: "+\ str(self.lab) + "\n\tExam: " + str(self.exam) +\ "\n\tProject: "+str(self.project)+"\n\tGrade: "+str(self.grade))

def __lt__(self,x): if (self.lastname != x.lastname): return self.lastname < x.lastname elif (self.firstname != x.firstname): return(self.firstname < x.firstname) else: return(self.grade < x.grade)

Page 20: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project self.grade = self.getgrade()

def getgrade(self): x= self.lab * 0.25 + self.exam * 0.5 + self.project * 0.25 return(x)

def __str__(self): return(self.lastname + ","+self.firstname+": \n\tLab: "+\ str(self.lab) + "\n\tExam: " + str(self.exam) +\ "\n\tProject: "+str(self.project)+"\n\tGrade: "+str(self.grade))

def __lt__(self,x): if (self.lastname != x.lastname): return self.lastname < x.lastname elif (self.firstname != x.firstname): return(self.firstname < x.firstname) else: return(self.grade < x.grade)

x = Student("Robert","Jones",64,71,86)print(x)y = Student("Tom","Miller",72,44,87)print(y)z = Student("Anne","Jones",87,78,92)

x = Student("Robert","Jones",64,71,86)print(x)y = Student("Tom","Miller",72,44,87)print(y)z = Student("Anne","Jones",87,78,92)if (x < y): print('hi')else: print('low')if (x < z): print('hi2')else: print('low2')

Page 21: Making our own Classes and objects  As in real life, were now creating classes of objects.  a class that defines basic characteristics and functions.

Others:

__eq__(self, other)Defines behavior for the equality operator, ==.

__ne__(self, other)Defines behavior for the inequality operator, !=.

__lt__(self, other)Defines behavior for the less-than operator, <.

__gt__(self, other)Defines behavior for the greater-than operator, >.

__le__(self, other)Defines behavior for the less-than-or-equal-to operator, <=.

__ge__(self, other)Defines behavior for the greater-than-or-equal-to operator, >=.