面向对象学习笔记1

本文介绍了面向对象编程的概念,包括类的定义和实例化。通过类模板创建多个对象,强调了封装的重要性,使代码更易于维护和扩展。文中还讨论了类方法中的self参数作用,并提供了一个车辆类的实例,演示如何初始化、运行、加油和查看里程数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

类:class
好处:减少重复代码的编写、可以更好地进行程序的维护、扩展和升级

class P:pass
P:类,定义个名字为P的类。
在内存中,会有一个存储P类所有的内容。

p1 = P()#实例化,从类P,生成一个具体的对象p1
p2 = P()#实例化,从类P,生成一个具体的对象p2
p3 = P()#实例化,从类P,生成一个具体的对象p3

P 是模板(模具),实例是通过这个模板(模具),添加了不同的初始化参数生产出来的具体产品。可以有多个,且每个具体产品可以有不同参数设定不同的造型。

class Person():
    def __init__(self,name,sex,height,weight,bachelor):
        self.name = name
        self.sex = sex
        self.height = height
        self.weight = weight
        self.bachelor = True


wulaoshi = Person("wulaoshi","M","180","200",True)
lilaoshi = Person("lilaoshi","M","180","200",False)

print(wulaoshi.sex)
print(lilaoshi.sex)

在这里插入图片描述
对于封装:
不用关心程序的内部实现,我们只需要调用函数或者类方法即可。

函数和类方法都可以实现封装:
函数只有操作某些数据的过程和算法,但是自己不保存数据。

类实例:
不但操作某些数据的过程和算法,且保存相关的数据。

类:
管理一组数据(可以是0个、1个或多个),以及提供操作这组数据的相关方法,数据和方法组成了类。

class Person():
    def __init__(self,name,sex,height,weight,bachelor):
        self.name = name
        self.sex = sex
        self.height = height
        self.weight = weight
        self.bachelor = True


wulaoshi = Person("wulaoshi","M","180","200",True)
lilaoshi = Person("lilaoshi","M","180","200",False)

print(wulaoshi.sex)
print(lilaoshi.sex)

wulaoshi.weight = str(int(wulaoshi.weight)+1)
print(wulaoshi.weight)

在这里插入图片描述
类定义的所有内容,在内存中仅有一份(类变量和类方法)。
类实例(多个),每个类实例在内存中都有自己的空间。
类的方法里面为啥要有个self呢?
wulaoshi.print_name()-------->在内存中找到类定义所在的内存位置,找到类里面定义的方法print_name(),self------>wulaoshi所在的内存地址,使用wulaoshi实例中的name值进行打印。

class Person():
    def __init__(self,name,sex,height,weight,bachelor):
        self.name = name
        self.sex = sex
        self.height = height
        self.weight = weight
        self.bachelor = True
    
    def print_name(self):
        print(self.name)

wulaoshi = Person("wulaoshi","M","180","200",True)
lilaoshi = Person("lilaoshi","M","180","200",False)

print(wulaoshi.sex)
print(lilaoshi.sex)

wulaoshi.weight = str(int(wulaoshi.weight)+1)
print(wulaoshi.weight)
wulaoshi.print_name()

在这里插入图片描述
写一个车的实例,实现:
初始化一个车里有多少油
调用run方法,实现车的运行,没油的时候提示需要加油了。跑一次里程数加100公里,油量减20升。
调用add_gas方法可以实现加油,让车继续运行。
check_distance方法查看车跑的里程数。

class CAR(object):
    CAR_NUM = 0 #类变量
    def __init__(self,name,length,height,color,gas_num,gas_consume_num,distance):
        self.name = name #实例变量
        self.length = length
        self.height = height
        self.color = color
        self.gas_num = gas_num  #初始化汽油的数量
        self.gas_consume_num = gas_consume_num  #百公里油耗
        self.distance = distance  #车跑的里程数

    def run(self):
        if self.gas_num >= self.gas_consume_num:
            self.distance+=100
            self.gas_num -= 20
        else:
            print("没油了,请速加!")

    def add_gas(self,n):
        self.gas_num+=n

    def check_distance(self):
        return self.distance

    def check_gas(self):
        return self.gas_num

c = CAR("吴老师的豪车","500cm","200cm","red",100,20,0)
for i in range(5):  #跑5次
    c.run()
    print(c.check_gas())

print(c.check_distance())
c.add_gas(100)
print(c.check_gas())

在这里插入图片描述

class CAR(object):
    CAR_NUM = 0 #类变量
    def __init__(self,name,length,height,color,gas_num,gas_consume_num,distance):
        self.name = name #实例变量
        self.length = length
        self.height = height
        self.color = color
        self.gas_num = gas_num  #初始化汽油的数量
        self.gas_consume_num = gas_consume_num  #百公里油耗
        self.distance = distance  #车跑的里程数

    def run(self):
        if self.gas_num >= self.gas_consume_num:
            self.distance+=100
            self.gas_num -= 20
        else:
            print("没油了,请速加!")

    def add_gas(self,n):
        self.gas_num+=n

    def check_distance(self):
        return self.distance

    def check_gas(self):
        return self.gas_num

c = CAR("吴老师的豪车","500cm","200cm","red",100,20,0)
"""for i in range(5):  #跑5次
    c.run()
    print(c.check_gas())

print(c.check_distance())
c.add_gas(100)
print(c.check_gas())
"""

l = CAR("李老师的豪车","800cm","300cm","blue",200,20,0)
c.run()
print(c.check_gas())
l.run()
print(l.check_gas())

在这里插入图片描述
c和l都在不同的内存空间里面,所以互相都不影响,都没关系。

class CAR(object):
    CAR_DISTANCE_NUM = 0 #类变量
    def __init__(self,name,length,height,color,gas_num,gas_consume_num,distance):
        self.name = name #实例变量
        self.length = length
        self.height = height
        self.color = color
        self.gas_num = gas_num  #初始化汽油的数量
        self.gas_consume_num = gas_consume_num  #百公里油耗
        self.distance = distance  #车跑的里程数

    def run(self):
        if self.gas_num >= self.gas_consume_num:
            self.distance+=100
            self.gas_num -= 20
            CAR.CAR_DISTANCE_NUM+=20
        else:
            print("没油了,请速加!")

    def add_gas(self,n):
        self.gas_num+=n

    def check_distance(self):
        return self.distance

    def check_gas(self):
        return self.gas_num

    @classmethod
    def get_total_distance(cls):
        return CAR.CAR_DISTANCE_NUM

c = CAR("吴老师的豪车","500cm","200cm","red",100,20,0)
"""
for i in range(5):  #跑5次
    c.run()
    print(c.check_gas())

print(c.check_distance())
c.add_gas(100)
print(c.check_gas())
"""

l = CAR("李老师的豪车","800cm","300cm","blue",200,20,0)
c.run()
print(c.check_gas())
l.run()
print(l.check_gas())
print(CAR.get_total_distance())

在这里插入图片描述

class CAR(object):
    CAR_DISTANCE_NUM = 0 #类变量
    def __init__(self,name,length,height,color,gas_num,gas_consume_num,distance):
        self.name = name #实例变量
        self.length = length
        self.height = height
        self.color = color
        self.gas_num = gas_num  #初始化汽油的数量
        self.gas_consume_num = gas_consume_num  #百公里油耗
        self.distance = distance  #车跑的里程数

    def run(self):
        if self.gas_num >= self.gas_consume_num:
            self.distance+=100
            self.gas_num -= 20
            CAR.CAR_DISTANCE_NUM+=100
        else:
            print("没油了,请速加!")

    def add_gas(self,n):
        self.gas_num+=n

    def check_distance(self):
        return self.distance

    def check_gas(self):
        return self.gas_num

    @classmethod
    def get_total_distance(cls):
        return CAR.CAR_DISTANCE_NUM  #不能使用self的实例变量

c = CAR("吴老师的豪车","500cm","200cm","red",100,20,0)
"""
for i in range(5):  #跑5次
    c.run()
    print(c.check_gas())

print(c.check_distance())
c.add_gas(100)
print(c.check_gas())
"""

l = CAR("李老师的豪车","800cm","300cm","blue",200,20,0)
c.run()
print(c.check_gas())
l.run()
print(l.check_gas())
print(CAR.get_total_distance())
l.run()
print(CAR.get_total_distance())

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值