标签:VID fun dict 绑定 而在 val test font 多个
# 新功能的可调用对象
# print(property)
"""
成人的BMI数值:
过轻:低于18.5
正常:18.5-23.9
过重:24-27
肥胖:28-32
非常肥胖, 高于32
体质指数(BMI)=体重(kg)÷身高^2(m)
EX:70kg÷(1.75×1.75)=22.86
"""
# 案例1:
class People:
def __init__(self, name, weight, height):
self.name = name
self.weight = weight
self.height = height
# 定义函数的原因1:
# 1、从bmi的公式上看,bmi应该是触发功能计算得到的
# 2、bmi是随着身高、体重的变化而动态变化的,不是一个固定的值
# 说白了,每次都是需要临时计算得到的
# 但是bmi听起来更像是一个数据属性,而非功能
@property
def bmi(self):
return self.weight / (self.height ** 2)
obj1 = People(‘egon‘, 90, 1.50)
# print(obj1.bmi())
obj1.height = 1.60
# print(obj1.bmi())
print(obj1.bmi)
# 输出:
35.15624999999999
egon
# 案例2:
class People:
def __init__(self, name):
self.__name = name
# @property
def get_name(self):
return self.__name
def set_name(self, val):
if type(val) is not str:
print(‘必须传入str类型‘)
return
self.__name = val
def del_name(self):
print(‘不让删除‘)
# def self.__name
name11 = property(get_name, set_name, del_name)
obj1 = People(‘egon‘)
# print(obj1.get_name())
print(obj1.get_name())
obj1.set_name(‘xxq‘)
print(obj1.get_name())
obj1.del_name()
# 输出:
egon
xxq
不让删除
egon
# 案例三:
class People:
def __init__(self, name):
self.__name = name
@property
def name(self): # obj1.name
return self.__name
@name.setter
def name(self, val): # obj1.name=‘EGON‘
if type(val) is not str:
print(‘必须传入str类型‘)
return
self.__name = val
@name.deleter
def name(self): # del obj1.name
print(‘不让删除‘)
# del self.__name
obj1 = People(‘egon‘)
# 人正常的思维逻辑
print(obj1.name) #
# obj1.name=18
# del obj1.name
# 输出:
egon
继承
class Parent1(object):
x = 1111
class Parent2(object):
pass
class Sub1(Parent1): # 单继承
pass
class Sub2(Parent1,Parent2): # 多继承
pass
print(Sub1.__bases__) # (<class ‘__main__.Parent1‘>,)
print(Sub2.__bases__) # (<class ‘__main__.Parent1‘>, <class ‘__main__.Parent2‘>)
print(Sub1.x) # 1111
print(Parent1.__bases__) # (<class ‘object‘>,)
print(Parent2.__bases__) # (<class ‘object‘>,)
# 优点:子类可以同时遗传多个父类的属性,最大限度地重用代码
# 缺点:
# 1、违背人的思维习惯:继承表达的是一种什么"是"什么的关系
# 2、代码可读性会变差
# 3、不建议使用多继承,有可能会引发可恶的菱形问题,扩展性变差,
# 如果真的涉及到一个子类不可避免地要重用多个父类的属性,应该使用Mixins
class Student:
school=‘OLDBOY‘
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def choose_course(self):
print(‘学生%s 正在选课‘ % self.name)
class Teacher:
school=‘OLDBOY‘
def __init__(self, name, age, sex, salary, level):
self.name = name
self.age = age
self.sex = sex
self.salary = salary
self.level = level
def score(self):
print(‘老师 %s 正在给学生打分‘ % self.name)
class OldboyPeople:
school = ‘OldBoy‘
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
class Student(OldboyPeople):
def choose_course(self):
print(‘学生%s 正在选课‘ % self.name)
stu_obj = Student(‘lili‘, 18, ‘female‘)
# print(stu_obj.__dict__) # {‘name‘: ‘lili‘, ‘age‘: 18, ‘sex‘: ‘female‘}
# print(stu_obj.school) # OldBoy
# stu_obj.choose_course() # 学生lili 正在选课
class Teacher(OldboyPeople):
# 老师的空对象,‘egon‘,18,‘male‘,3000,10
def __init__(self, name, age, sex, salary, level):
# 指名道姓地跟父类OldboyPeople去要__init__
OldboyPeople.__init__(self, name, age, sex)
self.salary = salary
self.level = level
def score(self):
print(‘老师 %s 正在给学生打分‘ % self.name)
tea_obj = Teacher(‘egon‘, 18, ‘male‘, 3000, 10)
# print(tea_obj.__dict__) # {‘name‘: ‘egon‘, ‘age‘: 18, ‘sex‘: ‘male‘, ‘salary‘: 3000, ‘level‘: 10}
# print(tea_obj.school) # OldBoy
tea_obj.score() # 老师 egon 正在给学生打分
class Foo:
def f1(self):
print(‘Foo.f1‘)
def f2(self):
print(‘Foo.f2‘)
self.f1() # obj.f1()
class Bar(Foo):
def f1(self):
print(‘Bar.f1‘)
obj = Bar()
obj.f2()
# 预料的结果
# Foo.f2
# Foo.f1
# 实际的结果
# Foo.f2
# Bar.f1
class Foo:
def f1(self):
print(‘Foo.f1‘)
def f2(self):
print(‘Foo.f2‘)
Foo.f1(self) # 调用当前类中的f1
class Bar(Foo):
def f1(self):
print(‘Bar.f1‘)
obj = Bar()
obj.f2()
# 输出:
# Foo.f2
# Foo.f1
class Foo:
def __f1(self): # _Foo__f1
print(‘Foo.f1‘)
def f2(self):
print(‘Foo.f2‘)
self.__f1() # self._Foo__f1,# 调用当前类中的f1
class Bar(Foo):
def __f1(self): # _Bar__f1
print(‘Bar.f1‘)
obj = Bar()
obj.f2()
# Foo.f2
# Foo.f1
大多数面向对象语言都不支持多继承,而在Python中,一个子类是可以同时继承多个父类的,这固然可以带来一个子类可以对多个不同父类加以重用的好处,但也有可能引发著名的 Diamond problem菱形问题(或称钻石问题,有时候也被称为“死亡钻石”),菱形其实就是对下面这种继承结构的形象比喻。
class A(object):
def test(self):
print(‘from A‘)
pass
class B(A):
def test(self):
print(‘from B‘)
pass
class C(A):
def test(self):
print(‘from C‘)
pass
class D(C, B):
# def test(self):
# print(‘from D‘)
pass
# print(D.mro()) # 类D以及类D的对象访问属性都是参照该类的mro列表
# 输出:[<class ‘__main__.D‘>, <class ‘__main__.C‘>, <class ‘__main__.B‘>, <class ‘__main__.A‘>, <class ‘object‘>]
obj = D()
obj.test() # from C
print(D.test) # <function C.test at 0x039D84A8>
print(C.mro()) # 类C以及类C的对象访问属性都是参照该类的mro列表
# 输出:[<class ‘__main__.C‘>, <class ‘__main__.A‘>, <class ‘object‘>]
c = C()
c.test() # from C
python会在MRO列表上从左到右开始查找基类,直到找到第一个匹配这个属性的类为止。 而这个MRO列表的构造是通过一个C3线性化算法来实现的。我们不去深究这个算法的数学原理,它实际上就是合并所有父类的MRO列表并遵循如下三条准则:
1.子类会先于父类被检查
2.多个父类会根据它们在列表中的顺序被检查
3.如果对下一个类存在两个合法的选择,选择第一个父类
class E:
# def test(self):
# print(‘from E‘)
pass
class F:
def test(self):
print(‘from F‘)
class B(E):
# def test(self):
# print(‘from B‘)
pass
class C(F):
# def test(self):
# print(‘from C‘)
pass
class D:
def test(self):
print(‘from D‘)
class A(B, C, D):
# def test(self):
# print(‘from A‘)
pass
# 新式类
# print(A.mro()) # A->B->E->C->F->D->object
obj = A()
obj.test() # 结果为:from F
class G: # 在python2中,未继承object的类及其子类,都是经典类
# def test(self):
# print(‘from G‘)
pass
class E(G):
# def test(self):
# print(‘from E‘)
pass
class F(G):
def test(self):
print(‘from F‘)
class B(E):
# def test(self):
# print(‘from B‘)
pass
class C(F):
def test(self):
print(‘from C‘)
class D(G):
def test(self):
print(‘from D‘)
class A(B, C, D):
# def test(self):
# print(‘from A‘)
pass
# 新式类
# print(A.mro()) # A->B->E->C->F->D->G->object
# 经典类:A->B->E->G->C->F->D
obj = A()
obj.test() #
标签:VID fun dict 绑定 而在 val test font 多个
原文地址:https://www.cnblogs.com/2722127842qq-123/p/12677703.html