Classes and Objects - MR. HASTINGS

Classes and Objects Objects Objects Help are things. Think nouns. us organize code Defined

by classes Classified Also into groups referred to as instances

Classes Parent classes and children of those classes. Classes can be both parents and children (like your parents are both parents to

you and children to your grandparents) You stay classy, Mardela! Class & Object Relationship: Things: Inanimate : Wood (obj)

Animate : Animals Mammals Cows (obj) Creating classes in Python class Animals(Animate): class Mammals(Animals):

class Cows(Mammals): ***We tell Python that the class is a child of another class by naming the parent in parenthesis (like a last name!) Adding objects to classes We want to add Bessy to our Cow

class Bessy is a cow and she is an object or instance of this class. Lets introduce her to the Cows class. bessy = Cows( ) we can also add another cow to the Cows class Hilda hilda = Cows( ) Great, we have two cows, but what can they do?

Well first, we need to add some characteristics to the classes. We do this with functions. Lets think on the characteristics each class has Animals Parent class

Mammals Cows Class of Animals Move Funct. of mammal

Gives milk to young Funct. of mammal Class of Mammals Chews grass Funct.

of cows Moo Funct. of Cows Lets add some character to our class class Mammals:

def move(self): print(I am moving, I am moving) def give_milk_to_young(self): print(The mother feeds her young) Lets add some character to our class class Cows: def chew_grass(self): print(I am chewing, I am chewing)

def moo(self): print(The sound you hear is a Moooooo!) Mooooving an object Invoking the (self) parameter Inheritance We type in self as the first parameter when defining our functions This is a characteristic of the Python program which allows us one function in the class to call another function within that same class, or

another parent class. Thats all Inheritance continued: Functions with a class, can use functions (methods) within a parent class as well. The abilities are passed down or passed through Can I borrow the car? The pass statement: We can do a few things that will help us:

We can use the pass statement. We can use this when we want to set up our program but may not know what to do yet. Begin our program by typing in the following: class Animals(Animate): These functions can be thought of as actions, which that class can do Lets

make Bessy move, which is a function of her parent class, animals. (Remember, Bessy is an object in the class Cows(Mammals). Bessy.move() Even though move is a function of the parent class Animals, bessy, an object of the Cows class, which is a class of Mammals, which is a class of Animals, can use this function this is an inherited function. How everything is set up:

class Mammals: def move(self ): print(I am moving, I am moving) def give_milk_to_young(self ): print(The mother feeds her young) class Cows: def chew_grass(self ): print(I chewing, I am chewing) def moo(self): print(The sound you hear is a Moooooo!)

bessy = Cows( ) bessy.move() hilda = Cows( ) Hilda.moo( ) If you typed this and got an error because bessy cant move. It is because we have not linked these two classes

with a parent class. Lets practice by teaching our cows dance! Create a program where our cow Bessy dances (printing what happens) *jumps in place Cow class Objects bessy & hilda (whatever names youd like)

Recently Viewed Presentations

  • Arthropods and Echinoderms  The zebra swallowtail butterfly is

    Arthropods and Echinoderms The zebra swallowtail butterfly is

    Arthropods and Echinoderms The zebra swallowtail butterfly is one of more than 750,000 species of arthropods--the largest phylum of animals
  • PowerPoint-presentatie

    PowerPoint-presentatie

    English : no Topic (Mackenzie & Keizer 1991) That Inever said: Contrast on . that. (p. 93) The Interpersonal Level. Contrast vs Overlap. Contrast assigned to bring out differences between two (or more) Communicated Contents or a Comm'd Content and...
  • Cal State LA College of Arts and Letters

    Cal State LA College of Arts and Letters

    Withdrawal Grades. W (Withdrawal) indicates that a student was permitted to drop a course after the "no-record drop" deadline with the approval of the instructor and the department/division/school chair or director. The W carries no connotation of quality of student...
  • Robert Frost - Commack Schools

    Robert Frost - Commack Schools

    Robert Frost A Detailed Study Brief Biography Robert Lee Frost was born March 26, 1874 in San Francisco, CA Father Will Frost dies. Family returns to Lawrence, MA for burial. Robert enters Lawrence H.S. 1888 Publishes first poem "La Noche...
  • 1 "A Game with Consequences" - Training Games, Inc.

    1 "A Game with Consequences" - Training Games, Inc.

    We'll be cuddling soon, By the silvery moon. All - By the light (not the dark but the light) of the silvery moon (not the sun but the moon) I wanna spoon (not croon but spoon) To my honey I'll...
  • Topic Sentences - Weebly

    Topic Sentences - Weebly

    Topic Sentences connect back to thesis. Thesis Formula: Topic (reality T.V.) + Opinion (What I think about it) + Why or How (because…). Thesis Example: Reality TV highly influences and dictates the decisions and actions of teenagers because they are...
  • 4.1: FORMS OF ENERGY - nmsby.yolasite.com

    4.1: FORMS OF ENERGY - nmsby.yolasite.com

    4.1 Forms of energy: The First Law of Thermodynamics. 4.2 Various form of energy. 4.3 Definitions of system (closed or open system) 4.4 Balance on various type of unit process (single/multiple unit operation, reactive and non-reactive) 4.5 Mechanical Energy Balances
  • Information Security Office of Budget and Finance Education

    Information Security Office of Budget and Finance Education

    A Raspberry Pi, an external hard drive, a wireless router, a GSM device, a battery backup. What does it do, what is it for? An IoT mystery…. Questions and Discussion. Education - Partnership - Solutions . Information Security. Office of...