water类
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.导入单个类
导入类是一种有效的编程方式,如果子类继承父类,父类和子类必须在同一个文件中,这会使我们的代码机器冗长。
from Water import Water
a=Water("red","sweet","100")
a.introduce()
通过一个类移到一个模块中,并导入该类,依然可以使用棋所有的功能,使主程序文件变得简洁,易读,还能让我们大部分逻辑存储在一个相对 独立的python文件中,这样你可以不管这个写文件,而专注于主程序的高级逻辑编写、设计。
Fanta类
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()
创建一个注册,登录,修改密码三个类,并使用代码完成用户的注册,登录,修改密码。只需要创建账号和密码就行。