程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python學習筆記:類和對象

編輯:Python
# 類與對象
# 對象:有屬性(特征)和函數/方法(行為),是類的實例
# 類:關鍵字是class,是創建對象的模板
class Human:
# 構造函數,初始化函數
def __init__(self, breed, sex, height = '170'):#默認值
# 定義三個屬性
self.breed = breed
self.sex = sex
self.height = height
# def __init__(self):
# # 定義三個屬性
# self.breed = None
# self.sex = None
# self.height = None
# 定義的3個函數
def study(self):
print("學習")
def eat(self):
print("吃飯")
def sleep(self):
print("睡覺")
print("一個%s血型,性別:%s,身高:%s" % (self.breed, self.sex, self.height))
# 類和對象的關系:類是創建對象的模板,對象是累的實例
human = Human('A', '女')
# human.breed = 'A'
# human.sex = '女'
# human.height = '170'
human.eat()
human.sleep()
human.study()
# 判斷函數type()
print(type(human)) # Human類型
print(type(human) == Human)
# isinstance判斷是否是實例?
print(isinstance(human, Human)) # 參數1是對象,參數2是判斷的類


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved