1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      如何在Python中使用類和實例-創(chuàng)新互聯(lián)

      今天就跟大家聊聊有關(guān)如何 在Python中使用類和實例,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

      創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計、網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)宣漢,10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575

      雖然 Python 是解釋性語言,但是它是面向?qū)ο蟮?,能夠進行對象編程。至于何為面向?qū)ο?,在此就不詳說了。面向?qū)ο蟪绦蛟O(shè)計本身就很值得深入學習,如要了解,請參閱網(wǎng)上其他的資料。

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

      以Student類為例,在Python中,定義類是通過 class 關(guān)鍵字:

      注意: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__ 構(gòu)造函數(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__ 析構(gòu)函數(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.

      相對于 構(gòu)造函數(shù) Python 也有類似 C++ 中的析構(gòu)函數(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有哪些常用庫

      python常用的庫:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。

      看完上述內(nèi)容,你們對如何 在Python中使用類和實例有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。


      分享題目:如何在Python中使用類和實例-創(chuàng)新互聯(lián)
      本文URL:http://ef60e0e.cn/article/jhpdo.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        固镇县| 芦溪县| 泰来县| 溧阳市| 保山市| 铜鼓县| 德令哈市| 永嘉县| 垫江县| 右玉县| 莱西市| 婺源县| 宣城市| 广州市| 全州县| 内江市| 启东市| 平邑县| 石门县| 南溪县| 博乐市| 深水埗区| 乌兰浩特市| 黑龙江省| 隆尧县| 弥勒县| 项城市| 双城市| 山阳县| 建水县| 承德市| 扎鲁特旗| 浑源县| 合肥市| 定边县| 东乡| 紫阳县| 平利县| 临湘市| 宣城市| 永清县|