CLASS类
一个类是对一类拥有相同属性的对象的抽象
类的特性
封装:
防止数据被随意修改,
使外部程序不需要关注对象内部的构造,只需要通过对象对外提供的接口进行直接访问即可
继承:
通过父类-子类的方式以最小代码量的方式实现,不通角色的共同点和不同点
多态:
一个接口,多种实现
实例
class Dog(object):
aaa = '22'
def __init__(self, name):
self.Name = name
def sayhi(self):
print("I am dog: %s" % self.Name)
def __del__(self):
print("deleted")
d1 = Dog("andy")
self就是d1本身
__init__: 构造函数
sayhi(): 方法, 动态属性
self.Name: 成员属性,只属于一个实例
self.__xxx: 私有属性
aaa: 公有属性
强制访问私有属性: d1._Dog__xxx
__del__(self),析构函数,可自己定义,系统也会默认自带
封装:
class A(object): def __init__(self, name): self.Name = name class B(object): def __init__(self, name): self.Name = name class C(object): def __init__(self, name): self.Name = name a1 = A('hinimix') b1 = B(a1) c1 = C(b1) print(c1.Name.Name.Name)
继承:
class Dog(object): def __init__(self, name, age, gender): self.Name = name self.Age = age self.Gender = gender self.__xx = 'default' def sayhi(self): print("I am a dog: %s" % self.Name) def __del__(self): print("deleted") class persion(Dog): def __init__(self, name, age, gender): # 先继承,再重构 Dog.__init__(self, name, age, gender) print(self.Name, self.Age) super(persion, self).sayhi() # 新式类用法 def sayhi(self): Dog.sayhi(self) print("I am a persion: %s" % self.Name) c = persion("andy", "45", "male")
#打印实例所有属性
print(c.__dict__)
# 新式类写法
class A(object):
super(curren_class, self).function()
# 经典类写法
class B:
ParentClass.__init__
c = persion("andy", "45", "male")
c.sayhi()
多态
# 多态 class animal(object): def __init__(self, name): self.name = name def talk(self): print("animal talk %s" % self.name) raise NotImplementedError("must abstract") class person(animal): def talk(self): print("person talk %s " % self.name) class dog(animal): def talk(self): print("dog talk %s " % self.name) # 通过这个而实现多态 def talk(x): x.talk() t1 = animal("sophie") t2 = person("annie") t3 = dog("audrey") talk(t3)
staticmethod/property/classmethod
静态方法
@staticmethod
只是名义上归类管理
类方法
@classmethod
只能访问类里的类变量,不能访问实例变量
属性方法
@property
把一个方法变成一个静态属性
class Dog(object): def __init__(self, name): self.name = name self.__food = None @staticmethod def haha(): print("hello world") @property def xxx(self): print(self.name, "fuck", self.__food) @xxx.setter def xxx(self, dog): print("set dog %s" % dog) self.__food = dog @xxx.deleter def xxx(self, dog): print("set dog %s" % dog) del self.__food d = Dog("andy") d.xxx = "aa"
class内置方法:
__doc__:是描述类的注释的
class Cat(object): '''descript object CAT''' def __init__(self, name): self.name = name c1 = Cat("sophie") print(c1.__doc__) descript object CAT
__module__:查看这个类是哪里调用的
__class__:输出类
__call__:能加括号调用
def __call__(self): print("666") c1()
__dict__:查看类或对象中的所有成员,字典形式
__str__: 输出时候打印的字符串
def __str__(self): return self.name + " is needed" print(c1)
__getitem__, __setitem__, __delitem__:把类的实例当成一个dict来处理
def __setitem__(self, key, value): print("__setitem__", key, value) self.data[key] = value def __getitem__(self, item): print("__getitem__", item) return self.data.get[item] def __delitem__(self, key): print("__delitem__", key)
__new__:实例化时候执行,实例化,然后调用__init__执行
def new(cls, *args, **kwargs): print("from new") return object.__new__(cls) __metaclass__: # 类的属性,表示该类由谁创建
新式类的写法:
def test1(self): print("from test1", self.name) def init(self, name, age): self.name = name self.age = age Foo = type('foo', (), {'test1': test1, '__init__': init}) f = Foo("a", "13") f.test1() print(f.age)