search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

Python模組[模組可以包含可執行代碼, 函數和類或者這些東西的組合。]- 自行開發模組 載入和引用 – jashliao部落格

Python模組[模組可以包含可執行代碼, 函數和類或者這些東西的組合。]- 自行開發模組 載入和引用

 

 

資料來源: https://github.com/stormzhang/free-programming-books/blob/master/assets/python/Python核心编程(中文第二版)带目录.pdf 的p54~p55

 

模組[jash_Module.py]

# -*- coding: UTF-8 -*-

#jash_Module.py

#參考網頁:http://www.alarmchang.com/index.php?title=Python_import_指定目錄裡面的_.py_檔案

class FooClass(object):

    “””my very first class: FooClass”””

    version = 0.1 # class (data) attribute

    def __init__(self, nm=’John Doe’):#建構子

        “””constructor”””

        self.name = nm # class instance (data) attribute

        print(‘Created a class instance for’, nm)

    def showname(self):

        “””display instance attribute and class name”””

        print(‘Your name is’, self.name)

        print(‘My name is’, self.__class__.__name__)#顯示類別名稱

    def showver(self):

        “””display class(static) attribute”””

        print(self.version) # references FooClass.version

    def addMe2Me(self, x): # does not use ‘self’

        “””apply + operation to argument”””

        return x + x

 

       

 

使用[Script1.py]

# -*- coding: UTF-8 -*-

import sys #系統模組

sys.path.append(“D:\小廖的每日開發紀錄\Python Big Data\Python Windows\python_base_pdf”)#指定jash_Module所在目錄

import jash_Module#模組的檔案名稱

 

foo1 = jash_Module.FooClass()#將物件實體化~此時會執行建構子的輸出

 

foo1.showname()

 

foo1.showver()#顯示成員變數值

 

print(foo1.addMe2Me(100))#呼叫成員函數做運算

 

 

 

foo2 = jash_Module.FooClass(‘jash.liao’)#將物件實體化,建構子傳入參數~此時會執行建構子的輸出

 

foo2.version=0.2#直接指定成員變數

 

foo2.showver()#顯示成員變數值

 

 

 

 

 

 



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦