一、面向对象编程(oop)
OOP把对象当成一个程序的基本单元,一个对象包含了数据和操作数据的函数。
二、基本使用
- 定义类
class ClassName(object):
pass
- 创建实例
实例名 = ClassName()
- 使用成员属性或成员方法
ClassName.属性
ClassName.方法名()
三、成员修饰符
共有属性:正常命名
私有属性:以两个下划线开头命名
四、面向对象三大特性
1. 封装
封装,顾名思义就是将内容封装到某个地方,以后再去调用被封装在某处的内容。 对于面向对象的封装来说,其实就是使用构造方法将内容封装到 对象 中,然后通过对象直接或者self间接获取被封装的内容。
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print("吃了{}".format(food))
a = Person('jack', 10, '男')
a.eat()
2. 继承
子类继承父类
class Animal:
def eat(self):
print "%s 吃 " %self.name
class Cat(Animal):
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name):
self.name = name
c1 = Cat('猫')
c1.eat()
d1 = Dog('狗')
d1.eat()
3. 多态
Python不支持多态,也不用支持多态,python是一种多态语言
class Animal(object):
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
pass
class Cat(Animal):
def __init__(self, name, age):
super().__init__(name, age)
def speak(self):
print("喵喵喵")
class Dog(Animal):
def __init__(self, name, age):
super().__init__(name, age)
def speak(self):
print("汪汪汪")
def speak(animal):
animal.speak()
if __name__ == '__main__':
dog = Dog("dog", 12)
cat = Cat("cat", 10)
speak(dog)
speak(cat)
五、各种方法
1. 静态方法
装饰器使用@staticmethod
如果没有写装饰器,调用时智能为类名.方法名(),不能使用对象名.方法名()
调用:
- 类名.方法名()
- 对象名.方法名()
特点:不能使用类属性和成员属性
class Person(object):
def __init__(self, name):
self.name = name
@staticmethod # 把eat方法变为静态方法
def eat(x):
print("%s is eating" % x)
d = Person("xiaoming")
d.eat("jack")
2. 类方法
装饰器使用@classmethod
调用:类名.方法名()
特点:只能使用类属性
class Person(object):
count = 0
def __new__(cls, *args, **kwargs):
cls.count += 1
@classmethod # 把eat方法变为类方法
def show_count(cls):
print("创建了{}个对象".format(cls.count))
d = Person()
Person.show_count()
3. 属性方法
装饰器
- 被 @属性名.setter 装饰的方法是设置属性值的方法。
- 被 @property 装饰的方法是获取属性值的方法,被装饰方法的名字会被用做 属性名。
- 被 @属性名.deleter 装饰的方法是删除属性值的方法
调用:对象名.属性名
特点:把一个方法变成一个静态属性,属性就不用加小括号那样的去调用了
class Person(object):
def __init__(self, name):
self.name = name
self.__age = 0
@property
def age(self):
return self.__age
@age.setter
def age(self, age):
if age > 0 and age <= 100:
self.__age = age
@age.deleter
def age(self):
print("age不能删除")
p = Person("Tom")
p.age = 18
print(p.age)
del p.age
4. 魔法方法
函数名 | 作用 |
---|---|
__init__(self[, ...]) | 构造方法,初始化类的时候被调用 |
__new__(cls[, ...]) | 1. 实例化对象时第一个被调用的方法 2. 其参数直接传递给__init__方法处理 3. 我们一般不会重写该方法 |
__del__(self) | 析构方法,当实例化对象被彻底销毁时被调用 (实例化对象的所有指针都被销毁时被调用) |
__call__(self[, args...]) | 允许一个类的实例像函数一样被调用:x(a, b) 调用 x.call(a, b) |
__str__(self) | 类的字符串表示 |
比较操作符
函数名 | 作用 |
---|---|
__lt__(self, other) | 定义小于号的行为:x < y 调用 x.__lt__(y) |
__le__(self, other) | 定义小于等于号的行为:x <= y 调用 x.__le__(y) |
__eq__(self, other) | 定义等于号的行为:x == y 调用 x.__eq__(y) |
__ne__(self, other) | 定义不等号的行为:x != y 调用 x.__ne__(y) |
__gt__(self, other) | 定义大于号的行为:x > y 调用 x.__gt__(y) |
__ge__(self, other) | 定义大于等于号的行为:x >= y 调用 x.__ge__(y) |
算数运算符
函数名 | 作用 |
---|---|
__add__(self, other) | 定义加法的行为:+ |
__sub__(self, other) | 定义减法的行为:- |
__mul__(self, other) | 定义乘法的行为:* |
__truediv__(self, other) | 定义真除法的行为:/ |
__floordiv__(self, other) | 定义整数除法的行为:// |
__mod__(self, other) | 定义取模算法的行为:% |
__pow__(self, other[, modulo]) | 定义当被 power() 调用或 ** 运算时的行为 |