1 / 10

Making our own Classes and objects

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.

teagan
Download Presentation

Making our own Classes and objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Creating your own classes class Elephant(object): def __init__(self, name, age, weight, trunkradius, trunklength ): self.name = name self.age = age self.weight = weight self.trunkradius = trunkradius self.trunklength = trunklength • We now have a class of Elephants • Elephants starts with a capital letter • Our Elephants all have names and ages. • __init__: • __init__() is always the constructor in Python • The two __ on both sides of the word init indicate that it is a special name • Constructor is automatically invoked when the an object of this type is created.

  3. Creating an Elephant named Rita: class Elephant(object): def __init__(self, name, age, weight, trunkradius, trunklength ): self.name = name self.age = age self.weight = weight self.trunkradius = trunkradius self.trunklength = trunklength elephant1 = Elephant("Rita",32,6080,10,96) print("The Elephant’s name is " + elephant1.name + "and her age is " + str(elephant1.age)) elephant2 = Elephant("Harold",28,7940,14,100) print("The Elephant’s name is " + elephant2.name + "and his age is " + str(elephant2.age))

  4. Creating an Elephant named Rita: class Elephant(object): def __init__(self, name, age, weight, trunkradius, trunklength): self.name = name self.age = age self.weight = weight self.trunkradius = trunkradius self.trunklength = trunklength # main elephant1 = Elephant("Rita",32,6080,10,96) print("The Elephant’s name is "+elephant1.name+" and her age is "+str(elephant1.age)) print("Her trunk’s volume is" ) ???

  5. Creating an Elephant named Rita: from math import * class Elephant(object): """A virtual pet""" def __init__(self, name, age, weight, trunkradius, trunklength): self.name = name self.age = age self.weight = weight self.trunkradius = trunkradius self.trunklength = trunklength def trunkvol(self): # Notice this is still part of the Elephant class! return(self.trunkradius**2 * pi * self.trunklength) # main elephant1 = Elephant("Rita",32,6080,10,96) print("The Elephant's name is " + elephant1.name + "and her age is " + str(elephant1.age)) print("Her trunk's volume is " + str(elephant1.trunkvol() ))

  6. class Rectangle(object): """ A rectangle is a shape with width and height [DESCRIPTION] width - number [PROPERTIES] height - number """ def __init__(self, width, height): #[CONSTRUCTOR] self.width = width self.height = height def area(self): """ Computes the area of a Rectangle object aRectangle - Rectangle return - number """ return self.width * self.height rectangle1 = Rectangle(10, 10) #[EXAMPLES] rectangle2 = Rectangle(20, 5) print(rectangle1.area()) print(rectangle2.area()) print(Rectangle(6,8).area())

  7. class Rectangle(object): def __init__(self, width, height): #[CONSTRUCTOR] self.width = width self.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())

  8. class Circle(object): """ A circle is a shape with radius, a circumference, and an area [DESCRIPTION] radius - number [PROPERTIES] circumference – number (2*pi*radius) area – number (pi*radius**2) """ def __init__(self, radius): #[CONSTRUCTOR] self.radius = radius self.area=??? self.circumference=???

  9. Can we include in the class a function that checks if this circle is bigger than another circle? • What type should it return? from math import * class Circle(object): def __init__(self,radius): self.radius = radius self.circumference = self.getcirc() self.area = self.getarea() def getcirc(self): return(self.radius * 2 * pi) def getarea(self): return(self.radius **2 * pi) circ1 = Circle(3) print(circ1.area) print(circ1.circumference)

  10. from math import * class Circle(object): def __init__(self,radius): self.radius = radius 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))

More Related