12导入类

water类

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

# File : Water.py
# Time : 2024/3/30 14:37
# Author : wes
# student :


class Water():
    # 初始化水属性
    def __init__(self, color, taste, point):
        self.color = color
        self.taste = taste
        self.point = point

    def introduce(self):
        print("我的颜色是{},我的味道是{},我的沸点是{}".format(self.color, self.taste, self.point))


if __name__ == '__main__':
    a = Water("无色", "无味", "100")
    a.introduce()

导入类:python编程允许你将一个个类,存储封装在模块中,然后在主程序中导入所需的这个模块,来对类的使用。

1.导入单个类

导入类是一种有效的编程方式,如果子类继承父类,父类和子类必须在同一个文件中,这会使我们的代码机器冗长。

# 下面我们创建一个water类,导入water类,我们就可以使用米快创建实例对象或者继承。
from Water import Water
a=Water("red","sweet","100")
a.introduce()

通过一个类移到一个模块中,并导入该类,依然可以使用棋所有的功能,使主程序文件变得简洁,易读,还能让我们大部分逻辑存储在一个相对 独立的python文件中,这样你可以不管这个写文件,而专注于主程序的高级逻辑编写、设计。

Fanta类

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

# File : Fanta.py
# Time : 2024/3/30 15:00
# Author : wes
# student :
from Water import Water


class Fanta(Water):
    # 初始化芬达的属性
    def __init__(self, color, taste, point):
        # 初始化父类的属性
        self.duce = "水,果葡糖浆,食品添加剂(......)"
        super().__init__(color, taste, point)

    def Int_duce(self):
        print("我的配料表是{}".format(self.duce))

    def introduce(self):
        print("颜色{},味道{},沸点{}".format(self.color, self.taste, self.point))

if __name__ == '__main__':
    my_water = Fanta("橙色", "甜", "101")
    my_water.introduce()
    my_water.Int_duce()

2.储存多个类

from Fanta import Fanta
a=Fanta("yellow","sweet","one_two_three")
a.introduce()
a.Int_duce()

通过这样的方式我们可以在模块文件中封装大量的类,使用这些模块的人不需要了解封装的具体内容,只需要了解大概的功能直接调用即可,这回大大提升我们软件程序的进度

3.导入多个类

在实际的编程中我们根据需要,需要在文件中导入任意数量的类,那么我们可以在文件中同时导入water类和Fanta类,在同一个文件中既有水,也有芬达。

from Water import Water,Fanta
a=Water("red","sweet","100")
a.introduce()


b=Fanta("yellow","sweet","one_two_three")
b.introduce()
b.Int_duce()

创建一个注册,登录,修改密码三个类,并使用代码完成用户的注册,登录,修改密码。只需要创建账号和密码就行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值