类与继承


主要是对函数的学习:

# 编写人:刘钰琢
# 编写日期:2021/1/30 16:17
class Car:
    def __init__(self,brand):
        self.brand=brand
    def start(self):
        print('汽车已启动')
car1=Car('宝马X5')
car1.start()
print(car1.brand)
class Student:
    def __init__(self,name,age):
        self.name=name
        self.__age=age#age不希望在类的外部去使用,所以加了两个_
    def show(self):
        print(self.name,self.__age)
student1=Student('张三',20)
student1.show()
#在类的外部使用name, age
print(student1.name)
#print(student1.__age)__age不能单独使用
print(dir(student1))
print(student1._Student__age)#在类的外部可以通过_Student__age进行访问
'''继承'''
print('这是继承部分:')
#继承的代码实现
class Persion(object):
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def info(self):
        print('姓名:{0},年龄:{1}'.format(self.name,self.age))
class Stu(Persion):
    def __init__(self,name,age,score):
        super().__init__(name,age)
        self.score=score
stu=Stu('王五',10,20)
stu.info()
class Tea(Persion):
    def __init__(self,name,age,teachofyear):
        super().__init__(name,age)
        self.teachofyear=teachofyear
tea=Tea('张麻子',20,30)
tea.info()
#多继承
class A(object):
    pass
class B:
    pass
class C(A,B):
    pass
#方法重写
print("方法重写")
class Persion(object):
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def info(self):
        print('姓名:{0},年龄:{1}'.format(self.name,self.age))
class Stu(Persion):
    def __init__(self,name,age,score):
        super().__init__(name,age)
        self.score=score
    def info(self):#重写方法info
        super().info()#这样会先执行父类当中的info
        print("Stu重写方法后:",self.score)#在执行子类当中的info
stu=Stu('王五',10,100)
stu.info()
class Tea(Persion):
    def __init__(self,name,age,teachofyear):
        super().__init__(name,age)
        self.teachofyear=teachofyear
    def info(self):
        super().info()
        print("Tea重写之后的","教龄:",self.teachofyear)
tea=Tea('张麻子',20,30)
tea.info()
class Liu:
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def __str__(self):
        return '我的名字是{0},今年{1}岁了'.format(self.name,self.age)
liu1=Liu('张三',20)
print(liu1)#默认会调用__str__()这样的方法
class Animal:
    def eat(self):
        print('动物要吃东西')
class Dog(Animal):
    def eat(self):
        print('狗吃肉')
class Cat(Animal):
    def eat(self):
        print('猫吃鱼')
class Persion:
    def eat(self):
        print('人吃五谷杂粮')
def fun(obj):
    obj.eat()
fun(Dog())
fun(Cat())
fun(Animal())
fun(Persion())
class A(object):
    pass
class B:
    pass
class C(A,B):
    def __init__(self,name,age):
        self.name=name
        self.age=age
#创建C类的对象
x=C('jack',20)
print(x.__dict__)#实例对象的属性字典
print(C.__dict__)
print(x.__class__)#输出对象所属的类
print(C.__bases__)#C类的父类类型的元素
print(C.__base__)#C类的第一个父类的元素(基类)
print(C.__mro__)#类的层次结构
print(A.__subclasses__())#子类的列表
a=20
b=100
c=a+b#两个整数类型的相加操作
d=a.__add__(b)
print(c)
print(d)
class Yu:
    def __init__(self,name):
        self.name=name
    def __add__(self, other):
        return self.name+other.name
    def __len__(self):
        return len(self.name)
yu1=Yu('张三')
yu2=Yu('李四')
s=yu1+yu2#实现了两个对象的加法运算(因为在Yu类当中 编写了__add__(self,other)的特殊算法
print(s)
s=yu1.__add__(yu2)
print(s)
lst=[11,22,3,4]
print(len(lst))
print(lst.__len__())
print('-----')
print(yu1.__len__())
#__new__()用于创建对象
class Persion:
    def __init__(self,name,age):
        self.name=name
        self.age=age
    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__被调用了self的id的值为{0}'.format(id(self)))
        self.name=name
        self.age=age
print('object的这个类的对象id为:{0}'.format(id(object)))
print('Persion这个对象的id为:{0}'.format(id(Persion)))
p1=Persion('张三',20)
print('p1这个Persion类的实例对象的id:{0}'.format(id(p1)))
#print(p1)
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(id(cpu1))
print(id(cpu2))
#(2)类的浅拷贝,只是原对象和拷贝对象会引用同一个引用对象,但是两者的内存地址是不相同的
print('-----------')
disk=Disk()
computer=Computer(cpu1,disk)
import copy
computer2=copy.copy(computer)
print(computer,computer.cpu,computer.disk)
print(computer2,computer2.cpu,computer2.disk)#cpu和disk类的内存地址是一样的,但是computer的内存地址是不一样的
#(3)深拷贝
print('-----------')
computer3=copy.deepcopy(computer)
print(computer,computer.cpu,computer.disk)
print(computer3,computer3.cpu,computer3.disk)#所有的computer,cpu,disk的内存地址都是不同的

这一部分已经一个多月没有碰了已经算是所以还是哟啊多看看这个
多多多多进行复习


文章作者: 毛豆不逗比
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 毛豆不逗比 !
  目录
{% include '_third-party/exturl.swig' %} {% include '_third-party/bookmark.swig' %} {% include '_third-party/copy-code.swig' %} + {% include '_custom/custom.swig' %}