In the original Tamagotchi game in the last chapter, you saw code that created instances of the Pet class. But the chasing_rats method is special: it’s only usable on Cat instances, because Cat is a subclass of Pet which has that additional method. You can call the _str_ method on an instance of Cat to print an instance of Cat, the same way you could call it on an instance of Pet, and the same is true for the hi method – it’s the same for instances of Cat and Pet. We can still use all the Pet methods in the Cat class, this way. In this case, the only difference is that the class variable sounds starts out with the string "Meow" instead of the string "mrrp", and there is a new method chasing_rats. In the definition of the class Cat, we only need to define the things that are different from the ones in the Pet class. We do that by putting the word Pet in parentheses, class Cat(Pet). In that extra code, we make sure the Cat class inherits from the Pet class. ![]() Return "What are you doing, Pinky? Taking over the world?!"Īll we need is the few extra lines at the bottom of the ActiveCode window! The elegance of inheritance allows us to specify just the differences in the new, inherited class. # Here's the new definition of class Cat, a subclass of Pet.Ĭlass Cat(Pet): # the class name that the new class inherits from goes in the parentheses, like so. Self.boredom = max(0, self.boredom - self.boredom_decrement) Self.hunger = max(0, self.hunger - self.hunger_decrement) # state += "Hunger %d Boredom %d Words %s" % (self.hunger, self.boredom, self.sounds) Self.sounds = self.sounds # copy the class attribute, so that when we make changes to it, we won't affect the other Pets in the class Self.boredom = randrange(self.boredom_threshold) Self.hunger = randrange(self.hunger_threshold) We want the Cat type to be exactly the same as Pet, except we want the sound cats to start out knowing “meow” instead of “mrrp”, and we want the Cat class to have its own special method called chasing_rats, which only Cat s have.įor reference, here’s the original Tamagotchi code Assume we have the Pet class that we defined earlier. Say we want to define a class Cat that inherits from Pet. In the example we’re discussing, Pet would be the superclass of Dog or Cat). In the definition of the inherited class, you only need to specify the methods and instance variables that are different from the parent class (the parent class, or the superclass, is what we may call the class that is inherited from. So if you wanted to define a Dog class as a special kind of Pet, you would say that the Dog type inherits from the Pet type. ![]() Inheritance provides us with an easy and elegant way to represent these differences.īasically, it works by defining a new class, and using a special syntax to show what the new sub-class inherits from a super-class. Going a step further, a Collie dog is different from a Labrador dog, for example. In the abstract, this is pretty intuitive: all pets have certain things, but dogs are different from cats, which are different from birds. ![]() We said that inheritance provides us a more elegant way of, for example, creating Dog and Cat types, rather than making a very complex Pet class. ![]() Inheriting Variables and Methods ¶ 22.2.1.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |