python面向对象操作1(速通版)(3)

简介: python面向对象操作1(速通版)(1)

2.创建对象

字符串、列表名等都是对象

python万物皆对象

定义一个类

class Person(object):
    def eat(self,food):
        print('一个人在吃',food)
    def sleep(self,t):
        print('一个人睡了',t,'小时')
tom = Person()
print(tom)
tom.eat('饭')
tom.sleep(10)

3.动态绑定属性

class Person(object):
    def eat(self,food):
        print(self.name,'一个人在吃',food)
    def sleep(self,t):
        print(self.name ,'睡了',t,'小时')
tom = Person()
print(tom)
tom.name = 'tom'
tom.eat('饭')
tom.sleep(10)

4.初始化方法中调用属性

class Lei(object):
    def __init__(self,name,age):
        print('init run ...', self)
        #绑定属性时,使用self.属性名 = 值
        self.name = name
        self.age = age
    def show(self):
        print(self.name,self.age)
rom = Lei('rom',10)
print(rom)
print(rom.name)
print(rom.age)
rom.show()
print("________________________________________________________________________________________________________________")
jack = Lei('jack',18)
print(jack)
print(jack.name)
print(jack.age)
jack.show()

5.__str__方法

5.1 方法 1

class Lei(object):
    def __init__(self,name,age,height):
        #绑定属性时,使用self.属性名 = 值
        self.name = name
        self.age = age
        self.height = height
    def __str__(self):
        print("to String run ....",self.name)
        print(self.name,self.age,self.height)
        #这个方法必须有个返回值,哪怕是个空串
        return "123"
rom = Lei('rom',10,'100cm')
print(rom)
print(rom.name)
print(rom.age)
print(rom.height)
print("________________________________________________________________________________________________________________")
jack = Lei('jack',18,'1800cm')
print(jack)
print(jack.name)
print(jack.age)
print(jack.height)

5.2 方法2

class Lei(object):
    def __init__(self,name,age,height):
        #绑定属性时,使用self.属性名 = 值
        self.name = name
        self.age = age
        self.height = height
    def __str__(self):
        st = self.name.ljust(6)+str(self.age).ljust(5)+self.height.ljust(10)
        #这个方法必须有个返回值,哪怕是个空串
        return st
rom = Lei('rom',10,'100cm')
print(rom)
print(rom.name)
print(rom.age)
print(rom.height)
print("________________________________________________________________________________________________________________")
jack = Lei('jack',18,'1800cm')
print(jack)
print(jack.name)
print(jack.age)
print(jack.height)

6.del方法

回收资源,在没有对象引用时,计数器为0,del方法被调用

class Lei(object):
    def __init__(self,name,age,height):
        #绑定属性时,使用self.属性名 = 值
        self.name = name
        self.age = age
        self.height = height
    def __str__(self):
        st = self.name.ljust(6)+str(self.age).ljust(5)+self.height.ljust(10)
        #这个方法必须有个返回值,哪怕是个空串
        return st
    def __del__(self):
        print("del 被调用")
rom = Lei('rom',10,'100cm')
del rom
print('over')

7.案例练习

7.1烤地瓜

class SweetPotato(object):
    #实现初始化方法,初始地瓜状态,和总烧烤时间
    def __init__(self):
        self.status = '生瓜蛋子'
        self.total_time = 0
    #实现一个烧烤方法
    #该方法有一个烧烤时间,这个时间会被累积到总时间上
    #判断总时间,来改变地瓜状态
    def cook(self,t):
        #累加时间
        self.total_time += t
        #判断 时间来改变地瓜状态
        if self.total_time >=1 and self.total_time < 3:
            self.status = "还是生的"
        elif self.total_time >=3 and self.total_time < 6:
            self.status = "半生不熟"
        elif self.total_time >=6 and self.total_time < 8:
            self.status = "烤好了"
        elif self.total_time >=8 and self.total_time < 10:
            self.status = "烤糊了"
        else:
            self.status = "成炭了"
        #显示地瓜对象
    def __str__(self):
        s = self.status + f'被烤了 {self.total_time}' + '分钟'
        return s
sp1 = SweetPotato()
print(sp1)
sp1.cook(2)
print(sp1)
sp1.cook(2)
print(sp1)
sp1.cook(2)
print(sp1)
sp1.cook(2)
print(sp1)
sp1.cook(2)
print(sp1)

7.2 人狗案例

class Dog(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def bark(self,n):
        for i in range(n):
            print("wowowo!")
class Person(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def add_pet(self,pet):
        self.pet = pet
    def listen_dog_bark(self,n):
        self.pet.bark(n)
tom = Person('tom',20)
tom.add_pet(Dog('旺财',5))
tom.listen_dog_bark(2)

7.3 存放家具

class Furniture(object):
    def __init__(self,name,area):
        self.name = name
        self.area = area
class House(object):
    def __init__(self,address,area):
        self.address = address
        self.area = area
        #只有百分之七十可以放家具
        self.free_area  = area * 0.7
        self.ocp = 0
        self.Fur = list()
    def add_funiture(self,F):
        if self.free_area - ( self.ocp + F.area) >= 0:
            self.ocp += F.area
            self.Fur.append(F.name)
        else:
            print("不好意思,该家具太大了,不能添加")
    def check_having_fur_and_space(self):
        if len(self.Fur) == 0:
            print("you do not have any FUR")
        else:
            print("fur: ",self.Fur)
            print("having space:",self.free_area-self.ocp)
home1 = House("beijing",100)
home1.check_having_fur_and_space()
print("____________________________________________________________________________")
home1.add_funiture(Furniture('电视',10))
home1.check_having_fur_and_space()
home1.add_funiture(Furniture('桌子',15))
home1.check_having_fur_and_space()
home1.add_funiture(Furniture('床',20))
home1.check_having_fur_and_space()
home1.add_funiture(Furniture('电脑',10))
home1.check_having_fur_and_space()
home1.add_funiture(Furniture('沙发',20))
home1.check_having_fur_and_space()
home1.add_funiture(Furniture('衣柜',15))
home1.check_having_fur_and_space()


结语

点个赞吧!!!求求求了!!!!


相关文章
|
7天前
|
Python
你真的会面向对象吗!解密Python“魔术方法”
你真的会面向对象吗!解密Python“魔术方法”
16 0
|
21天前
|
存储 监控 数据处理
💻Python高手必备!文件系统操作秘籍,让你的数据存取如臂使指
【7月更文挑战第29天】在数据驱动时代, Python以简洁语法、丰富库生态和强大跨平台能力, 成为数据科学等领域首选。本文探讨Python文件系统操作秘籍, 助力高效数据处理。
31 11
|
17天前
|
安全 算法 Go
Python面向对象的三大特性
python面向对象编程(OOP)的三大特性是封装、继承和多态。这些特性共同构成了OOP的基础,使得软件设计更加灵活、可维护和可扩展。
14 3
|
20天前
|
安全 数据安全/隐私保护 Python
|
21天前
|
监控 网络协议 网络安全
SMTP操作使用详解并通过python进行smtp邮件发送示例
SMTP操作使用详解并通过python进行smtp邮件发送示例
30 3
|
21天前
|
数据挖掘 数据处理 Python
🔍深入Python系统编程腹地:文件系统操作与I/O管理,打造高效数据处理流水线
【7月更文挑战第29天】深入Python系统编程腹地:文件系统操作与I/O管理,打造高效数据处理流水线
16 3
|
20天前
|
API Python
Python高手修炼手册:精通文件系统操作,掌控I/O管理,提升编程效率
【7月更文挑战第30天】在 Python 编程中, 文件系统操作与 I/O 管理是连接程序与数据的关键。初学者常因路径错误和权限问题受挫, 而高手能自如管理文件。传统 `os` 和 `os.path` 模块易出错, `pathlib` 提供了更直观的对象导向 API。I/O 方面, 同步操作会阻塞程序, 异步 (如使用 `aiofiles`) 则能大幅提升并发能力。真正的高手不仅掌握 API, 更能预见性能瓶颈并优化代码, 实现高效与优雅。
16 1
|
21天前
|
Serverless 语音技术 开发工具
函数计算操作报错合集之怎么何集成nls tts python sdk
在使用函数计算服务(如阿里云函数计算)时,用户可能会遇到多种错误场景。以下是一些常见的操作报错及其可能的原因和解决方法,包括但不限于:1. 函数部署失败、2. 函数执行超时、3. 资源不足错误、4. 权限与访问错误、5. 依赖问题、6. 网络配置错误、7. 触发器配置错误、8. 日志与监控问题。
|
22天前
|
数据采集 Java C语言
Python面向对象的高级动态可解释型脚本语言简介
Python是一种面向对象的高级动态可解释型脚本语言。
17 3