溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python中class和對象基本用法是什么

發(fā)布時間:2021-10-27 16:52:29 來源:億速云 閱讀:123 作者:柒染 欄目:編程語言

本篇文章為大家展示了Python中class和對象基本用法是什么,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

  主要介紹了Python 面向對象之類class和對象基本用法,結合實例形式詳細分析了Python面向對象程序設計中類class和對象基本概念、原理、使用方法與操作注意事項,需要的朋友可以參考下。

  類(class):定義一件事物的抽象特點,usually,類定義了事物的屬性和它可以做到的性為對象(object):是類的實例。

  1.基本點

  < code class="prism language-bash">class MyClass(object):

  message = "hello,world"

  def show(self):

  print (self.message)

  < /code>

  類名為MyClass 有一個成員變量:message,并賦予初值。

  類中定義了成員函數(shù)show(self),注意類中的成員函數(shù)必須帶有參數(shù)self。

  參數(shù)self是對象本身的引用,在成員函數(shù)中可以引用self參數(shù)獲得對象的信息。

  輸出結果:

  < code class="prism language-bash">inst = Myclass() # 實例化一個MyClass 的對象

  inst.show # 調(diào)用成員函數(shù),無需傳入self參數(shù)

  hello,world

  < /code>

  注: 通過在類名后面加小括號可以直接實例化類來獲得對象變量,使用對象變量可以訪問類的成員函數(shù)與成員變量。

  2.構造函數(shù)

  構造函數(shù)是一種特殊的類成員方法,主要用來創(chuàng)建對象初始化,python 中的類構造函數(shù)用__init__命名:

  < code class="prism language-bash">class MyClass(object):

  message = 'Hello, Developer.'

  def show(self):

  print self.message

  def __init__(self):

  print "Constructor is called"

  inst = MyClass()

  inst.show()

  >>>

  < /code>

  打印結果:

  < code class="prism language-bash">>>>Constructor is called

  >>>Hello, Developer.

  < /code>

  注:構造函數(shù)不能有返回值,python 中不能定義多個構造函數(shù),但可以通過為命名參數(shù)提供默認值的方式達到用多種方式構造對象的目的。

  3.析構函數(shù)

  是構造的反向函數(shù),在銷毀或者釋放對象時調(diào)用他們。

  python 中為類定義析構函數(shù)的方法在類定義中定義一個名為__del__的沒有返回值和參數(shù)的函數(shù)。

  < code class="prism language-bash">class MyClass(object):

  message = 'Hello, Developer.'

  def show(self):

  print self.message

  def __init__(self, name = "unset", color = "black"):

  print "Constructor is called with params: ",name, " ", color

  def __del__(self):

  print "Destructor is called!"

  inst = MyClass()

  inst.show()

  inst2 = MyClass("David")

  inst2.show()

  del inst, inst2

  inst3 = MyClass("Lisa", "Yellow")

  inst3.show()

  del inst3

  >>>

  < /code>

  4.實例成員變量

  構造函數(shù)中定義self引用的變量,因此這樣的成員變量在python中叫做實例成員變量。

  < code class="prism language-bash">def __init__(self, name = "unset", color = "black"):

  print "Constructor is called with params: ",name, " ", color

  self.name = name

  self.color = color

  < /code>

  5.靜態(tài)函數(shù)和類函數(shù):

  python 支持兩種基于類名訪問成員的函數(shù):靜態(tài)函數(shù),類函數(shù)。

  區(qū)別在于:類函數(shù)有一個隱形參數(shù)cls可以用來獲取類信息。而靜態(tài)函數(shù)沒有該函數(shù)。

  靜態(tài)函數(shù)用裝飾器:@staticmethod定義

  類函數(shù)使用裝飾器:@classmethod定義

  < code class="prism language-bash">class MyClass(object):

  message = 'Hello, Developer.'

  def show(self):

  print (self.message)

  print ("Here is %s in %s!" % (self.name, self.color))

  @staticmethod

  def printMessage():

  print ("printMessage is called")

  print (MyClass.message)

  @classmethod

  def createObj(cls, name, color):

  print ("Object will be created: %s(%s, %s)"% (cls.__name__, name, color))

  return cls(name, color)

  def __init__(self, name = "unset", color = "black"):

  print ("Constructor is called with params: ",name, " ", color)

  self.name = name

  self.color = color

  def __del__(self):

  print ("Destructor is called for %s!"% self.name)

  MyClass.printMessage()

  inst = MyClass.createObj( "Toby", "Red")

  print (inst.message)

  del inst

  < /code>

  6.私有成員

  python 使用指定變量名格式的方法定義私有成員,即所有以雙下劃線“__”開始命名的成員都為私有成員。

  < code class="prism language-bash">class MyClass(object):

  def __init__(self, name = "unset", color = "black"):

  print "Constructor is called with params: ",name, " ", color

  self.__name = name

  self.__color = color

  def __del__(self):

  print "Destructor is called for %s!"% self.__name

  inst = MyClass("Jojo", "White")

  del inst

  < /code>

上述內(nèi)容就是Python中class和對象基本用法是什么,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI