1. 今天學了一些概念。(書的第41~43章)
用這個方法可以分清楚class、object、instance
## Animal is-a object (yes, sort of confusing) look at the extra creditclass Animal(object): pass## Dog is-a animal, it has-a nameclass Dog(Animal): def __init__(self, name): ## ?? self.name = name## Cat is-a animal, it has-a nameclass Cat(Animal): def __init__(self, name): ## ?? self.name = name## Person is-a object, it has-a name and petclass 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 salaryclass Employee(Person): def __init__(self, name, salary): ## ?? hmm what is this strange magic? super(Employee, self).__init__(name) ## ?? self.salary = salary## Fish is-a objectclass Fish(object): pass## Salmon is-a Fishclass Salmon(Fish): pass## Halibut is-a Fishclass Halibut(Fish): pass## rover is-a Dogrover = Dog("Rover")## satan is-a Catsatan = Cat("Satan")## mary is-a Personmary = Person("Mary")## mary has-a pet, satanmary.pet = satan## frank is-a employee, it has-a salary 120000frank = Employee("Frank", 120000)## frank has-a pet, roverfrank.pet = rover## flipper is-a fishflipper = Fish()## crouse is-a salmoncrouse = Salmon()## harry is-a halibutharry = 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.