编程基础python学习2完结
What is a Class?
Data structures like lists and strings are extremely useful, but sometimes they aren’t enough to represent something you’re trying to implement. For example, let’s say we needed to keep track of a bunch of pets. We could represent a pet using a list by specifying the first element of the list as the pet’s name and the second element of the list as the pet’s species. This is very arbitrary and nonintuitive, however – how do you know which element is supposed to be which?
Classes give us the ability to create more complicated data structures that contain arbitrary content. We can create a?Pet?class that keeps track of the name and species of the pet in usefully named attributes called?name?and?species, respectively.
What is an Instance?
Before we get into creating a class itself, we need to understand an important distinction. A class is something that just contains structure – it defines how something should be laid out or structured, but doesn’t actually fill in the content. For example, a?Pet?class may say that a pet needs to have a name and a species, but it will not actually say what the pet’s name or species is.
This is where?instances?come in. An instance is a specific copy of the class that does contain all of the content. For example, if I create a pet?polly, with name?"Polly"?and species?"Parrot", then?polly?is an instance of?Pet.
This can sometimes be a very difficult concept to master, so let’s look at it from another angle. Let’s say that the government has a particular tax form that it requires everybody to fill out. Everybody has to fill out the same?type?of form, but the content that people put into the form differs from person to person. A?classis like the form: it specifies what content should exist. Your copy of the form with your specific information if like an?instance?of the class: it specifies what the content actually is.
?
?
This is the basic instant for creating a class. The first word,?class, indicates that we are creating a class. The second word,?Pet, indicates the name of the class. The word in parentheses,?object, is the class thatPet?is inheriting from. We’ll get more into inheritance below, so for now all you need to know is that?object?is a special variable in Python that you should include in the parentheses when you are creating a new class.
class表示我們要新建一個類
pet是新建的類名
object是在python里一個特殊的變量,需要包含在新建類的圓括號里(知道怎么用就行了)
?http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/
?
?movie_storyline和poster_image等是初始化函數(shù)的自變量,而self.title,self.storyline等是實例變量。
self.title=movie_title就是在用函數(shù)的自變量初始化實例變量。
對比,在定義新類和定義初始化函數(shù)的括號后面少了冒號:
對比,在調(diào)用Media.movie時,實際上是在調(diào)用init函數(shù),而init函數(shù)里的self就是類的一個實例,在此處就是指向toy_story.問題出在此處沒有給init函數(shù)里的除self其他各個參數(shù)賦值,當你需要print toy_story時程序不知道此值是什么應該輸出什么所以此處會報錯。
?
?
一旦init函數(shù)被調(diào)用,并且media.movie里的四個自變量獲得了正確的值,所有與toy_story相關(guān)的變量都會被適當?shù)爻跏蓟R虼水斘覀冃枰绦虼蛴〕鑫覀冃枰闹禃r我們就能正確得到。
?
當我們創(chuàng)建好實例后,后臺真正的操作是為我們的每一個實例創(chuàng)建一定的空間,在空間內(nèi) 每個實例有自己的變量副本
Like this
?
由于關(guān)于類movie的每個實例都是唯一的,所以這些變量為“實例變量”
?此時如果把init函數(shù)中movie_storyline賦值語句中的self刪掉,那么如果print(toy_story.movie_storyline)會導致調(diào)用init函數(shù)的時候報錯toy_story沒有movie_storyline這個屬性。
?
在類中定義,并與實例相關(guān)的函數(shù):實例方法。
例如init()就是一個實例方法。并且每一個實例方法的第一個自變量都是self。
show_trailer是另外一個實例方法。打開的鏈接就儲存在self.trailer_youtube_url中,所以要訪問這個實例變量。
然后再在avatar這個實例中調(diào)用這個實例方法即可。
?
練習:
?
此處應該是調(diào)用的webbrowser的open函數(shù)而不是簡單的一個名字所以應該是點·而不是下劃線_
?
術(shù)語總結(jié):
一個新的知識點:
fresh_tomatoes這個文件中有一個open_movies_page的函數(shù)
?這個函數(shù)輸入一個電影列表,會創(chuàng)建并輸出一個html文件或網(wǎng)頁來顯示這些電影。(好牛杯!厲害了我的小西紅柿...)
但是有一個重要前提是這個西紅柿文件必須和你其他所有電影程序儲存在同一個文件夾內(nèi)才可以起作用。
?
面向?qū)ο缶幊讨械母呒壐拍?/p>
1.類變量(所有同類的實例都相同的變量)定義在類中。(類變量可能是個常量,python指南中說當它為常量最好用全大寫。)like this.
?
三個引號表示可以在多行創(chuàng)建文檔。
?
就可以打印出打引號的內(nèi)容。
注意用法print(模塊或者文件.類名和預定義的類變量)
?
面向?qū)ο缶幊痰囊淮髢?yōu)勢:reuse the code
?
劃線的語法說明child類可以繼承或重用所有parent類公開可用的所有東西。
?
要初始化所有從parent類那里繼承的變量,例如last_name和eye_color,實際上要重用類parent中init方法,like this:
而child類自己的實例變量在后面初始化。
高亮處為創(chuàng)建一個child類的實例,當運行這條代碼的時候child.__init__初始化函數(shù)會被立刻調(diào)用。
整個程序后臺運行的過程:
運行child類的實例——>child__init__函數(shù)被調(diào)用——>init函數(shù)第一行就是打印所以先打印出Child Constuctor called——>parent類的init函數(shù)被調(diào)用,所以程序從4到5class parent定義處,parent__init__被調(diào)用——>Parent Constructor called被打印——>parent類的實例變量last_name和eye_color被初始化,類parent.__init__函數(shù)成功運行后——>控制回到4處,然后到8child類的實例變量被初始化。child類的init函數(shù)被成功運行——>child實例miley_cyrus被成功創(chuàng)建。——>接下來到9按順序打印出需要打印的內(nèi)容。
like this:
?
新知識點:重寫函數(shù)(方法)
雖然在child類中沒有明確地定義方法show_info但是child從parent那里繼承了所有公開可用的信息包括show_info方法,所以可以直接用。結(jié)果like this,直接調(diào)用了parent類中定義的show_info方法。
?
?
新知識:方法覆蓋(method overriding)
?
此時在child類中再定義和parent類同名的方法show_info,當調(diào)用這個方法時,child類的方法會覆蓋掉parent類的方法。結(jié)果like this
輸出了child類中定義的show_info方法中的內(nèi)容。
?
轉(zhuǎn)載于:https://www.cnblogs.com/Yiren-33/p/6689491.html
總結(jié)
以上是生活随笔為你收集整理的编程基础python学习2完结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nth-child和nth-of-typ
- 下一篇: C#通过COM组件操作IE浏览器(四):