重新梳理Python基礎(10)

來源:互聯網
上載者:User

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

  1. Write or draw about the problem.
  2. Extract key concepts from #1 and research them.
  3. Create a class hierarchy and object map for the concepts.
  4. Code the classes and a test to run them.
  5. Repeat and refine.
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.