Article Directory
1. Three characteristics of object-oriented
Encapsulation: Improve the security of the program.
Wrap data (attributes) and behaviors (methods) into class objects, and manipulate properties inside methods
. There are no special modifiers for private properties in Python. If the properties do not want to be accessed in the class object, use it before "__"
Inheritance: Improve code reusability
Polymorphism: Improve program scalability and maintainability
2. Package
#封装
class Car:
def __init__(self,brand,price):
self.brand=brand
self.__price=price #价格不希望在类的外部被使用
def start(self):
print('汽车已启动')
def show(self):
print(self.brand,self.__price)
car=Car('XX',10)
car.start() #汽车已启动
car.show() # XX 10
# print(stu.__age)报错不能用
print(car._Car__price) #10 ,可以用这种方式访问
3. Inheritance
#继承
'''
Python支持多继承
定义子类,必须在其构造函数中调用父类的构造函数
如果一个类没有继承任何类,则默认继承object
'''
class Person:
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(Person): #学生继承了人
def __init__(self,name,age,stu_no):
super().__init__(name,age)
self.stu_no=stu_no
class Teacher(Person):
def __init__(self,name,age,tea_no):
super().__init__(name,age)
self.tea_no=tea_no
stu=Student('张三',20,'1001')
teacher=Teacher('李四',30,10)
stu.info() #张三 20
teacher.info() #李四 30
#多继承
class A:
pass
class B:
pass
class C(A,B):
pass
4. Method rewriting
'''
方法重写
'''
class Person:
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(Person): #学生继承了人
def __init__(self,name,age,stu_no):
super().__init__(name,age)
self.stu_no=stu_no
def info(self):
super().info()
print(self.stu_no)
class Teacher(Person):
def __init__(self,name,age,tea_no):
super().__init__(name,age)
self.tea_no=tea_no
def info(self):
super().info()
print(self.tea_no)
stu=Student('张三',20,'1001')
teacher=Teacher('李四',30,10)
stu.info()
teacher.info()
5. Object class
class Student:
def __init__(self,name,age):
self.name=name
self.age=age
def __str__(self):
return '我的名字是{0},今年{1}岁'.format(self.name,self.age)
'''
__str()__方法经常用于print()方法,帮助我们查看对象的信息
所以经常对__str()__重写
'''
stu=Student('张三',20)
print(stu) #我的名字是张三,今年20岁
6. Polymorphism
#多态
class Animal:
def eat(self):
print('动物会吃')
class Dog(Animal):
def eat(self):
print('狗吃肉')
class Cat(Animal):
def eat(self):
print('猫吃鱼')
class Person:
def eat(self):
print('人少都吃')
def fun(obj):
obj.eat()
fun(Cat())
fun(Dog())
fun(Animal())
fun(Person())
'''
动态语言的多态崇尚“鸭子类型”,当看到一只鸟走起来像鸭子,游泳像鸭子,那么这只鸟
就可以被称为鸭子。不用关心对象是什么类型,到底是不是鸭子,只关心对象的行为
'''
7. Special attributes and methods
'''
__dict__ 获得类对象或实例对象所绑定的所有属性和方法的字典
__len__() 重写__len__()方法,让内置函数len()的参数可以是自定义类型
__add__() 重写__add__()方法,使自定义对象具有“+”功能
__new__() 用于创建对象
__init__() 对创建的对象进行初始化
'''
class Student:
def __init__(self,name):
self.name=name
def __add__(self, other):
return self.name+other.name
def __len__(self):
return len(self.name)
stu1=Student('第一')
stu2=Student('第二')
s=stu1+stu2
print(s) #第一第二
s=stu1.__add__(stu2)
print(s) #第一第二
lst=[1,2,3,4]
print(len(lst)) #4
print(lst.__len__()) #4
print(len(stu1)) #2
class Person:
def __new__(cls, *args, **kwargs):
print('__new___被调用执行了,cls的id值为{0}'.format(id(cls)))
obj=super().__new__(cls)
print('创建的对象id为:{0}'.format(id(obj)))
return obj
def __init__(self,name,age):
print('__init__被调用,cls的id值为{0}'.format(id(self)))
self.name=name
self.age=age
print('object这个类对象的id为:{0}'.format(id(object)))
print('Person这个类对象的id为:{0}'.format(id(Person)))
#创建Person类的实例对象
p1=Person('张三',20)
print('p1这个Person类的实例对象的id:{0}'.format(id(p1)))
'''
object这个类对象的id为:140703982435152
Person这个类对象的id为:1863372716000
__new___被调用执行了,cls的id值为1863372716000
创建的对象id为:1863404112048
__init__被调用,cls的id值为1863404112048
p1这个Person类的实例对象的id:1863404112048
'''
class A:
pass
class B:
pass
class C(A,B):
def __init__(self,name,age):
self.name=name
self.age=age
x=C('XCSSs',10)
print(x.__dict__) # {'name': 'XCSSs', 'age': 10} 实例对象的属性字典
print(x.__class__) #<class '__main__.C'> 输出了对象所属的类
print(C.__bases__)# (<class '__main__.A'>, <class '__main__.B'>).继承的父类
print(C.__mro__) #类的层次结构
print(A.__subclasses__()) #子类的列表
8. Shallow copy and deep copy
class Cpu:
pass
class Disk:
pass
class Computer:
def __init__(self,cpu,disk):
self.cpu=cpu
self.disk=disk
#1.变量的赋值
cpu1=Cpu()
cpu2=cpu1
print(cpu1) #<__main__.Cpu object at 0x000001F22FDA6430>
print(cpu2)#<__main__.Cpu object at 0x000001F22FDA6430>
#2.类的浅拷贝
disk=Disk()
computer=Computer(cpu1,disk)
#浅拷贝 ,相当于拷贝了一个文件的快捷方式,本质没变
import copy
computer2=copy.copy(computer)
print(computer,computer.cpu,computer.disk)
print(computer2,computer2.cpu,computer2.disk)
#深拷贝
computer3=copy.deepcopy(computer)
print(computer,computer.cpu,computer.disk)
print(computer3,computer3.cpu,computer3.disk)