面向对象的三大特征:封装、继承、多态

# -*- coding: utf-8 -*-
# @Time    : 2021/10/3 15:11
# @Author  : 李新宇
# @FileName: demo1.py
# @Software: PyCharm

class Car:
    def __init__(self,brand):
        self.brand=brand
    def  start(self):
        print('汽车已启动')

car=Car('宝马x5')
car.start()
print(car.brand)
# -*- coding: utf-8 -*-
# @Time    : 2021/10/3 15:18
# @Author  : 李新宇
# @FileName: demo2.py
# @Software: PyCharm

class Student:
    def __init__(self,name,age):
        self.name=name
        self.__age=age  #年龄不希望在类的外部被使用,所以加了两个_
    def show(self):
        print(self.name,self.__age)


stu=Student('张三',20)
stu.show()

#在类的外部使用name与age
print(stu.name)
#print(stu.__age)#AttributeError: 'Student' object has no attribute '__age'

print(dir(stu))
print(stu._Student__age) #在类的外部可以通过 _Student__age 进行访问
# -*- coding: utf-8 -*-
# @Time    : 2021/10/3 15:25
# @Author  : 李新宇
# @FileName: demo3.py
# @Software: PyCharm

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

class Teacher(Person):
    def __init__(self,name,age,teachofyer):
        super().__init__(name,age)
        self.teachofyer=teachofyer

stu=Student('zhangsan',20,'1001')
teacher=Teacher('lisi',40,10)

stu.info()
teacher.info()







# -*- coding: utf-8 -*-
# @Time    : 2021/10/3 15:37
# @Author  : 李新宇
# @FileName: demo4.py
# @Software: PyCharm

#多继承,不能用super()函数

class A(object):
    pass

class B(object):
    pass

class C(A,B):
    pass

# -*- coding: utf-8 -*-
# @Time    : 2021/10/3 15:25
# @Author  : 李新宇
# @FileName: demo3.py
# @Software: PyCharm

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): #方法重写
        super().info()
        print(self.stu_no)

class Teacher(Person):
    def __init__(self,name,age,teachofyer):
        super().__init__(name,age)
        self.teachofyer=teachofyer

    def info(self):
        super().info()
        print('教龄',self.teachofyer)


stu=Student('张三',20,'1001')
teacher=Teacher('李四',40,10)

stu.info()
print('---------')
teacher.info()







# -*- coding: utf-8 -*-
# @Time    : 2021/10/3 15:47
# @Author  : 李新宇
# @FileName: demo6.py
# @Software: PyCharm

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return '我的名字是{0},今年{1}岁了'.format(self.name, self.age)

stu=Student('张三', 20)
print(dir(stu))
print(stu) #默认会调用__str__()这样的方法
print(type(stu))
# -*- coding: utf-8 -*-
# @Time    : 2021/10/3 16:00
# @Author  : 李新宇
# @FileName: demo7.py
# @Software: PyCharm


class Animal(object):
    def eat(self):
        print('动物会吃')
class Dog(Animal):
    def eat(self):
        print('狗吃骨头')
class Cat(Animal):
    def eat(self):
        print('猫吃鱼')
class Person(object):
    def eat(self):
        print('人吃五谷杂粮')


#定义一个函数
def fun(obj):
    obj.eat()

#开始调用函数
fun(Cat())
fun(Dog())
fun(Person())
print('-------------')
fun(Person())



# -*- coding: utf-8 -*-
# @Time    : 2021/10/3 16:07
# @Author  : 李新宇
# @FileName: demo8.py
# @Software: PyCharm

print(dir(object)) #dir()查找对象的属性和方法

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)
print(x.__dict__) #绑定实例对象的属性字典
print(C.__dict__)
print('---------')
print(x.__class__) #<class '__main__.C'> 输出了对象所属的类
print(C.__bases__) #C类父类型的元素
print(C.__base__)
print(C.mro()) #类的层次结构
print(A.__subclasses__()) #查看子类列表
# -*- coding: utf-8 -*-
# @Time    : 2021/10/3 16:18
# @Author  : 李新宇
# @FileName: demo9.py
# @Software: PyCharm

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

    def __len__(self):
        return len(self.name)
stu1=Student('张三')
stu2=Student('李四')

s=stu1+stu2
print(s)
s=stu1.__add__(stu2)
print(s)
print('--------------')

lst=[11,2,2,333,44]
print(len(lst))
print(lst.__len__())

print(len(stu1))


# -*- coding: utf-8 -*-
# @Time    : 2021/10/3 16:34
# @Author  : 李新宇
# @FileName: demo10.py
# @Software: PyCharm


class Person(object):

    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('Person的类对象的id为:{0}'.format(id(Person)))

#创建Person类的实例对象
p1=Person('张三',20)
print('p1这个Person类的实例对象的id:{0}'.format(id(p1)))
# -*- coding: utf-8 -*-
# @Time    : 2021/10/3 17:37
# @Author  : 李新宇
# @FileName: demo11.py
# @Software: PyCharm


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,id(cpu1))
print(cpu2,id(cpu2))

#2、类的浅拷贝
print('-------------')
disk=Disk() #创建一个硬盘类对象
computer=Computer(cpu1,disk) #创建一个计算机类对象

#浅拷贝
import copy
print(disk)
computer2=copy.copy(computer)
print(computer,computer.cpu,computer.disk)
print(computer2,computer2.cpu,computer2.disk)

print('------------------------------')
#深拷贝
computer3=copy.deepcopy(computer)
print(computer,computer.cpu,computer.disk)
print(computer3,computer3.cpu,computer3.disk)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙黄橘绿时_Eden

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值