利用函数来引入面向对象
在了解python面向对象概念,我们先用函数来实现
# #方法方式定义类
def Dog():
def bite(person):
person["total"]-=5
dog = {
"name":"alpha",
"total":100,
"bite":bite
}
return dog
def Person():
def stack(dog):
dog["total"]-=5
person = {
"name":"safly",
"total":100,
"stack":stack
}
return person
dog = Dog()
person = Person()
person["stack"](dog)
print(dog)
print(person)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/safly.py
{'name': 'alpha', 'total': 95, 'bite': <function Dog.<locals>.bite at 0x007F1810>}
{'name': 'safly', 'total': 100, 'stack': <function Person.<locals>.stack at 0x007F17C8>}
Process finished with exit code 0
面向对象(类名调用)
#类定义
class Person:
def walk(self):
print("self",self)
print("walk")
country = "中国"
mWalk = walk
#获取属性2种方法
print(Person.country)
print(Person.__dict__["country"])
#调用方法
print(Person.walk)
#如下错误missing 1 required positional argument: 'self'
# print(Person.walk())
Person.walk(1)
Person.mWalk("mmmmm")
#通过如下的方式修改
Person.country = "美国"
print(Person.country)
#不能修改__dict__
# Person.__dict__["country"] = "美国"
# print(Person.__dict__["country"])
#直接添加属性
Person.role = "人"
print(Person.role)
# 删除属性
del Person.role
# print(Person.role)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/safly.py
中国
中国
<function Person.walk at 0x036817C8>
self 1
walk
self mmmmm
walk
美国
人
Process finished with exit code 0
1、类名.静态属性
2、类名.方法(self) 必须传参数!!!
否则TypeError: walk() missing 1 required positional argument: ‘self’
类调用的一些迷惑用法
def method():
print("out method")
class Animal():
name = "animal"
method = method
def method1(self, a):
print("method1",a)
def method2(a):
print("method2", a)
a = Animal()
print(a.method1(1))
print("--------")
#如下方式不行
# a.method()
#如下方式也不行
# a.method2(2)
#如下可以调用类外函数
Animal.method()
print("--------")
Animal.method2(111)
print("--------")
Animal.method1(Animal,333)
print("--------")
Animal.method1(a,333)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/python.py
method1 1
None
--------
out method
--------
method2 111
--------
method1 333
--------
method1 333
Process finished with exit code 0
面向对象(实例调用)
# #类定义
class Person:
#动态属性、函数属性、方法
def walk(self):
print(self)
print("walk")
# 静态属性(数据属性)
country = "中国"
mWalk = walk
p = Person()
print(p.country)
p.walk()
p.mWalk()
p.role = "driver"
print(p.role)
# 如下2种报错!!!!!!!!!!
# p.mWalk(33)
# p.walk("呵呵")
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/safly.py
中国
<__main__.Person object at 0x00B32270>
walk
<__main__.Person object at 0x00B32270>
walk
driver
Process finished with exit code 0
实例.方法(self) 不需要传递参数
面向对象(_init_)
class Person:
#动态属性、函数属性、方法
def walk(self):
print(self)
print("walk")
# 静态属性(数据属性)
country = "中国"
mWalk = walk
#添加属性返回初始化对象
def __init__(self):
#内存地址
print(id(self))
print("init")
#把上面的无参__init__覆盖了,使用如下的初始函数
def __init__(self,name):
#内存地址
self.name = name
print(id(self))
print("self",self)
print("init",name)
#init会被覆盖,看执行顺序!!!!!
p = Person("saly")
# 以下报错
# p = Person()
# 内存地址
print(id(p))
print(p.__dict__)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/safly.py
11740496
self <__main__.Person object at 0x00B32550>
init saly
11740496
{'name': 'saly'}
Process finished with exit code 0
再来看_init_的代码
class Person:
def __init__(self,name):
self.name = name
print(id(self),self)
def walk(self):
print(id(self), self)
print("walk",self.name)
p = Person("safly")
Person.__init__(p,"rrr")
print(p.__dict__)
print("------------")
#走__init__函数
p = Person("safly")
p.walk()
print("------------")
p = Person("safly")
Person.walk(p)
#报错么有该属性
# Person.walk(11)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/safly.py
46015088 <__main__.Person object at 0x02BE2270>
46015088 <__main__.Person object at 0x02BE2270>
{'name': 'rrr'}
------------
46015856 <__main__.Person object at 0x02BE2570>
46015856 <__main__.Person object at 0x02BE2570>
walk safly
------------
46015952 <__main__.Person object at 0x02BE25D0>
46015952 <__main__.Person object at 0x02BE25D0>
walk safly
Process finished with exit code 0
如下也是构造对象的另外方式
#构造对象
class Person:
def __init__(self, name):
print(self,name)
self.name = name
return self
p = Person.__init__(Person,"ddd")
print(p.name)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/safly.py
<class '__main__.Person'> ddd
ddd
Process finished with exit code 0
对象属性的增删改查2种方式
通过实例.dict进行增删改查
或者实例.属性方法实现
class Person:
#动态属性、函数属性、方法
def walk(self):
print("walk")
# 静态属性(数据属性)
country = "中国"
def __init__(self,name):
#内存地址
self.name = name
p = Person("saly")
#修改
print(p.__dict__["name"])
p.__dict__["name"] = "aaa"
print(p.__dict__)
#添加
p.__dict__["sex"] = "nv"
print(p.__dict__)
#删除
p.__dict__.pop("sex")
print(p.__dict__)
#添加新方式
p.sex = "nv"
print(p.sex)
print(p.__dict__)
p.sex = "nan"
print(p.sex)
print(p.__dict__)
#删除
del p.sex
print(p.__dict__)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/safly.py
saly
{'name': 'aaa'}
{'name': 'aaa', 'sex': 'nv'}
{'name': 'aaa'}
nv
{'name': 'aaa', 'sex': 'nv'}
nan
{'name': 'aaa', 'sex': 'nan'}
{'name': 'aaa'}
Process finished with exit code 0
练习demo
#人类 :
# 属性 :life_value,aggr,name,job
# 方法: attack
class Person:
def __init__(self,life_value,aggr,name,job):
self.life = life_value
self.aggressive = aggr
self.name = name
self.job = job
def attack(self,dog_obj): #boss_gold,tiedan
print('%s 攻击了 %s'%(self.name,dog_obj.name))
dog_obj.life = dog_obj.life - self.aggressive
#狗类:
# 属性:life_value,aggr,name,kind
# 方法:bite
class Dog:
def __init__(self,life_value,aggr,name,kind):
self.life = life_value
self.aggressive = aggr
self.name = name
self.kind = kind
def bite(self,person_obj):
print('%s 咬了 %s' % (self.name, person_obj.name))
person_obj.life -= self.aggressive
tiedan = Dog(1000,100,'铁蛋','土狗')
boss_gold = Person(100,2.5,'太黑','old_driver')
boss_gold.attack(tiedan) #Person.attack(boss_gold,tiedan)
print(tiedan.life)
输出如下:
E:\python\python_sdk\python.exe "E:/python/py_pro/5. 面向对象交互.py"
太黑 攻击了 铁蛋
997.5
Process finished with exit code 0