目录
一、编程思想
二、类与对象
三、类的创建
四、对象的创建
五、类属性、类方法、静态方法
六、动态绑定属性和方法
七、知识点总结
八、面向对象的三大特征
1.封装
2.继承
3.多态
九、方法重写
十、object类
十一、特殊方法和特殊属性
1.dict/len/add
2.new / init
十二、类的赋值与浅拷贝
十三、知识点总结
一、编程思想
二、类与对象

python中一切皆对象
三、类的创建
类的名称由一个或多个单词组成,每个单词的首字母大写,其余小写
类是对象,开辟内存空间
这里self当然可以换为其他单词 ,但是必须存在这样一个单词,python默认就是self
在类里定义的函数称为实例方法,在类之外定义的函数称为函数。
四、对象的创建
class Student:# Student称为类的名称native_pace='吉林'#直接写在类里面的变量,称为类属性def __init__(self,name,age):#name,age是实例属性self.name=nameself.age=age#实例方法def eat(self):print('学生在吃饭')#静态方法,使用@staticmethod@staticmethoddef method():print('静态方法中不允许写self')#类方法,@classmethod@classmethoddef cm(cls):print('类方法中传cls')
#在类之外定义的称为函数,在类之内定义的称为方法stu1=Student('张三',20)
stu1.eat() #对象名.方法名
stu1.cm()
stu1.method()
print(stu1.name,stu1.age)
# 学生在吃饭
# 类方法中传cls
# 静态方法中不允许写self
# 张三 20Student.eat(stu1) #和上面的调用方法功能一样
# 学生在吃饭
五、类属性、类方法、静态方法
print(Student.native_pace)#吉林
stu1=Student('张三',20)
stu2=Student('李四',30)
print(stu1.native_pace)
print(stu2.native_pace)
# 吉林
# 吉林
Student.native_pace='天津'
print(stu1.native_pace)
print(stu2.native_pace)
# 天津
# 天津Student.cm()
Student.method()
# 类方法中传cls
# 静态方法中不允许写self
六、动态绑定属性和方法
class Student:def __init__(self,name,age):self.name=nameself.age=agedef eat(self):print(self.name+'在吃饭')stu1=Student('张三',30)
stu2=Student('李四',40)stu2.gender='男' #动态绑定stu2属性
print(stu1.name,stu1.age)
print(stu2.name,stu2.age,stu2.gender)
# 张三 30
# 李四 40 男def show():print('定义在类之外,函数')
stu1.show=show #动态绑定stu1的方法
stu1.show()
七、知识点总结
八、面向对象的三大特征
1.封装
class Student:def __init__(self, name, age):self.name = nameself.__age = agedef show(self): #可让外部对象通过调用成员方法来进行访问print(self.name, self.__age)stu = Student('张三', 20)
stu.show()
# 在类的外部使用name和age
print(stu.name)
# print(stu.__age) print(stu.__age)AttributeError: 'Student' object has no attribute 'name'
print(dir(stu))
print(stu._Student__age) # 在类的外部强制访问,虽然说私有属性外部没法访问,但是也不是不能访问,只是自觉不使用进行访问
# 20
2.继承
class Person(object):def __init__(self,name,age):self.name=nameself.age=agedef info(self):print('姓名:{0},年龄:{1}'.format(self.name,self.age))
#定义子类
class Student(Person):def __init__(self,name,age,score):super().__init__(name,age)self.score=score
#测试
stu=Student('Jack',20,'1001')
stu.info()
# 姓名:Jack,年龄:20
3.多态
九、方法重写
java就是静态语言,python是动态语言,静态语言实现多态必须明确继承关系,而动态语言只关心你是否具有这个方法
class Animal(object):def eat(self):print('动物吃')class Dog(Animal):def eat(self): #重写父类Animal方法print('狗吃')class Cat(Animal):def eat(self): #重写父类Animal方法print('猫吃')class Person:def eat(self): #重写父类object方法print('人吃')def fun(a):#定义一个函数,来调用eat方法,因此只要类中有eat方法,就可以被调用a.eat()fun(Dog())
fun(Cat())
fun(Animal())
fun(Person())
# 狗吃
# 猫吃
# 人吃
class Person(object):def __init__(self, name, age):self.name = nameself.age = agedef 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# 会发现父类中也有info方法,但是子类Student在调用时只会输出name,age,# 无法输出子类的stu_no,因此想着继承父类的属性,但是也要有子类的属性,于是重写info方法def info(self):super().info() #这个是为了继承父类的name,age的属性,不写就继承不了print(self.stu_no)class Teacher(Person):def __init__(self, name, age, teachofyear):super().__init__(name, age)self.teachofyear = teachofyearstu = Student('张三', 20, '10010')
stu.info()
# 张三 20
# 10010
十、object类
class Student: #没写继承,默认继承object类pass
stu=Student()
print(dir(stu))
class Student:def __init__(self, name, age):self.name = nameself.age = agedef __str__(self):#相当于重写了__str__方法,因为继承的object中也有这个方法return '我的名字是{0},今年{1}岁了'.format(self.name, self.age)stu = Student('张三', 20)
print(dir(stu))
# ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__',
# '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__',
# '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
# '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
print(stu) #默认会调用__str__这样的方法,
# <__main__.Student object at 0x0000016E05C38400>
# 我的名字是张三,今年20岁了 #默认调用__str__()这样的方法,重写了
十一、特殊方法和特殊属性
1.dict/len/add
a = 20
b = 100
c = a + b
d = a.__add__(b)print(c) # 120
print(d) # 120 #从此可以看出,ab相加的原理就是调用了本身的__add__方法,如果本身没有add方法,d也不会有结果,如下面的例子class Student:def __init__(self, name):self.name = namedef __add__(self, other):return self.name + other.namedef __len__(self):return len(self.name)stu1 = Student('张三')
stu2 = Student('李四')# print(stu1+stu2)TypeError: unsupported operand type(s) for +: 'Student' and 'Student'
print(stu1 + stu2) # 张三李四
# 实现了两个对象的加法运算,在类中编写了add特殊的方法,没有add方法,这两个就不能相加
s=stu1.__add__(stu2) #当然这样写也必须是在类中有add方法,如果把Student类中的add方法去除,一样不对
print(s)list=[1,2,3,4]
print(len(list))
print(list.__len__())#可以发现列表自带len的内置方法print(len(stu1))
# TypeError: object of type 'Student' has no len() 因为类中没写len方法,所以报错
# 2 在类中编写了len特殊的方法,return len(self.name)
2.new / init
class Person(object):def __new__(cls, *args, **kwargs): #创建对象print('__new__被调用执行了,cls的id值为{0}'.format(id(cls))) #4864obj=super().__new__(cls)print('创建的对象的id为:{0}'.format(id(obj))) #7264return objdef __init__(self,name,age): #对创建的对象初始化self.name=nameself.age=ageprint(' _init__被调用了,self的id为:{0}'.format(id(self))) #7264print('object id:{0}'.format(id(object))) #4048
print('Person id:{0}'.format(id(Person))) #4864
p1=Person('三',20)
print('p1 id:{0}'.format(id(p1))) #7264# object id:140709592514048
# Person id:2701662344864
# __new__被调用执行了,cls的id值为2701662344864
# 创建的对象的id为:2701663637264
# _init__被调用了,self的id为:2701663637264
# p1 id:2701663637264
十二、类的赋值与浅拷贝
#类对象的赋值操作,形成两个变量,实际上还是指向同一个对象
cpu1=CPU()
cpu2=cpu1
print(cpu1)
print(cpu2)
# <__main__.CPU object at 0x0000021A25626FD0>
# <__main__.CPU object at 0x0000021A25626FD0>disk=Disk()
computer=Computer(cpu1,disk)
import copy
computer2=copy.copy(computer) #浅拷贝,对象包含的子对象内容不拷贝
computer3=copy.deepcopy(computer) #深拷贝,递归拷贝对象中包含的子对象
print(computer,computer.cpu,computer.disk)
print(computer2,computer2.cpu,computer2.disk)
print(computer3,computer3.cpu,computer3.disk)
# <__main__.Computer object at 0x000002281D258F70> <__main__.CPU object at 0x000002281D258FD0> <__main__.Disk object at 0x000002281D258FA0>
# <__main__.Computer object at 0x000002281D258820> <__main__.CPU object at 0x000002281D258FD0> <__main__.Disk object at 0x000002281D258FA0>
# <__main__.Computer object at 0x000002281D258730> <__main__.CPU object at 0x000002281D258430> <__main__.Disk object at 0x000002281D258460>