1. 今天学了一些概念。(书的第41~43章)
用这个方法可以分清楚class、object、instance
## Animal is-a object (yes, sort of confusing) look at the extra credit class Animal(object): pass ## Dog is-a animal, it has-a name class Dog(Animal): def __init__(self, name): ## ?? self.name = name ## Cat is-a animal, it has-a name class Cat(Animal): def __init__(self, name): ## ?? self.name = name ## Person is-a object, it has-a name and pet class Person(object): def __init__(self, name): ## ?? self.name = name ## Person has-a pet of some kind self.pet = None ## Employee is-a Person, it has-a name and salary class Employee(Person): def __init__(self, name, salary): ## ?? hmm what is this strange magic? super(Employee, self).__init__(name) ## ?? self.salary = salary ## Fish is-a object class Fish(object): pass ## Salmon is-a Fish class Salmon(Fish): pass ## Halibut is-a Fish class Halibut(Fish): pass ## rover is-a Dog rover = Dog("Rover") ## satan is-a Cat satan = Cat("Satan") ## mary is-a Person mary = Person("Mary") ## mary has-a pet, satan mary.pet = satan ## frank is-a employee, it has-a salary 120000 frank = Employee("Frank", 120000) ## frank has-a pet, rover frank.pet = rover ## flipper is-a fish flipper = Fish() ## crouse is-a salmon crouse = Salmon() ## harry is-a halibut harry = Halibut()
用这种方式可以比较清楚的明白对象、类和实例的概念。
2. OOP
- Write or draw about the problem.
- Extract key concepts from #1 and research them.
- Create a class hierarchy and object map for the concepts.
- Code the classes and a test to run them.
- Repeat and refine.