成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

Python中class和對象基本用法是什么-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)公司專注于銀海企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站定制開發(fā)。銀海網(wǎng)站建設(shè)公司,為銀海等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站制作,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

主要介紹了Python 面向?qū)ο笾恈lass和對象基本用法,結(jié)合實例形式詳細(xì)分析了Python面向?qū)ο蟪绦蛟O(shè)計中類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ù)獲得對象的信息。

輸出結(jié)果:

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

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

hello,world

< /code>

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

2.構(gòu)造函數(shù)

構(gòu)造函數(shù)是一種特殊的類成員方法,主要用來創(chuàng)建對象初始化,python 中的類構(gòu)造函數(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>

打印結(jié)果:

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

>>>Hello, Developer.

< /code>

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

3.析構(gòu)函數(shù)

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

python 中為類定義析構(gòu)函數(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.實例成員變量

構(gòu)造函數(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和對象基本用法是什么,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享標(biāo)題:Python中class和對象基本用法是什么-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://jinyejixie.com/article38/jsjsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)虛擬主機(jī)、搜索引擎優(yōu)化網(wǎng)站營銷、響應(yīng)式網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司
宁乡县| 栾城县| 都兰县| 阿合奇县| 禄丰县| 大安市| 阿巴嘎旗| 林州市| 宝应县| 富顺县| 越西县| 双桥区| 黄山市| 赤城县| 濮阳市| 突泉县| 息烽县| 无极县| 全椒县| 望城县| 平安县| 化德县| 池州市| 茶陵县| 四川省| 水富县| 洛扎县| 伊金霍洛旗| 四子王旗| 孝昌县| 合山市| 大埔区| 榆社县| 安泽县| 库伦旗| 原阳县| 望奎县| 成安县| 萨迦县| 东兰县| 库伦旗|