Difference between revisions of "Introduction to Programming Week 10"
(Created page with '==Summary== This week we worked primarily on one large project. See the transcript for details, this page will only focus on the concepts introduced. ====Part 2: Finding The Cl…') |
(→Summary) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
==Summary== | ==Summary== | ||
− | This week we worked primarily on one large project. See the transcript for details, this page will only focus on the concepts introduced. | + | This week we worked primarily on one large project: writing a program to play [[Pass the Pigs]]. See the transcript for details, this page will only focus on the concepts introduced. |
+ | |||
+ | ====Part 1: Pass The Pigs==== | ||
+ | See [[Pass the Pigs]]. | ||
====Part 2: Finding The Classes==== | ====Part 2: Finding The Classes==== | ||
− | Often, when writing a large program, the most difficult and important part is deciding how to design the program - which classes you need, and how they'll interact. | + | Often, when writing a large program, the most difficult and important part is deciding how to design the program - which [[class (Computer Science)|classes]] you need, and how they'll interact. |
One way to come up with ideas for the classes you'll need is to make a list of all the important nouns related to the problem or program you want to write. Then you can go through and decide if each noun should be an object of an existing class, an extension of an existing class (see below), in its own new class, or an attribute of some other object. | One way to come up with ideas for the classes you'll need is to make a list of all the important nouns related to the problem or program you want to write. Then you can go through and decide if each noun should be an object of an existing class, an extension of an existing class (see below), in its own new class, or an attribute of some other object. | ||
Line 14: | Line 17: | ||
In Python, we can create subclasses by putting the superclass we want to extend in parentheses at the end of the class definition: | In Python, we can create subclasses by putting the superclass we want to extend in parentheses at the end of the class definition: | ||
class Subclass(Superclass): | class Subclass(Superclass): | ||
+ | |||
+ | Python actually allows you to list multiple superclasses in a comma-separated list, but this is rarely necessary. | ||
Note that an [[isinstance function|instance]] of a subclass is also considered an instance of the superclass, but an instance of the superclass isn't automatically an instance of the subclass. For example, suppose we have a Mammal superclass and a Dog subclass. Then Python would consider every Dog to also be a Mammal, but not every Mammal to necessarily be a Dog. | Note that an [[isinstance function|instance]] of a subclass is also considered an instance of the superclass, but an instance of the superclass isn't automatically an instance of the subclass. For example, suppose we have a Mammal superclass and a Dog subclass. Then Python would consider every Dog to also be a Mammal, but not every Mammal to necessarily be a Dog. | ||
Line 22: | Line 27: | ||
If all the new method does is extend the functionality of the original one, it's common to call the original method from within the new one, so the duplicated parts don't have to be rewritten. This is especially common in the constructor, which is almost always overridden: | If all the new method does is extend the functionality of the original one, it's common to call the original method from within the new one, so the duplicated parts don't have to be rewritten. This is especially common in the constructor, which is almost always overridden: | ||
def __init__(self, otherParameters): | def __init__(self, otherParameters): | ||
− | + | """Constructor for Subclass""" | |
...subclass specific things... | ...subclass specific things... | ||
Superclass.__init__(self, otherParameters) | Superclass.__init__(self, otherParameters) |
Latest revision as of 18:18, 3 September 2011
Contents
[hide]Summary
This week we worked primarily on one large project: writing a program to play Pass the Pigs. See the transcript for details, this page will only focus on the concepts introduced.
Part 1: Pass The Pigs
See Pass the Pigs.
Part 2: Finding The Classes
Often, when writing a large program, the most difficult and important part is deciding how to design the program - which classes you need, and how they'll interact.
One way to come up with ideas for the classes you'll need is to make a list of all the important nouns related to the problem or program you want to write. Then you can go through and decide if each noun should be an object of an existing class, an extension of an existing class (see below), in its own new class, or an attribute of some other object.
Part 3: Inheritance
In a game of Pass The Pigs the pigs are very similar, but not identical to, dice. We've already written a Die class, so it would seem like a good idea to use it as a starting point when making a new Pig class.
This type of scenario is actually very common, many classes are simply extensions of previous classes. In object-oriented programming new classes extending existing ones is called inheritance. The "parent" class is called the superclass, and the "child" class is called a subclass.
In Python, we can create subclasses by putting the superclass we want to extend in parentheses at the end of the class definition:
class Subclass(Superclass):
Python actually allows you to list multiple superclasses in a comma-separated list, but this is rarely necessary.
Note that an instance of a subclass is also considered an instance of the superclass, but an instance of the superclass isn't automatically an instance of the subclass. For example, suppose we have a Mammal superclass and a Dog subclass. Then Python would consider every Dog to also be a Mammal, but not every Mammal to necessarily be a Dog.
Part 4: Building The Pig
An instance of a subclass has access to all the methods of the superclass, so you don't have to rewrite them all. However, when writing a subclass you'll often want to redefine some methods to support the the new, subclass specific behavior. If you write a new method in the subclass with the same name as a method in the superclass the new method will override the original one.
If all the new method does is extend the functionality of the original one, it's common to call the original method from within the new one, so the duplicated parts don't have to be rewritten. This is especially common in the constructor, which is almost always overridden:
def __init__(self, otherParameters): """Constructor for Subclass""" ...subclass specific things... Superclass.__init__(self, otherParameters)
Private variables (variables that start with two underscores) in a superclass are also hidden from subclasses.