python入门之面向对象


一、类和对象

# 1.创建类
class Washer():
    def wash(self):
        print("用洗衣机来执行洗衣服功能")


# 2.创建对象 -- 格式:定义对象名 = 类名()--用等号连接
haier = Washer()
print(haier)

# 3.实例方法 -- def wash(self): -- 使用格式:对象名.实例方法名()
haier.wash()

二、实例方法

class Washer():
    def wash(self):
        print("洗衣服")
        print(self)


haier = Washer()
geli = Washer()
print(f'第一个对象地址{haier}')
print(f'第二个对象地址{geli}')

haier.wash()
geli.wash()
"""
这里的self指代的就是haier,即self = 调用wash方法的对象
"""

三、类的属性

1.例1

class Washer():
    def wash(self):
        print("洗衣服")


# 在类的外面通过对象来定义类的属性 并 获取(打印)类的属性
haier = Washer()
haier.width = 100
haier.height = 150
print(f'第一个属性{haier.width}')
print(f'第一个属性{haier.height}')

2.例2

class Washer():
    def wash(self):
        print("洗衣服")

    # 在类的内部获取(打印)属性 -- 定义实例方法
    def print_info(self):
        print(f'第一个属性{self.wdith}')
        print(f'第二个属性{self.height}')


# 在类的外面通过对象来定义类的属性
haier = Washer()
haier.wdith = 100
haier.height = 150
# 调用方法
haier.print_info()

四、魔法方法

1.魔法方法_init_

class Washer():
    def __init__(self):  # 魔法方法(类似于java中的构造方法),在类的内部定义类的属性
        self.width = 100
        self.height = 150

    def print_info(self):
        print(f"属性一{self.width}")
        print(f"属性二{self.height}")


haier = Washer()
print(haier.width)
print(haier.height)

haier.print_info()

2.魔法方法_init_(带参)

class Washer():
    def __init__(self, width, height):
        self.width = width
        self.heigth = height

    def print_info(self):
        print(f'属性一{self.width},属性二{self.heigth}')


# 创建多个对象,并且属性值不同
haier01 = Washer(width=100, height=150)
haier02 = Washer(300, 450)

haier01.print_info()
haier02.print_info()

3.魔法方法_str_

class Washer():
    def __init__(self):
        self.width = 100

    def __str__(self):  # 当print(对象)时不再打印地址,而是打印return 后面的"这是海尔洗衣机说明书"
        return "这是海尔洗衣机说明书"


haier = Washer()
print(haier)

4.魔法方法_del_

class Washer():
    def __init__(self):
        self.width = 100
        self.height = 150

    def __del__(self):  # 当删除对象时,python解释器会默认调用__del__()方法
        print(f'{self}对象已删除')


haier = Washer()

del haier
# 但是由于程序执行完会自动释放内存,所以编译器内部执行删除对象的操作,那么__del__()方法就会触发

5.魔法方法_dict_

class A(object):
    a = 0

    def __init__(self):
        self.b = 1


if __name__ == '__main__':
    a = A()
    print(A.__dict__)  # 返回类内部所有属性和⽅法对应的字典
    print(a.__dict__)  # 返回实例属性和值组成的字典

五、实例Ⅰ

"""********** 烤地瓜 **********"""
class SweetPotato():
    def __init__(self):
        # 被烤的时间
        self.cook_time = 0
        # 地瓜的状态
        self.cook_static = '生的'
        # 调料列表
        self.condiments = []

    def cook(self, time):
        self.cook_time += time
        if 0 <= self.cook_time < 3:
            self.cook_static = '生的'
        elif 3 <= self.cook_time < 5:
            self.cook_static = '半生不熟'
        elif 5 <= self.cook_time < 8:
            self.cook_static = '熟了'
        elif 8 <= self.cook_time:
            self.cook_static = '烤糊了'

    def add_condiments(self, condiment):
        self.condiments.append(condiment)

    def __str__(self):
        return f'地瓜烤了{self.cook_time}分钟,状态是{self.cook_static},调料有{self.condiments}'


digua01 = SweetPotato()
print(digua01)

digua01.cook(2)
digua01.add_condiments('辣椒面')
print(digua01)

digua01.cook(2)
digua01.add_condiments('老干妈')
print(digua01)

digua01.cook(2)
print(digua01)

六、实例Ⅱ

"""********** 搬家具 ***********"""
class Furniture():
    def __init__(self, name, area):
        self.name = name
        self.area = area


bed = Furniture('水床', 100)
sofa = Furniture('沙发', 80)
bingxiag = Furniture('冰箱', 300)


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

    def add_furniture(self, item):
        if self.free_area >= item.area:
            self.furniture.append(item.name)
            self.free_area -= item.area
        else:
            print('面积容量不足')

    def __str__(self):
        return f'房子的位置在{self.address},房子的总面积{self.area},剩余面积{self.free_area},家具有{self.furniture}'


jia01 = Home('北京', 250)
print(jia01)

jia01.add_furniture(bed)
print(jia01)

jia01.add_furniture(sofa)
print(jia01)

jia01.add_furniture(bingxiag)
print(jia01)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

二十四桥_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值