python基础-学习记录day14

73 阅读3分钟

面向对象一

1、类和对象

"""
超市: 生活用品区  水果区  海鲜区    类
   对象:
         牙膏 牙刷 洗发水
         苹果 梨子 香蕉
         小龙虾 鱼 螃蟹

人类 对应每一个人就是一个对象
胡歌  彭于晏  朱亚文  吴彦祖
眼睛 鼻子 颜值
动物 大类
狗  分类
哈士奇  二哈  拉布拉多 田园犬
"""


class Student:
    def pinrt_info(self):  # self ??? 是啥
        print('张三学习python')
        print(self)  # <__main__.Student object at 0x0000021A032A6FD0>
    # 属性 方法


zhangsan = Student()  # 实例化对象
print(zhangsan)  # <__main__.Student object at 0x0000021A032A6FD0>
# 怎么调用类中方法   对象名.方法名()
zhangsan.pinrt_info()
# def print_info():
#     pass
"""
self参数:
1 形参  
2 代表当前对象本身  
"""

2、self参数

"""
self参数:
1 形参
2 代表当前对象本身
"""


class Student:
    # 初始化方法 魔法方法  当我的对象创建成功是 会自动的触发
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def print_info(self):
        # print(lisi.name,lisi.age)  # 不建议使用
        print(self.name, self.age)  # self代表的就是当前对象本身


lisi = Student('李四', 18)  # 创建对象 通过传递参数的形式给我的对象进行属性设置
# 对象创建的属性   在类外面给对象设置属性是不好的
# lisi.name = '李四'
# lisi.age = 18
lisi.print_info()

wangwu = Student('王五', 18)
wangwu.print_info()

3、魔法方法

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

    def main(self):
        print(self.width)
        print(self.heigth)


apple = My_Phone(1, 2)
apple.main()

xiaomi = My_Phone(100, 200)
xiaomi.main()
class My_Phone():
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def apple_phone(self):
        print("苹果手机的宽为:", self.width)
        print("苹果手机的高为:", self.height)

    def huawei_phone(self):
        print("华为手机的宽为:", self.width)
        print("华为手机的高为:", self.height)


apple = My_Phone(10, 20)
apple.apple_phone()
"""str 返回一些我们自定义的内容"""
class Persons:
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender

    def __str__(self):  # 返回的是字符串
        # return self.name   # 对象的创建处
        # return self.age
        # return self.gender
        # return self.name,str(self.age),self.gender   # 返回多个值 类型为元组
        return f'{self.name},{self.age},{self.gender}'

    def asd(self):
        return f'{self.name},{self.age},{self.gender}'


zhangsan = Persons('张三', 19, '男')
print(zhangsan)
print(zhangsan.asd())
""del方法"""
class Demo:
    def __init__(self, width, heigth):
        self.width = width
        self.heigth = heigth

    def __del__(self):
        print("对象已经删除")


a = Demo(10, 20)
# del方法
class Demo:
    def __del__(self):  # 当由该类创建的实例对象,被删除或者说在内存中被释放,将会自动触发执行。
        print("被释放了!")


d = Demo()
print("*" * 20)
del d  # 相当于提前执行了del方法
print("*" * 20)
"""del 每种语言种都有 但是python的处理效率不高 弱类型语言 """

4、类属性与方法

# 1.实例属性
class Province:
    # 类属性
    gender = '男'

    def __init__(self, country, province):
        # 实例属性
        self.country = country
        self.province = province

    # 实例方法
    def print_info(self):
        print(self.gender)
        print(self.country, self.province)  # 通过self去访问  这个要方便很多
        print(zs.country, zs.province)  # 通过每个对象去访问


zs = Province("中国", "湖南")
zs.print_info()

ls = Province("中国", "湖北")
ls.print_info()
# 2.私有属性
class Demo():
    __num = 0  # 私有属性
    result = 0  # 公开属性

    def count_(self):
        self.__num += 1
        self.result += 1
        print(self.__num)

    def getnum(self):
        return self.__num


run = Demo()
run.count_()  # 1
print(run.result)  # 1
# print(run.__num)    # 报错,实例不能访问私有变量
__num = run.getnum()
print(__num)  # 1
class func:
    def fun1(self):
        print("1")

    def __fun2(self):
        print("2")

    def fun3(self):
        return self.__fun2()


f = func()

f.fun1()  # 1
# f.__fun2()  # 报错
f.fun3()  # 2
#
# # 对象._类名__私有属性(方法)
print(f._func__fun2)  # 不建议使用  拓展
# <bound method func.__fun2 of <__main__.func object at 0x000001477A976C80>>

5、烤地瓜

# -*- coding:utf-8 -*-

class Test_Potato():
    # 初始化方法
    def __init__(self):
        # 烤的时间
        self.cook_time = 0
        # 烤的状态
        self.cook_condition = "生的"
        # 放的调料
        self.cook_seasoning = []

    # 被烤
    def cook(self, time):
        # 初始被烤时间 0 加上 用户输入的被烤时间
        self.cook_time += time  # 11

        if 0 <= self.cook_time < 5:
            self.cook_condition = "生的"
        if 5 <= self.cook_time < 10:
            self.cook_condition = "半生半熟"
        if 10 <= self.cook_time <= 12:
            self.cook_condition = "熟了"
        if self.cook_time > 12:
            self.cook_condition = "烤糊了"

    # 添加调料
    def add_seasoning(self, *seasoning):  # 不定长参数  元组
        self.cook_seasoning.append(seasoning)

    # 显示地瓜当前的状态
    def __str__(self):
        return f"当前地瓜烤了--{self.cook_time}--分钟,地瓜的状态是--{self.cook_condition}--,添加的调料是--{self.cook_seasoning}"


# 程序主入口
if __name__ == '__main__':
    # 创建对象
    main = Test_Potato()  # 自动触发init方法
    # 调用被烤
    main.cook(11)  # 传入被烤的时间
    # 调用添加调料的功能
    main.add_seasoning("辣椒", "花生", "孜然")  # 传入的调料
    # 打印自定义信息
    print(main)
    # 当前地瓜烤了--11--分钟,地瓜的状态是--熟了--,添加的调料是--[('辣椒', '花生', '孜然')]

6、练习:

创建一个学生类:

属性:姓名,年龄,学号 使用init方法

方法:展示学生信息(自我介绍) 使用str方法

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

    def __str__(self):
        return f"同学们好,我叫{self.name},今年{self.age}岁,我的学号是{self.stu_id}"


king = Student("阿金", 24, 114514)
print(king)
# 同学们好,我叫阿金,今年24岁,我的学号是114514