最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python函數(shù)調(diào)用的幾種方式(類里面,類之間,類外面)

 更新時(shí)間:2023年07月07日 09:49:44   作者:一紙春秋  
本文主要介紹了Python函數(shù)調(diào)用的幾種方式(類里面,類之間,類外面),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

第一種 是在class內(nèi)部,方法之間的互相調(diào)用

舉一個(gè)簡(jiǎn)單的例子

class cat():
    def __init__(self,age):
        cat.age = age
    def show_color(self):
        print("The color of the cat is white")
    def show_age(self):
        self.show_color()
        print("The age of the cat is "+str(self.age))
Ragdoll = cat(2)
Ragdoll.show_age()

結(jié)果為

這里我只用實(shí)例Ragdoll調(diào)用了show_age方法,但因?yàn)樵趕how_age中調(diào)用了show_color方法,所以兩個(gè)方法最后都執(zhí)行了

類里面的方法想調(diào)用另外一個(gè)方法,需要在方法體中用self.方法名的方式來調(diào)用

同樣,類里面的方法想調(diào)用類的屬性,需要用self.屬性名的方式

第二種 是在類的外面,def函數(shù)之間的彼此調(diào)用

還是來看一個(gè)簡(jiǎn)單的例子

def show_num(num):
    print(num)
def test(num):
    if num>3:
        show_num(num)
test(4)

結(jié)果為

在類外面的函數(shù)之間,只需要函數(shù)名就可以調(diào)用彼此

另外要記得參數(shù)列表保持一致,如果你要調(diào)用的函數(shù)參數(shù)列表中有多個(gè)參數(shù),那么自身這個(gè)函數(shù)的參數(shù)列表中也要有那些參數(shù)

以下是錯(cuò)誤示范

def create_cat(color,age):
    print("The age and color of the cat are"+str(age)+color)
def show_cat():
    create_cat(color,age)
show_cat()

結(jié)果是

正確的函數(shù)調(diào)用是這樣

def create_cat(color,age):
    print("The age and color of the cat are "+str(age)+" and "+color)
def show_cat(color,age):
    create_cat(color,age)
show_cat("white",3)

結(jié)果是

第三種 是在類外面的函數(shù),調(diào)用在類里面的方法

class cat():
    def __init__(self,age):
        self.age = age
    def age_increase(self):
        self.age += 1
        print("the age of the cat is "+str(self.age))
def year():
    Ragdoll = cat(3)
    Ragdoll.age_increase()
year()

結(jié)果為

創(chuàng)建該類的實(shí)例對(duì)象,再通過實(shí)例調(diào)用方法,最后運(yùn)行函數(shù)即可

class cat():
    def __init__(self,age):
        self.age = age
        self.color = "white"
    def age_increase(self):
        self.age += 1
        print("the age of the cat is "+str(self.age))
def year():
    Ragdoll = cat(3)
    Ragdoll.age_increase()
    print(Ragdoll.color)
year()        

在函數(shù)中直接用實(shí)例調(diào)用屬性,獲取屬性值也是可以的結(jié)果為

第四種 是不同的類之間的方法的彼此調(diào)用

class cat():
    def __init__(self,age1):
        self.age1 = age1
    def show_age(self):
        print("the age of the cat is "+str(self.age1))
class dog():
    def __init__(self,age2):
        self.age2 = age2
    def show_cat_age(self):
        Ragdoll = cat(3)
        Ragdoll.show_age()
Husky = dog(2)
Husky.show_cat_age()

結(jié)果為

而如果想在這個(gè)類里面調(diào)用其他類里面的屬性值,則需要這樣做

class cat():
    def __init__(self,age1):
        self.age1 = age1
        self.color = "white"
    def show_age(self):
        print("the age of the cat is "+str(self.age1))
class dog():
    def __init__(self,age2,Ragdoll):
        self.age2 = age2
        self.Ragdoll = Ragdoll
    def show_cat_age(self,Ragdoll):
        Ragdoll = cat(3)
        Ragdoll.show_age()
        print(self.Ragdoll.color)
        if self.Ragdoll.age1 > self.age2:
            print("the cat is older than the dog")
def run():
    Ragdoll = cat(3)
    Husky = dog(2,Ragdoll)
    Husky.show_cat_age(Ragdoll)
run()

結(jié)果為

需要將對(duì)象作為參數(shù)放入類里面

到此這篇關(guān)于Python函數(shù)調(diào)用的幾種方式(類里面,類之間,類外面)的文章就介紹到這了,更多相關(guān)Python函數(shù)調(diào)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

包头市| 福泉市| 运城市| 工布江达县| 运城市| 上饶县| 峨山| 土默特右旗| 怀远县| 花莲县| 临海市| 西林县| 弥勒县| 任丘市| 内丘县| 黄骅市| 平乡县| 墨脱县| 海原县| 长武县| 宝兴县| 孟津县| 中方县| 临清市| 交口县| 海南省| 丹棱县| 青海省| 桐柏县| 寿宁县| 藁城市| 贵阳市| 鄂尔多斯市| 云林县| 阳朔县| 屏东市| 南漳县| 通渭县| 桂阳县| 青铜峡市| 清丰县|