zoukankan      html  css  js  c++  java
  • py怎样建立模块和怎样导入模块

    模块是.py文件

    新建一个.py文件,在里面写上类

    就是一个模块了

    '''''Created on 2011-11-1 
     
    @author: dudong0726 
    '''  
      
    class Person:  
        ''''' 
        classdocs 
        '''  
        Count = 0  
      
        def __init__(self,name,age):  
            ''''' 
            Constructor 
            @param: name the name of this person 
            @param: age the age of this person   
            '''  
            self.name = name  
            self.age = age  
            Person.Count += 1  
              
        def detail(self):  
            ''''' 
             the detail infomation of this person 
            '''  
            print('name is ',self.name)  
            print('age is ',self.age)  
            print('there are '+str(Person.Count)+" person in the class")

    1.怎样引入一个模块?

    from cn import *

    怎样使用模块中的方法呢?

        '''''Created on 2011-11-1 
         
        @author: dudong0726 
        '''  
        from cn import *  
          
        if __name__ == '__main__':  
            p = Person('marry',21)  
            p.detail()  
              
            q = Person('kevin',24)  
            q.detail()  

    2.导入模块的第二种方法

    import cn告诉python我们将要使用这个,当我们使用时要在前面加上cn.来指明来自cn这个模块

        ''' 
        Created on 2011-11-1 
         
        @author: dudong0726 
        '''  
        import cn  
          
        if __name__ == '__main__':  
            p = cn.Person('marry',21)  
            p.detail()  
            q = cn.Person('kevin',24)  
            q.detail()  

     更多详细见http://dudong0726.iteye.com/blog/1226907

  • 相关阅读:
    策略模式
    Properties类学习笔记
    System类学习笔记
    一个反射的妙用案例
    new 对象时的暗执行顺序
    常用数据库默认端口号
    java对日开发常用语(词汇)总结
    java开发中常用语(词汇)含义
    MyBatis 常用词汇含义
    java SE,EE,ME区别
  • 原文地址:https://www.cnblogs.com/lonely-buffoon/p/6270142.html
Copyright © 2011-2022 走看看