Python __init__函數的使用


class Cat:
        def __init__(self,_name):
                self.name = _name
        def eat(self):
                print("i am eating .")
        def show(self):
                print("name is %s"%self.name)


tom = Cat("tom")

tom.show()


print("------------");

lanmao = Cat("lanpang")

lanmao.show()
python類的創建過程
1.分配內存空間
2.調用__init__方法,並且傳遞參數
3.將地址返回給變量
__init__函數的使用,使得python中的類更加接近於c++中的類,通過在__init__函數中定義變量達到在類中統一定義屬性的目的

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM