亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python面向?qū)ο笾惡蛯嵗梅ǚ治?/h1>
系統(tǒng) 1524 0

本文實例講述了Python面向?qū)ο笾惡蛯嵗梅ā7窒斫o大家供大家參考,具體如下:

雖然 Python 是解釋性語言,但是它是面向?qū)ο蟮模軌蜻M行對象編程。至于何為面向?qū)ο螅诖司筒辉斦f了。面向?qū)ο蟪绦蛟O計本身就很值得深入學習,如要了解,請參閱網(wǎng)上其他的資料。

面向?qū)ο笞钪匾母拍罹褪穷悾–lass)和實例(Instance),牢記 類 是抽象的模板,比如Student類,而實例是根據(jù)類創(chuàng)建出來的一個個具體的“對象”,每個對象都擁有相同的方法,但各自的數(shù)據(jù)可能不同。

以Student類為例,在Python中,定義類是通過 class 關鍵字:

注意 :Python 2.X 需要在類名后面加 (object)? 這邊 pass 語句表示空語句段。

class 后面緊接著是類名,即Student,類名通常是大寫開頭的單詞,緊接著是(object),表示該類是從哪個類繼承下來的,繼承的概念我們后面再講,通常,如果沒有合適的繼承類,就使用object類,這是所有類最終都會繼承的類。

            
class Student(object):
  pass
          

Python 3.X 則只需要類名,不需要加 (object)

            
class Student:
  pass
          

實例

創(chuàng)建實例是通過類名+()實現(xiàn)的(若 __init__ 無或僅有self);如定義好了Student類,就可以根據(jù)Student類創(chuàng)建出Student的實例,如下:

            
class Student(object):
  pass
May = Student()          # 新建May實例
print(May)


          

運行結(jié)果:

<__main__.Student object at 0x0000000001DCC400>

可以看到,變量May指向的就是一個Student的object,后面的0x006C4770是內(nèi)存地址,每個object的地址都不一樣,而Student本身則是一個類。

可以自由地給一個實例變量綁定屬性,比如,給實例 May 綁定一個 name 屬性,這個 name 屬性是實例 May 特有的,其他新建的實例是沒有 name 屬性的

            
class Student(object):
  pass
May = Student()         # 新建May實例
print(May)
May.name = "May"         # 給實例 May 綁定 name 屬性為 "May"
print(May.name)
Peter = Student()        # 新建Peter實例
# print(Peter.name)       # 報錯,因為Peter沒有Name屬性


          

那么,如果我們需要類必須綁定屬性,那如何定義呢?? 請參見下文。

__init__ 構造函數(shù)

由于類可以起到模板的作用,因此,可以在創(chuàng)建實例的時候,把一些我們認為必須綁定的屬性強制填寫進去。 ( 注意 __init__ 雙下劃線)

如對于Student類,我們定義 name 和 score 屬性(所有Sudent 都須有的屬性):

__init__ 方法的第一個參數(shù)永遠是 self ,表示創(chuàng)建的實例本身,因此,在 __init__ 方法內(nèi)部,就可以把各種屬性綁定到self,因為self就指向創(chuàng)建的實例本身。

有了 __init__ 方法,在創(chuàng)建實例的時候,就不能傳入空的參數(shù)了,必須傳入與 __init__ 方法匹配的參數(shù),但 self 不需要傳,Python解釋器自己會把實例變量傳進去:

            
class Student(object):
  def __init__(self, name, score):
    self.name = name
    self.score = score
May = Student("May",90)      # 須要提供兩個屬性
Peter = Student("Peter",85)
print(May.name, May.score)
print(Peter.name, Peter.score)


          

__del__ 析構函數(shù)

Just like the __init__ method, there is another special method __del__ which is called when an object is going to die i.e. it is no longer being used and is being returned to the computer system for reusing that piece of memory.

The __del__ method is run when the object is no longer in use and there is no guarantee when that method will be run. If you want to explicitly see it in action, we have to use the del statement which is what we have done here.

相對于 構造函數(shù) Python 也有類似 C++ 中的析構函數(shù) __del__ , Python的垃圾回收過程與常用語言的不一樣,如果一定需要,最好需要使用del語句來激活。

私有變量

Note for C++/Java/C# Programmers
All class members (including the data members) are public and all the methods are virtual in Python.
One exception: If you use data members with names using the double underscore prefix such as __privatevar, Python uses name-mangling to effectively make it a private variable.
Thus, the convention followed is that any variable that is to be used only within the class or object should begin with an underscore and all other names are public and can be used by other classes/objects. Remember that this is only a convention and is not enforced by Python (except for the double underscore prefix).

Python 中定義私有變量,命名規(guī)則為前綴加兩個下劃線 “__” ,注意不可前后都包含__XXX__(該命名表示為類屬性或內(nèi)建變量);還有一種命名為單下劃線 _XXX ,表示約定俗成不可訪問的變量。

            
class Person(object):
  def __init__(self,name,sex):
    self.name = name
    self.__sex = sex       # sex 定義為私有變量
  def print_title(self):
    if self.sex == "male":
      print("man")
    elif self.sex == "female":
      print("woman")
May = Person("May","female")
print(May.name)    
print(May.__sex)           # 會報錯


          

更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O計入門與進階教程》、《Python數(shù)據(jù)結(jié)構與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進階經(jīng)典教程》

希望本文所述對大家Python程序設計有所幫助。


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 青青青久在线视频免费观看 | 久久综合九九亚洲一区 | 午夜久久久精品 | 成年人一级黄色片 | 欧洲欧美成人免费大片 | 亚洲国产精品一区二区九九 | 香蕉一区二区 | 天天色天天操天天 | 91在线精品老司机免费播放 | 黄wwwwww| 日本一级网站 | 国产成人精品视频频 | 久久中文字幕一区二区三区 | 天天干天天爽天天射 | aⅴ免费在线观看 | 久久九色综合九色99伊人 | 久久精品国产影库免费看 | 国产精品欧美一区二区三区不卡 | 又刺激又黄的一级毛片 | 福利姬视频在线观看 | 成人毛片18岁女人毛片免费看 | 国产色网址 | 免费一看一级毛片人 | 综合视频网 | 色综合综合色 | 亚洲精品国产自在久久出水 | 九九九九热精品免费视频 | 九九爱这里只有精品 | 香蕉视频在线网站 | 奇米第四色7777 | 久久精品亚洲 | 久久久久99精品成人片三人毛片 | 日批视频网址免费观看 | 一级日本特黄毛片视频 | 午夜在线精品不卡国产 | 69久成人做爰视频 | 四虎网站最新 | 国产成人精品日本亚洲专一区 | 人人干人 | 国产区精品一区二区不卡中文 | 亚洲精品一二三区 |