# 类与对象
# 对象:有属性(特征)和函数/方法(行为),是类的实例
# 类:关键字是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是判断的类
Python学习笔记:类和对象
于 2022-06-28 07:15:56 首次发布