python self.__class__


'''
self代表類的實例,而非類
哪個對象調用方法,那么該方法中的self就代表那個對象

self.__class__ 代表類名_
'''

class Person(object):
def run(self):
print("run")
print(self.__class__)
p = self.__class__("tt", 30, 10, 30)
print(p)
def eat(self, food):
print("eat " + food)
def say(self):
print("Hello! my name is %s, I am %d years old" % (self.name, self.age))
#self不是關鍵字,換成其他的標識符也是可以的,但是帥的人都是用self
def play(a):
print("play " + a.name)
def __init__(self, name, age, height, weight):
self.name = name
self.age = age
self.height = height
self.weight = weight

per1 = Person("tom", 20, 160, 80)
per1.say()

per2 = Person("hanmeimei", 21, 160, 80)
per2.say()


per1.play()

per1.run()


免責聲明!

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



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