What payment type? (purchase, rental) purchase
Enter the square feet: 800
Enter number of bedrooms: 3
Enter number of baths: 2
What laundry facilities does the property have? (coin, ensuite,
one) ensuite
Does the property have a balcony? (yes, no, solarium) yes
What is the selling price? $200,000
What are the estimated taxes? 1500
>>> agent.display_properties()
PROPERTY DETAILS
================
square footage: 900
bedrooms: 2
bathrooms: one and a half
HOUSE DETAILS
# of stories: 1
garage: detached
fenced yard: yes
RENTAL DETAILS
rent: 1200
estimated utilities: included
furnished: no
PROPERTY DETAILS
================
square footage: 800
bedrooms: 3
www.it-ebooks.info
Chapter 3
[
95
]
bathrooms: 2
APARTMENT DETAILS
laundry: ensuite
has balcony: yes
PURCHASE DETAILS
selling price: $200,000
estimated taxes: 1500
Exercises
Look around you at some of the physical objects in your workspace and see if you
can describe them in an inheritance hierarchy. Humans have been dividing the
world into taxonomies like this for centuries, so it shouldn't be difficult. Are there
any non-obvious inheritance relationships between classes of objects? If you were to
model these objects in a computer application, what properties and methods would
they share? Which ones would have to be polymorphically overridden?
What properties would be completely different between them?
Now, write some code. No, not for the physical hierarchy; that's boring. Physical
items have more properties than methods. Just think about a pet programming
project you've wanted to tackle in the past year, but never got around to. For
whatever problem you want to solve, try to think of some basic inheritance
relationships. Then implement them. Make sure that you also pay attention to the
sorts of relationships that you actually don't need to use inheritance for. Are there
any places where you might want to use multiple inheritance? Are you sure? Can
you see any place you would want to use a mixin? Try to knock together a quick
prototype. It doesn't have to be useful or even partially working. You've seen how
you can test code using
python -i
already; just write some code and test it in the
interactive interpreter. If it works, write some more. If it doesn't, fix it!
Now, take a look at the real estate example. This turned out to be quite an effective use
of multiple inheritance. I have to admit though, I had my doubts when I started the
design. Have a look at the original problem and see if you can come up with another
design to solve it that uses only single inheritance. How would you do it with abstract
base classes? What about a design that doesn't use inheritance at all? Which do you
think is the most elegant solution? Elegance is a primary goal in Python development,
but each programmer has a different opinion as to what is the most elegant solution.
Some people tend to think and understand problems most clearly using composition,
while others find multiple inheritance to be the most useful model.
www.it-ebooks.info
When Objects Are Alike
Do'stlaringiz bilan baham: |