码迷,mamicode.com
首页 > 其他好文 > 详细

类(三)

时间:2017-05-13 01:00:30      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:文件   __init__   span   主程序   elf   运动员   str   编写   import   

导入类:

 随之不断的给类添加功能,文件可能变得很长(即便妥善的使用了继承)。为了遵循python的总体理念,应让文件尽可能整洁。

为了在这方面提供帮助,Python允许你将类存储在模块中,然后在主程序中导入需要的模块。

 

在个模块中,是可以存储多个类的,在主程序中导入类的时候,需要明确导入哪一个类

 

练习:

 编辑一个类用于描述篮球运动员,其通用属性有:

身高、体重、卧推、垂直弹跳、司职、年龄

在这个类中配置一个方法,打印(XX球员正在NBA试训)

"""一个描述篮球运动员的类"""
class Basketball_player():
def __init__(self,name,height,weight,bench_press,part,age):
self.name = name
self.height = height
self.weight = weight
self.bench_press = bench_press
self.part = part
self.age = age
    def brother_basketball(self):
print("Hi Lebron " + "I am " + self.name.title() + ".\n" + "We play together !")
 

这个类对应的文件=basketball.py

编写程序,来调用这个模块

"""勒布朗组建兄弟篮球队"""
from basketball import Basketball_player

CP3 = Basketball_player(‘pual‘,‘190‘,‘170‘,‘75KG‘,‘PG‘,‘28‘)
wade = Basketball_player(‘dwyane‘,‘201‘,‘180‘,‘100KG‘,‘SG‘,‘31‘)
melon = Basketball_player(‘Anthony‘,‘210‘,‘200‘,‘120KG‘,‘SF‘,‘30‘)

CP3.brother_basketball()
wade.brother_basketball()
melon.brother_basketball()

在一个模块中,有时候可能出现多个类,需要全部导入的时候,使用下面的指令
from module_name import *

类(三)

标签:文件   __init__   span   主程序   elf   运动员   str   编写   import   

原文地址:http://www.cnblogs.com/alben-cisco/p/6847773.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!