面向对象
自定义对象
class Student:
place = "中国"
def __init__(self, name, age):
self.name = name
self.age = age
def fun1(self):
print("实例方法:fun1")
@staticmethod
def fun2():
print("静态方法:fun2")
@classmethod
def fun3(cls):
print("类方法:fun3")
student = Student("小明", 18)
print(student)
print(id(student))
print(type(student))
print(Student)
print(id(Student))
print(type(Student))
对象的属性和方法
class Student:
place = "中国"
def __init__(self, name, age):
self.name = name
self.age = age
def fun1(self):
print("实例方法:fun1")
@staticmethod
def fun2():
print("静态方法:fun2")
@classmethod
def fun3(cls):
print("类方法:fun3")
student = Student("小明", 18)
print(student.name)
print(student.age)
print(student.place)
print(Student.place)
student.fun1()
Student.fun1(student)
student.fun2()
Student.fun2()
student.fun3()
Student.fun3()
动态绑定属性和方法
class Student:
place = "中国"
def __init__(self, name, age):
self.name = name
self.age = age
def fun1(self):
print("实例方法:fun1")
@staticmethod
def fun2():
print("静态方法:fun2")
@classmethod
def fun3(cls):
print("类方法:fun3")
student1 = Student("小明", 18)
student2 = Student("小红", 18)
student1.phone = "Apple"
student2.watch = "XiaoTianCai"
print(student1.name, student1.phone)
print(student2.name, student2.watch)
def info():
print("show info")
student1.info = info
student1.info()
对象的封装
class Student:
def __init__(self, name, age):
self.name = name
self.__age = age
def show(self):
print(self.name, self.__age)
student1 = Student("小明", 18)
student1.show()
print(student1.name)
print(dir(student1))
print(student1._Student__age)
对象的继承
继承的使用
class Person(object):
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, number):
super().__init__(name, age)
self.number = number
class Teacher(Person):
def __init__(self, name, age, level):
super().__init__(name, age)
self.level = level
student = Student("小明", 18, "001")
student.info()
teacher = Teacher("语文", 28, "高级")
teacher.info()
多继承
class AFun(object):
pass
class BFun(object):
pass
class CFun(AFun, BFun):
pass
cfun = CFun()
print(type(cfun))
print(isinstance(cfun, AFun))
print(isinstance(cfun, BFun))
print(isinstance(cfun, CFun))
print(issubclass(CFun, AFun))
print(issubclass(CFun, BFun))
print(issubclass(CFun, CFun))
对象的多态
"""
静态语言的多态:
必须继承;必须方法重写;父类引用指向子类对象
动态语言的多态:“鸭子类型”
当一只鸟走路像鸭子,游泳像鸭子,那么这只鸟就可以称为鸭子。
动态语言的多态,不关心对象的类型,只关心对象的行为。
"""
class Animal(object):
def eat(self):
print("Animal eat")
class Dog(Animal):
def eat(self):
print("Dog eat")
class Cat(Animal):
def eat(self):
print("Cat eat")
class Person(object):
def eat(self):
print("Person eat")
# fun函数中,调用对象的eat方法
def fun(animal):
animal.eat()
# 多态的具体实现;只要传入的参数带有 eat 方法即可
fun(Person())
fun(Animal())
fun(Dog())
fun(Cat())
对象的特殊属性
class A:
pass
class B:
pass
class C(A, B):
place = "中国"
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def show(cls):
return "show:place={0}".format(cls.place)
subC = C("Java", 18)
print(dir(subC))
print(dir(C))
"""
实例对象的所有实例属性,{'name': 'Java', 'age': 18}
"""
print(subC.__dict__)
"""
类对象的所有类属性和方法
{'__module__': '__main__', 'place': '中国', '__init__': <function C.__init__ at 0x000002940BB4D430>,
'show': <classmethod object at 0x000002940BB56FD0>, '__doc__': None}
"""
print(C.__dict__)
"""
实例对象的类型
<class '__main__.C'>
"""
print(subC.__class__)
"""
类对象的类型
<class 'type'>
"""
print(C.__class__)
"""
C类的父类元组
(<class '__main__.A'>, <class '__main__.B'>)
"""
print(C.__bases__)
"""
C类的层次结构
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
"""
print(C.__mro__)
"""
A类的子类列表
[<class '__main__.C'>]
"""
print(A.__subclasses__())
对象的特殊方法
add 方法和 len 方法
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("Java")
stu2 = Student("Python")
new_stu = stu1 + stu2
print(new_stu)
new_stu = stu2.__add__(stu2)
print(new_stu)
print(len(stu1))
print(stu2.__len__())
new 方法和 init 方法
class Person(object):
def __init__(self, name, age):
print("__init__ 开始调用,实例对象的 id:{0}".format(id(self)))
self.name = name
self.age = age
def __new__(cls, *args, **kwargs):
print("__new__ 开始调用,类对象的 id:{0}".format(id(cls)))
newCls = super().__new__(cls)
print("__new__ 调用完成,创建的对象 id:{0}".format(id(newCls)))
return newCls
print("object类对象:", id(object))
print("Person类对象:", id(Person))
p1 = Person("Java", 18)
print("Person实例对象:", id(p1))
"""
执行log:
object类对象: 140719148129792
Person类对象: 1775829927024
__new__ 开始调用,类对象的 id:1775829927024
__new__ 调用完成,创建的对象 id:1775835839072
__init__ 开始调用,实例对象的 id:1775835839072
Person实例对象: 1775835839072
"""
对象的浅拷贝与深拷贝
import copy
class CPU:
pass
class Disk:
pass
class Computer:
def __init__(self, cpu, disk):
self.cpu = cpu
self.disk = disk
cpu1 = CPU()
cpu2 = cpu1
print(cpu1, id(cpu1))
print(cpu2, id(cpu2))
myCpu = CPU()
myDisk = Disk()
myComputer = Computer(myCpu, myDisk)
computer1 = copy.copy(myComputer)
print(myComputer, myComputer.cpu, myComputer.disk)
print(computer1, computer1.cpu, computer1.disk)
myCpu = CPU()
myDisk = Disk()
myComputer = Computer(myCpu, myDisk)
computer2 = copy.deepcopy(myComputer)
print(myComputer, myComputer.cpu, myComputer.disk)
print(computer2, computer2.cpu, computer2.disk)