《Python 编程从入门到实践》———— 导入类

 随着你不断地给类添加功能,文件可能变得很长,即便你妥善地使用了继承亦如此。为遵循 Python 的总体理念,应让文件尽可能整洁。为在这方面提供帮助,Python 允许你将类存储在模块中,然后在主程序中导入所需的模块。
导入单个类
  import 语句让 Python 打开模块 Class,并导入其中的 Newcar 类。这样我们就可以使用 Newcar 类了,就像它是在这个文件中定义的一样。导入类是一种有效的编程方式。如果在这个程序中包含了整个 Newcar 类,阅读的难度将更高。通过将这个类移到一个模块中,并导入该模块,你依然可以使用其所有功能,但主程序文件变得整洁而易于阅读了。这还能让你将大部分逻辑存储在独立的文件中;确定类像你希望的那样工作后,你就可以不管这些文件,而专注于主程序逻辑。

# Class.py
class Newcar(object):   # 介绍新车基本信息(品牌、型号、年份)
    def __init__(self,band,model,year):
        self.band = band
        self.model = model
        self.year = year
        self.mile = 0

    def printnewcarinfo(self):
        message = "My car band is " + str(self.band)
        message += " and the model is " + str(self.model)
        message += " which was bought in " + str(self.year)
        print(message)

# 输入
from Class import Newcar

my_newcar = Newcar("geely","preface",2020)
my_newcar.printnewcarinfo()


# 输出
My car band is geely and model is preface which was bought in 2020

一个模块中导入多个类
 可根据需要在程序文件中导入任意数量的类。从一个模块中导入多个类时,用逗号分隔了各个类。导入必要的类后,就可根据需要创建每个类的任意数量的实例。

# Class.py
class Newcar(object):   # 介绍新车基本信息(品牌、型号、年份)
    def __init__(self,band,model,year):
        self.band = band
        self.model = model
        self.year = year
        self.mile = 0

    def printnewcarinfo(self):
        message = "My car band is " + str(self.band)
        message += " and the model is " + str(self.model)
        message += " which was bought in " + str(self.year)
        print(message)


class Usedcar(object):   # 介绍二手车基本信息(品牌、型号、年份)
    def __init__(self,formerdriver,year,mile):
        self.driver = formerdriver
        self.year = year
        self.mile = mile

    def printusedcarinfo(self):
        message = "The used car former driver is " + str(self.driver)
        message += " and he bought this car in " + str(self.year) + "."
        message += " The driving mile is " + str(self.mile)
        print(message)

# 输入
from Class import Newcar,Usedcar

my_newcar = Newcar("geely","preface",2020)
my_newcar.printnewcarinfo()
my_usedcar = Usedcar("VW","Bora",2015)
my_usedcar.printusedcarinfo()

# 输出
My car band is geely and the model is preface which was bought in 2020
The used car former driver is VW and he bought this car in Bora. The driving mile is 2015

导入整个模块
 还可以导入整个模块,再使用句点表示法访问需要的类。这种导入方法很简单,代码也易于阅读。由于创建类实例的代码都包含模块名,因此不会与当前文件使用的任何名称发生冲突。

# Class.py
class Newcar(object):   # 介绍新车基本信息(品牌、型号、年份)
    def __init__(self,band,model,year):
        self.band = band
        self.model = model
        self.year = year
        self.mile = 0

    def printnewcarinfo(self):
        message = "My car band is " + str(self.band)
        message += " and the model is " + str(self.model)
        message += " which was bought in " + str(self.year)
        print(message)


class Usedcar(object):   # 介绍二手车基本信息(品牌、型号、年份)
    def __init__(self,formerdriver,year,mile):
        self.driver = formerdriver
        self.year = year
        self.mile = mile

    def printusedcarinfo(self):
        message = "The used car former driver is " + str(self.driver)
        message += " and he bought this car in " + str(self.year) + "."
        message += " The driving mile is " + str(self.mile)
        print(message)

# 输入
import Class

my_newcar = Class.Newcar("geely","preface",2020)
my_newcar.printnewcarinfo()
my_usedcar = Class.Usedcar("VW","Bora",2015)
my_usedcar.printusedcarinfo()

# 输出
My car band is geely and the model is preface which was bought in 2020
The used car former driver is VW and he bought this car in Bora. The driving mile is 2015

おすすめ

転載: blog.csdn.net/qq_42957717/article/details/118175268