python面向对象1

1.什么是面向对象

⾯向对象就是将编程当成是⼀个事物,对外界来说,事物是直接使⽤的,不⽤去管他内部的情况。⽽编程就是设置事物能够做什么事

面向对象的思想简化了过程,在使用时可以更加方便直观

2.类和对象

在⾯向对象编程过程中,有两个重要组成部分:类 和 对象
类和对象的关系:⽤类去创建⼀个对象

2.1理解什么是类和对象

2.1.1什么是类

**类是对⼀系列具有相同特征和⾏为的事物的统称,是⼀个抽象的概念,不是真实存在的事物。*

2.1.2什么是对象

对象是类创建出来的真实存在的事物

注意:开发中,先有类,再有对象(例如在动物这个类里面有许多的)

2.2面向对象的实现方法

2.2.1定义类

语法:
class 类名(): 
		代码 ......

示例:

class wahser():
    def wash(self):
        print('洗衣服')

2.2.2定义对象(实例)

语法:对象名=类名()
示例:

class Wahser():
    def wash(self):
        print('洗衣服')



haier=Wahser()#创建对象haier(没给广告费)
print(haier)
haier.wash()

运行结果:
在这里插入图片描述

2.2.3self(调用该函数的对象)

示例:

class Wahser():
    def wash(self):
        print(self)



print(Wahser())#<__main__.Wahser object at 0x00000230D8521E20>
haier1=Wahser()#第一个对象

haier1.wash()#<__main__.Wahser object at 0x00000230D8521E20>

在这里插入图片描述
此时的print(self)和print(Washer)的结果是一样的
注意:打印对象和self得到的结果是⼀致的,都是当前对象的内存中存储地址

3.添加和获取对象属性

3.1 类外⾯添加对象属性

1)语法:对象名.属性名 = 值
2)示例:

haier1.width=1000
haier1.height=20

3.2 类外⾯获取对象属性

示例:

class Wahser():
    def wash(self):
        print(self)




haier1=Wahser()#第一个对象
haier1.width=1000
haier1.height=20

print(f'洗衣机的宽度是{haier1.width},高度是{haier1.height}')

运行结果:
在这里插入图片描述

3.3 类⾥⾯获取对象属性

示例:

class Wahser():
    def wash(self):
        print(f'haire1的宽度是{haier1.width},haier1的高度是{haier1.height}')#此时是在类里面获取对象属性
        print('上面的是函数内的执行')




haier1=Wahser()#第一个对象
haier1.width=1000
haier1.height=20
haier1.wash()

print(f'洗衣机的宽度是{haier1.width},高度是{haier1.height}')

在这里插入图片描述

4.魔法方法

在Python中,xx()的函数叫做魔法⽅法,指的是具有特殊功能的函数

4.1__init__()

示例:

class Washer():
    def __init__(self) -> None:
        self.height=1000
        self.width=10
    def wash(self):
        print(f'宽度是:{self.width},高度是{self.height}')


haier1=Washer()
print(haier1.height)
print(haier1.width)
haier1.wash()

运行结果:
在这里插入图片描述

注意事项:
init()⽅法,在创建⼀个对象时默认被调⽤,不需要⼿动调⽤
init(self)中的self参数,不需要开发者传递,python解释器会⾃动把当前的对象引⽤传递过去

4.2 带参数的__init__()

应用场景:⼀个类可以创建多个对象,如何对不同的对象设置不同的初始化属性?——传参数

class Washer():
    # 定义初始化功能的函数
    def __init__(self,height,width) -> None:#此时的初始化函数,带参数height和width
        self.height=height
        self.width=width
    def wash(self):
        print(f'宽度是:{self.width},高度是{self.height}')


haier1=Washer(100,20)#传入参数100和20
print(haier1.height)
print(haier1.width)
haier1.wash()

运行结果:
在这里插入图片描述

4.3 str()

当使⽤print输出对象的时候,默认打印对象的内存地址。如果类定义了__str__⽅法,那么就会打印从在这个⽅法中 return 的数据

示例:

class Washer():
    # 定义初始化功能的带参数的函数
    def __init__(self,height,width) -> None:#此时的初始化函数,带参数height和width
        self.height=height
        self.width=width
    def __str__(self) -> str:
        return '这是haier洗衣机'
    def wash(self):
        print(f'宽度是:{self.width},高度是{self.height}')


haier1=Washer(10,29)
print(haier1)

运行结果:
在这里插入图片描述

4.4__del__()(当执行del时会调用的代码

当删除对象时,python解释器也会默认调⽤__del__()⽅法

示例:

class Washer():
    # 定义初始化功能的带参数的函数
    def __init__(self,height,width) -> None:#此时的初始化函数,带参数height和width
        self.height=height
        self.width=width
    def __str__(self) -> str:
        return '这是haier洗衣机'
    def wash(self):
        print(f'宽度是:{self.width},高度是{self.height}')
    #删除对象执行的代码
    def __del__(self):
        print('当删除对象时执行的代码')


haier=Washer(10,200)
del haier

在这里插入图片描述

5.面向对象实例

1)实例1

class Sweetpotato():
    def __init__(self):
        """初始化实例对象的属性"""
        self.cooktime=0
        self.cook_static='生的'
        self.add=[]

    def cook_time(self,time):
        """制作的时间,以及随着时间变化食物的状态"""
        self.cooktime+=time
        if self.cooktime<=3:
            self.cook_static='生的'
        elif 3<self.cooktime<=6:
            self.cook_static='半生不熟'
        elif 6<self.cooktime<=9:
            self.cook_static='熟了'
        else:
            self.cook_static='烤焦了'

    def add_food(self,food):
        """添加食物"""
        self.add.append(food)

    def __str__(self):
        """定义str属性,返回此时sweetpotato的制作状态print(对象名)"""
        return f'此时的sweetpotato的制作时间是{self.cooktime},烤制的状态是{self.cook_static}。烤制添加的食物是{self.add}'

sweetpotato1=Sweetpotato()
print(sweetpotato1)
sweetpotato1.cook_time(5)#添加烤制时间
sweetpotato1.add_food('lajiao')#添加食物
print(sweetpotato1)
#注意事项:变量名和函数名不要使用时搞错了

运行结果:
在这里插入图片描述
2)实例2

class Furniture():
    #定义一个家具类
    def __init__(self,name,area):
        self.name=name
        self.area=area



class Home():
    def __init__(self,address,area):
        self.address=address
        self.area=area
        self.free_area=area
        self.furniture=[]

    def __str__(self):
        return f'房屋的地址是{self.address},房屋的面积是{self.area},剩余面积是{self.free_area},家具分别为{self.furniture}'


    def add_furniture(self,item):
        if self.free_area>=item.area:
            self.free_area-=item.area
            self.furniture.append(item.name)
        else:
            print('空间不足')
bed=Furniture('双人床',10)

home1=Home('北京',1000)
print(home1)
home1.add_furniture(bed)
print(home1)
sofa=Furniture('沙发',6)
home1.add_furniture(sofa)
print(home1)
playgrond=Furniture('操场',2000)
home1.add_furniture(playgrond)
print(home1)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值