创建类:
class 类名(父类):
属性
方法 (如果在方法里直接定义属性,那么只有在改方法执行后,该属性才能被访问)
创建对象:
- 对象名=类名(参数)
- 对象.属性
- 对象.方法
对象的内置方法:
new:真正的类构造方法,用于产生实例化对象。 ```python单例模式
class Student: instance = None def new__(cls, args, *kwargs):if not cls.__instance:cls.__instance = object.__new__(cls)return cls.__instance
stu1 = Student() stu2 = Student() print(id(stu1), id(stu2)) # 两者输出相同 print(stu1 is stu2) # True
__init__:方法是初始化方法,负责对实例化对象进行属性值初始化<br />__str__:在使用时调用,与__repr__功能相似,只不过不是在控制台输出时调用<br />__repr__:被控制台输出时默认调用,方便查看和调试```pythondef __str__(self):return .....__repr__=__str__
del:一般用于需要声明在对象被删除前需要处理的资源回收操作
call:将一个类实例变成一个可调用对象
class Person(object):def __init__(self, name, gender):self.name = nameself.gender = genderdef __call__(self, friend):print('My name is %s...' % self.name)print('My friend is %s...' % friend)p = Person('Bob', 'male')p('Tim')'''My name is Bob...My friend is Tim...'''
私有属性与方法
私有属性与方法
_xxx “单下划线 “ 开始的成员变量叫做保护变量,意思是只有类对象(即类实例)和子类对象自己能访问到这些变量
__xxx 类中的私有变量/方法名 ,” 双下划线 “ 开始的是私有成员,意思是只有类对象自己能访问,连子类对象也不能访问到这个数据。
- 私有的属性,不能通过对象直接访问,但是可以通过方法访问
- 私有的方法,不能通过对象直接访问
- 私有的属性、方法,不会被子类继承,也不能被访问
- 一般情况下,私有的属性、方法都是不对外公布的,往往用来做内部的事情,起到安全的作用
继承:
```python class Animal: def init(self,color):
def eat(self):self.color=color
def show(self):print("吃东西")
print(self.color)
class Cat(Animal): def init(self,color):
# 父子类是由__init__实现的,如果父类__init__()无参数,这句话可以不写,默认就有Animal.__init__(self,color)pass
dog=Animal(“黑色”) dog.show() c=Cat(“白色”) c.show()
<a name="sCsQs"></a>### 多继承:```pythonclass Father:money=100def __init__(self,x):self.x=xclass Money:money=10face="好看"def __init__(self,y):self.y=yclass Son(Money,Father): #这里的Money,Father要有顺序,这里去找一个变量,先会从Money类找,def __init__(self,x,y): #当Money类没有时,才会去Father找Money.__init__(self,y)Father.__init__(self,x)s=Son(3,4)print(s.money)print(s.x) #3print(s.y) #4
多态:
class Aniaml:def eat(self):print("啥都吃")class Dog(Aniaml):def eat(self):print("吃骨头")class Cat(Aniaml):def eat(self):print("吃鱼")class Persion:def feed(self,Animal):#Animal表示我要喂哪个动物,这个例子是传入Animal的子类对象Animal.eat()c = Cat()d = Dog()p = Persion()p.feed(c)p.feed(d)
重写:
'''重写:当父类的属性与方法不满足子类的需求时,可以重写要求:子类与父类方法名一样即可,一旦重写,子类对象看不到父类的方法'''class Animal :color="黑色"def eat(self):print("啥都吃")def play(self):print("乱蹦乱跳")class Cat(Animal):color="白色"def eat(self,name):print(name+"猫吃鱼")c=Cat()c.eat("Tom")c.play()print(c.color)
动态添加属性与方法:
import typesclass Person:def __init__(self,name,age):self.name=nameself.age=agep1=Person("张三",20)p1.sex="男" #对象给类添加属性,只对当前对象有效Person.sex="女" #类本身添加属性,对类的对象都起作用#添加方法def run(self):print("蹦蹦跳跳")p1.personrun=types.MethodType(run,p1) #对象给类添加方法,只对当前对象有效Person.personsay=say #类本身添加方法,对类的对象都起作用
super函数:
#super用于字节调用父类的函数class A:def add(self, x):y = x+1return yclass B(A):def add(self, x):result=super().add(x)return result*2b = B()b.add(2) # 6
类的方法:
class Root:def __init__(self): #构造方法,只在创建对象时自动调用passdef show(self): #普通方法,只有对象能够调用pass@classmethoddef show(cls): #声明类方法,cls可以传实参可以不传实参,但是要写上pass #可以用对象和类名调用@staticmethod #静态方法,不用传参,可以用对象和类名调用def show():pass
@property
在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数
为了限制score的范围,可以通过一个set_score()方法来设置成绩,再通过一个get_score()来获取成绩,这样,在set_score()方法里,就可以检查参数:
class Student(object):def get_score(self):return self._scoredef set_score(self, value):if not isinstance(value, int):raise ValueError('score must be an integer!')if value < 0 or value > 100:raise ValueError('score must between 0 ~ 100!')self._score = values = Student()s.set_score(60) # ok!s.get_score()60
这样明显太麻烦,因此@property就出来了
class Student(object):@propertydef score(self):return self._score@score.setterdef score(self, value):if not isinstance(value, int):raise ValueError('score must be an integer!')if value < 0 or value > 100:raise ValueError('score must between 0 ~ 100!')self._score = values=Students.score=50 #实际转化为s.set_score(50)s.score>>>50 #实际转化为s.get_score(50)
@abstractmethod
抽象方法表示基类的一个方法,没有实现,所以基类不能实例化,子类实现了该抽象方法才能被实例化。
Python的abc提供了@abstractmethod装饰器实现抽象方法
见下图的代码,基类Foo的fun方法被@abstractmethod装饰了,所以Foo不能被实例化;子类SubFoo没有实现基类的fun方法也不能被实例化;子类SubB实现了基类的抽象方法fun所以能实例化。
from abc import ABC, abstractmethodclass Foo(ABC):@abstractmethoddef fun(self):'''please Implemente in subclass'''class FooA(Foo):def run(self):print('run in FooA')class FooB(Foo):def fun(self):print('fun in FooB')a = FooA()#TypeError: Can't instantiate abstract class FooA with abstract methods funb = FooB()b.fun()
