demo1 封装
# 学校:上海海洋大学工程学院
# 时间:2021/11/1 21:10
class Car:
def __init__(self, brand):
self.brand = brand
def start(self):
print('汽车已启动..')
car=Car('宝马X5')
car.start()
print(car.brand)
#不希望在外部被使用可以加两个_ , 例如: self.__age=age
demo2 继承
# 学校:上海海洋大学工程学院
# 时间:2021/11/1 21:17
class Person(object): #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,xuehao):
self.xuehao=xuehao
super().__init__(name,age) #super()是调用父类的方法
class Teacher(Person):
def __init__(self,name,age,teachofyear):
super().__init__(name,age)
self.teachofyear=teachofyear
stu1=Student('茆俊亚',23,'M210811448')
teacher1=Teacher('李四',34,10)
stu1.info()
teacher1.info()
demo3 重写
# 学校:上海海洋大学工程学院
# 时间:2021/11/1 21:17
class Person(object): #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,stu_no):
super().__init__(name,age)
self.stu_no=stu_no
def info(self): #重写info类方法(先调用父类的info,再重写子类的info)
super().info()
print('学号:',self.stu_no)
class Teacher(Person):
def __init__(self,name,age,teachofyear):
super().__init__(name,age)
self.teachofyear=teachofyear
def info(self): #重写info类方法(先调用父类的info,再重写子类的info)
super().info()
print('教龄:',self.teachofyear)
stu=Student('茆俊亚',23,'M210811448')
teacher=Teacher('李四',34,10)
stu.info()
print('-------------------')
teacher.info()
demo4
# 学校:上海海洋大学工程学院
# 时间:2021/11/3 21:26
class Student:
def __init__(self,name,age):
self.name=name
self.age=age
def __str__(self): #重写父类的方法
return'我的名字是{0},今年{1}岁了'.format(self.name,self.age)
stu1=Student('茆俊亚',23)
print(dir(stu1))
print(stu1) #原来输出的是对象的内存地址,一旦重写str类型后,默认会调用__str__方法,输出里面的内容
print(type(stu1))
# 类对象后面加括号代表着实例化
# java是静态语言,python是动态语言
demo5 类的特殊属性
# 学校:上海海洋大学工程学院
# 时间:2021/11/3 21:35
class A:
pass
class B:
pass
class C(A,B):
def __init__(self,name,age):
self.name=name
self.age=age
class D(A):
pass
#创建C类的对象
x=C('Jack',20) #x是C类型的一个实例对象
print(x.__dict__) #实例对象的属性字典
print(C.__dict__)
print('---------------------------------------------------------------------------------')
print(x.__class__) #<class '__main__.C'> 输出了对象所属的类
print(C.__bases__) #C类的父类的元组
print(C.__base__) #类的基类,和它考的最近的,例如C(A,B)就是A , C(B,A)就是B
print(C.__mro__) #类的层次结构
print(A.__subclasses__()) #子类的列表
demo6 类的特殊方法
# 学校:上海海洋大学工程学院
# 时间:2021/11/4 21:35
a=20
b=100
c=a+b
d=a.__add__(b)
print(c)
print(d)
class Student:
def __init__(self,name):
self.name=name
def __add__(self,other):
return self.name+other.name
stu1=Student('张三')
stu2=Student('李四')
s=stu1+stu2 #实现了两个对象的加法运算(因为在Student类中编写了__add__()特殊方法)
print(s)
s=stu1.__add__(stu2)
print(s)
demo7 类的特殊方法(2)
# 学校:上海海洋大学工程学院
# 时间:2021/11/5 10:28
class Person:
def __new__(cls,*args,**kwargs):
print('__new__被调用了,cls的id值为:{0}'.format(id(cls)))
obj=super().__new__(cls) #调用object的new方法
print('创建对象的id为:{0}'.format(id(obj)))
return obj
def __init__(self,name,age):
print('__init__被调用了,self的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)))
#总结:
# 1.obj 就是 self 也是 p1
# 2. new在前是创建对象,init在后是为对象的实例属性进行赋值,最后将创建的对象放到p1当中进行存储
demo8 类的浅拷贝与深拷贝
# 学校:上海海洋大学工程学院
# 时间:2021/11/9 21:22
class CPU:
pass
class Disk:
pass
class Computer:
def __init__(self,cpu,disk):
self.cpu=cpu
self.disk=disk
cpu1=CPU() #创建一个cpu类的对象
disk1=Disk() #创建一个硬盘类的对象
computer1=Computer(cpu1,disk1) #创建一个计算机类的对象
print('----------------------------------------------------------------------------------------------------------------')
'''----------类的浅拷贝(对象包含的子对象内容不拷贝,只拷贝源对象)-----------'''
import copy
computer2=copy.copy(computer1)
print(computer1,computer1.cpu,computer1.disk)
print(computer2,computer2.cpu,computer2.disk)
print('------------------------------------------------------------------------------------------------------------')
'''-----------类的深拷贝(源对象和对象包含的子对象内容都要重新拷贝一份)-----------'''
computer3=copy.deepcopy(computer1)
print(computer1,computer1.cpu,computer1.disk)
print(computer3,computer3.cpu,computer3.disk)