#coding:utf-8
#self/super
#多继承
#经典类和新式类
#__slots__
#动态绑定
#属性包装
#枚举类
#__class__
#type--->创建类
#__str__
#多态---父类对象,子类对象,父类行为被子类复写---->表现不同的行为(函数逻辑)
#鸭子形态
#self/super
# class Person():
# def __init__(self,name,age):
# self.name=name
# self.age=age
#
# class Boy(Person):
# def __init__(self,name,age,sex):
# super(Boy,self).__init__(name,age)
# self.sex=sex
# p1=Person('ben',23) #self-->p1--->p1.name,p1.age
# p2=Person('cici',24)
#super(在子类里面-->super()--->父类对象)
#多继承
# class A():
# name='ben'
# age=12
# id=1
#
# class B():
# name='cici'
# age=18
# id=2
# sex='男'
#
# class C(A,B):#C类继承了A类,页继承了B类
# pass
# c1=C() #创建子类对象
# print(c1.name) #按继承顺序
# #输出 ben
# print(c1.sex)
# #输出 男
# class A():
# def func(self):
# print('a_func')
#
# class B():
# def func(self):
# print('b_func')
#
# class C(A,B):
# pass
# class D(A,B):
# def func(self):
# print('d_func')
# class E(C,D):
# pass
#
# e=E()
# e.func()
#pyrhon3.6 输出 d_func --->新式类
#Python2.7 输出 a_func ---->经典类
#经典类和新式类-->多继承---->集成的顺序有差别
#3.新式类---->广度优先
#2.x经典类----->深度优先
# class A():
# def func(self):
# print('a_func')
# class B(A):
# def func(self):
# print('b_func')
# class C(A):
# def func(self):
# print('c_func')
# class D(B,C):
# def func(self):
# print('d_func')
#
# d=D()
#d.func()
#输出 d_func
#__slots__
#实例对象--->动态绑定属性(静态语言难实现)
class Person():
pass #没有意义,没有属性,没有方法
#动态绑定属性
p1=Person() #当前实例为p1对象
p1.name='ben'
print(p1.name)
#动态绑定方法
def func(self,name):
self.name=name
print(self.name)
p1.func=func
p1.func(p1,'cici')
#动态绑定方法其他方式
def set_name(self,name):
self.name=name
from types import MethodType
p1.set_name=MethodType(set_name,p1) #这个函数来实现动态方法
p1.set_name('lsy')
print(p1.name)
#3.x版本新特性 __slots__属性
# class Person():
# __slots__=('name','age') #元组范围内
# p1=Person()
# p1.name='ben'
# p1.age=22
# print(p1.name,p1.age)
#属性包装
# class Person():
# age=1
#
# p=Person()
# p.age=9 #重新赋值
# print(p.age)
# class Person():
# age=1
# def get_age(self):
# return self.age
# def set_age(self,age):
# if not isinstance(age,int): #第一个参数是否是第二个参数类型
# raise ValueError('年龄必须为整数')
# if age<0 or age>100:
# raise ValueError('年龄必须在1--100')
# self.age=age
#
# p=Person()
# p.set_age(199)
# print(p.age)
class Person():
@property #装饰器--->
def age(self):
return self.age
@age.setter #设置函数--->属性的赋值(设置属性0
def age(self,age):
if not isinstance(age,int): #第一个参数是否是第二个参数类型
raise ValueError('年龄必须为整数')
if age<0 or age>100:
raise ValueError('年龄必须在1--100')
self.age=age
@age.deleter #删除属性
def age(self):
del self.age
p=Person() #创建对象
p.age=70
print(p.age) #获取属性,@property的age函数
del p.age
#枚举类型
#常量--->所有字幕全大写
# from enum import Enum,unique
# @unique
# class Num(Enum):
# ONE=1
# TWO=2
# Five=5
#
# print(Num.ONE.value)
#__class__属性
num=99
print(num.__class__)
#输出 <type 'int'>
print(num.__class__.__class__) #class type (元类) --->用来制造类的类对象
#输出 <type 'type'>
#type
def work(self):
print('正在工作')
def play(self):
print('正在玩')
Boy=type('Boy',(),{'is_girl':'false','name':'ben','work':work,'play':play})
print(type(Boy))
#输出 <type 'type'>
boy=Boy()
print(type(boy))
#输出 <class '__main__.Boy'>
print(boy.is_girl,boy.name)
#输出 ('false', 'ben')
boy.work()
#输出正在工作
#模型方法
#str__,__call__,__init__ , __new__ , __del__,__