肺热,生死时速5,qingersm
什么是对象?
什么事面向对象?
对象都有什么特征?
什么是类?
车(类) -------> byd e6 (京a.88888) 实例
---------------> bmw x5 (xxxxxx) 实例
狗(类) -------> 金毛犬 编号:007 实例
----------------> 萨摩耶 编号:008 实例
类 | 对象 | 实例 |
---|---|---|
class | object | instance |
语法:
class 类名(继承列表) '''类的文档字符串''' 示例方法定义(类内的函数称为方法method) 类变量定义 类方法定义 静态方法定义
作用:
构造函数
实例(对象)说明
class dog: # 定义一个类,类名为dog pass dog1 = dog() # 创建dog类的对象 dog2 = dog() # 创建dog类的另一个对象 #类似如下语句: int1 = int() int2 = int()
语法:
class 类名(继承列表):
def 实例方法名(self, 参数1, 参数2, ...)
'''实例方法的文档字符串'''
语句块
实例方法的调用语法:
实例.实例方法名(调用传参)
或
类名.示例方法名(实例调用传参)
class dog: def eat(self, food): '''此方法用来描述小狗吃东西的行为''' print("小狗正在吃:", food) def sleep(self, hour): print("小狗睡了:", hour, "小时") def play(self, obj): print("小狗正在玩:", obj) # 创建一个dog的类的实例 dog1 = dog() dog1.eat("狗粮") dog1.sleep(1) dog1.play("球") # 创建另一个dog对象 dog2 = dog() dog2.play("飞盘") # 可以用以下方法代替 dog.play(dog2, "杂技") dog2.sleep(2) dog2.eat("骨头")
小狗正在吃: 狗粮 小狗睡了: 1 小时 小狗正在玩: 球 小狗正在玩: 飞盘 小狗正在玩: 杂技 小狗睡了: 2 小时 小狗正在吃: 骨头
每个实例都可以有自己的变量,此变量称为实例变量(属性)
语法:
实例.属性名
class dog: def eat(self, food): print(self.color, '的', self.kinds, '正在吃', food) pass # 创建第一个对象 dog1 = dog() dog1.kinds = '金毛' # 添加属性kinds dog1.color = '白色' # 添加属性color dog1.color = '黄色' # 改变属性color绑定关系 print(dog1.color, '的', dog1.kinds) dog2 = dog() dog2.kinds = '萨摩耶' dog2.color = '灰色' print(dog2.color, '的', dog2.kinds) dog1.eat("骨头") dog2.eat("包子")
黄色 的 金毛 灰色 的 萨摩耶 黄色 的 金毛 正在吃 骨头 灰色 的 萨摩耶 正在吃 包子
class cat: pass c1 = cat() c1.color = '白色' print(c1.color) del c1.color
白色
语法:
class 类名(继承列表):
def __init__(self [,形参列表]):
语句快
# 此实例示意__init__方法的自助调用以及添加实例变量 class car: def __init__(self, c, b, m): print('__init__方法被调用') self.color = c # 颜色 self.brand = b # 品牌 self.model = m # 型号 def run(self, speed): print(self.color, '的', self.brand, self.model, '正在以', speed, '公里/小时的速度行驶!') def set_color(self, clr): '''此方法用来修改车的颜色信息''' self.color = clr a4 = car('红色', '奥迪', 'a4') # a4.__init__(.....) 显示调用 print(a4.color) a4.run(179) a4.set_color('黑色') a4.run(10)
__init__方法被调用 红色 红色 的 奥迪 a4 正在以 179 公里/小时的速度行驶! 黑色 的 奥迪 a4 正在以 10 公里/小时的速度行驶!
语法:
class 类名(继承列表):
def del(self):
语句块
python不建议在析构方法内做任何事情,因为对象销毁的时间难以确定
class car: def __init__(self, name): self.name = name print('汽车', name, '对象已经创建!') def __del__(self): print(self.name, "对象已经销毁") c1 = car('byd e6') c1 = car('bmw x5')
汽车 byd e6 对象已经创建! bmw x5 对象已经销毁 汽车 bmw x5 对象已经创建! byd e6 对象已经销毁
class dog: pass print(dog1.__dict__) dog1.kinds = '金毛' print(dog1.__dict__)
{'kinds': '金毛', 'color': '黄色'} {'kinds': '金毛', 'color': '黄色'}
class dog: pass dog1 = dog() dog2 = dog() dog3 = dog1.__class__() dog3.__class__ is dog
true
有两个人:
行为:
1. 教别人学东西 teach
2. 赚钱
3. 借钱
事情:
- 张三 教 李四 学 python
- 李四 教 张三 学 跳皮筋
- 张三 上班赚了 1000元
- 李四向张三借了200元
# 此示例示意如何用面向对象的方式创建对象, # 并建立对象与对象之间的逻辑关系 class human: '''人类,用于描述人的行为''' def __init__(self, n, a): self.name = n self.age = a self.money = 0 def teach(self, other, skill): print(self.name, '教', other.name, '学', skill) def works(self, money): self.money += money print(self.name, '工作赚了', money, '元钱') def borrow(self, other, money): if other.money > money: print(other.name, '借给', self.name, money, '元钱') other.money -= money self.money += money else: print(other.name, '不借给', self.name) def show_info(self): print(self.age, '岁的', self.name, '存有', self.money, '元钱') # 以下的类的使用 zhang3 = human('张三', 35) li4 = human('李四', 8) zhang3.teach(li4, 'python') li4.teach(zhang3, '跳皮筋') zhang3.works(1000) li4.borrow(zhang3, 200) zhang3.show_info()
张三 教 李四 学 python 李四 教 张三 学 跳皮筋 张三 工作赚了 1000 元钱 张三 借给 李四 200 元钱 35 岁的 张三 存有 800 元钱
class dog: pass class cat: pass animal = dog() isinstance(animal, dog) # t isinstance(animal, cat) # f isinstance(animal, (cat, int, list)) # f isinstance(animal, (cat, int, dog)) # t
true
类变量是类的属性,此属性属于类
# 此示例表示类变量的定义和使用 class human: count = 0 # 创建一个类变量 print("human的类变量count=", human.count) human.count = 100 print("human的类变量count=", human.count) class human: count = 0 h1 = human() print("用h1对象访问的类变量count=", h1.count) h1.count = 100 print("用h1对象访问的类变量count=", h1.count, "但是human的类变量还是", human.count) h1.__class__.count += 1 print("用h1对象访问的类变量还是count=", h1.count, "但是human的类变量变了", human.count)
human的类变量count= 0 human的类变量count= 100 用h1对象访问的类变量count= 0 用h1对象访问的类变量count= 100 但是human的类变量还是 0 用h1对象访问的类变量还是count= 100 但是human的类变量变了 1
# 此示例示意用类变量来记录对象的个数 class car: count = 0 # 创建类变量,用来记录汽车对象的总数 def __init__(self, info): print(info, '被创建') self.data = info # 记录传入数据 self.__class__.count += 1 # 让车的总数加1 def __del__(self): print(self.data, '被销毁') self.__class__.count -= 1 # 当车被销毁总数减1 b1 = car("byd") b2 = car("ttesla") b3 = car("audi") print("当前汽车总数是", car.count) del b3 print("当前汽车总数是", car.count)
byd 被创建 byd 被销毁 ttesla 被创建 ttesla 被销毁 audi 被创建 当前汽车总数是 3 audi 被销毁 当前汽车总数是 2
类内第一个没有赋值给任何变量的字符串是类的文档字符串
class car: '''此类用来描述车的对象的行为 这是car类的文档字符串''' def run(self, speed): '''车的run方法''' pass help(car) car.__doc__
help on class car in module __main__: class car(builtins.object) | 此类用来描述车的对象的行为 | 这是car类的文档字符串 | | methods defined here: | | run(self, speed) | 车的run方法 | | ---------------------------------------------------------------------- | data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) '此类用来描述车的对象的行为\n 这是car类的文档字符串'
# 此示例示意类的__slots__列表的作用 class student: def __init__(self, name, score): self.name = name self.score = score s1 = student('小张', 58) print(s1.score) s1.socre = 100 # 此处错写了属性名!但运行时不报错! print(s1.socre) # 这里打印出了第三个属性 class student: __slots__ = ['name', 'score'] def __init__(self, name, score): self.name = name self.score = score s1 = student('小张', 58) print(s1.score) s1.socre = 100 # 此处出错,是因为已经用slots限定了变量
58 100 58 --------------------------------------------------------------------------- attributeerror traceback (most recent call last) <ipython-input-19-5cac1bf3ae50> in <module> 18 s1 = student('小张', 58) 19 print(s1.score) ---> 20 s1.socre = 100 # 此处错写了属性名!但运行时不报错! 21 print(s1.socre) # 这里打印出了第三个属性 attributeerror: 'student' object has no attribute 'socre'
类方法是描述类的行为的方法,类方法属于类
# 此示例示意类方法的定义方法和用法 class car: count = 0 @classmethod def gettotalcount(cls): '''此方法为类方法, 第一个参数为cls,代编调用此方法的类''' return cls.count @classmethod def updatecount(cls, number): cls.count += number print(car.gettotalcount()) # 用类来调用类方法 #car.count += 1 # 面向对象思想不提倡直接操作属性 car.updatecount(1) print(car.gettotalcount()) c1 = car() c1.updatecount(100) # car类的实例也可以调用类方法 print(c1.gettotalcount()) # 101
0 1
# 此示例示意静态方法的创建和使用 class a: @staticmethod def myadd(x, y): '''此方法为静态方法 此方法的形参不需要传入类或实例''' return x+y print('1+2=', a.myadd(1, 2)) a = a() print('100+200=', a.myadd(100, 200))
1+2= 3 100+200= 300
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python 实现将numpy中的nan和inf,nan替换成对应的均值
python爬虫把url链接编码成gbk2312格式过程解析
网友评论