前言: 身為 LabVIEW 的開發者,每當我們想要新增功能時,就必須考量到:旗下的多數客戶,皆不是程式設計人員。本技術文章將說明,如何建立簡易的LabVIEW 物件導向程式設計 (LVOOP) 專案,包含使用 LabVIEW 物件導向程式設計的優點與方法,以及 LabVIEW 物件導向程式設計的技術細節與更多資源 。此篇承接上一篇LabVIEW_OOP物件導向是什麼?為什麼要用物件導向?_Part1 而寫,建議先複習此篇,再繼續閱讀本技術文章。另外整理了一些國內的中文LVOOP資源:LabVIEW_物件導向(LVOOP)資料、範例程式蒐集整理,給大家參考。
在寫程式前,先來說說LVOOP的最核心三個觀念:
1.繼承 (Inheritance)
2.封裝 (encapsulation)
3.多型 (Polymorphism)
繼承 (Inheritance),這個是最好理解的,在父類別裡寫好的方法,子類別繼承下來直接使用,是要達到"程式碼再利用"(Code Reuse)
至於封裝、多型,可能就比較難解釋,這裡先提一下,後面會再解釋,
封裝 (encapsulation) 每個類別都把屬於自己的資料以及功能放在同一空間稱為封裝。這麼一來比較不會產生「牽一髮而動全身」的狀況,降低模組間相互依賴的程度,也等於是降低複雜度,讓開發與維護更容易。
多型 (Polymorphism) 指的是不同型態的物件,由於被呼叫者 (Callee) 有著相同的介面,呼叫者並不用指定特別型別,實際執行的物件動態(Dynamic)決定,藉此增加程式碼的彈性。
繼上一篇提到,
客戶需求現在有手機專案要開發,其中有三種不同款式的手機程式,分別是 :一般手機、兒童機、網美機,
它們個別有各自功能的需求如下:
- 一般手機功能: 撥電話、拍照,文字大小為 20
- 兒童手機功能: 撥電話、 ,文字大小為 40
- 網美手機功能: 撥電話、拍照,文字大小為 12,另外拍照須加上美肌功能
依照需求整理如下表:
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521521733-1386738042_n.png&width=600&height=320)
依照這個需求,可以發現三種手機都有撥電話、拍照(兒童機除外)、設定文字大小的功能,而網美機另外還須加上美肌的功能,
若稍微修改一下,加上LVOOP的觀念近,可以把需求稍做改變如下,兒童和網美機繼承了一般手機的撥電話功能,卻又有一些改變,
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521529021-1710790099_n.png&width=600&height=348)
有了這個架構後,開始來寫程式,首先創立一個Project 叫做:Cell Phone Project
接著在My Computer下面新建立Class: Normal Cell Phone ,也就是這個專案的一般手機
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521529233-3508376925.png&width=311&height=434)
接著要建立的是這物件的方法: Setting設定、Picture(拍照)
對著Normal Cell Phone.lvclass 按右鍵選擇New->VI from Dynamic Dispatch Template 新增兩個動態的的功能叫做:Setting、Picture
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521531371-3426224418.png&width=449&height=134)
這裡會有靜態呼叫、動態呼叫的分別,是因為 撥打電話(Call)在不同款的手機中不會不同,而設定(Setting)、拍照(Picture)會因為手機不同而有所改變,所以選擇動態(Dynamic Dispatch)
創建好後呈現會是這樣,
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521531310-4034137756.png&width=192&height=72)
接著要來"複製"這些功能到不同的手機上,複製兩次分別取名為: Child Cell Phone.lvclass、Beauty Cell Phone.lvclass 也就是本專案的"兒童機"、"網美機"
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521530188-4241016223.png&width=339&height=158)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521530323-2086378358_n.png&width=418&height=294)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521530344-2425581577_n.png&width=421&height=297)
一切就緒後,要來介紹第一個觀念
繼承
首先我們需要把Beauty Class、Child Class這兩個子物件都繼承父物件Normal
Child Cell Phone.lvclass->Properties->Inheritance->Change Inheritance->選擇Normall Cell Phone->OK,以此類推設定好Beauty Cell Phone
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521531494-1094723650.png&width=267&height=503)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521531533-3407292029.png&width=430&height=327)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521530703-3350448897.png&width=328&height=291)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521530880-496960958_n.png&width=600&height=238)
這時候在Normal Class 按右鍵->Show Class Hierarchy,就可以看到Child和Beauty已經在Normal Class下面,可以繼承Normal Class功能
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521530987-2159349675.png&width=109&height=183)
接著,對著Normal Cell Phone.lvclass 按右鍵選擇New->VI from Static Dispatch Template 新增一個靜態的的功能叫做:Call
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521529628-3575504846_n.png&width=600&height=153)
可以從下面這個動畫看出直接繼承的樣子,可以看到不管哪種Class,Call這個功能都是使用Normal 的撥號方法(綠色),而且Child Cell Phone、Beauty Cell Phone在使用時完全不需要做任何修改,直接引用即可以。
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521531141-2051517328.gif&width=300&height=188)
封裝
每個類別都把屬於自己的資料以及功能放在同一空間稱為封裝
另外類別中需要的資料也在專屬的xxx.ctl中
多型
第二個要介紹的是多型,也可以叫做重載、多載
因為網美機在像機部分做了一點修改,所以我們對Beauty Cell Phone 底下的Picture.vi做點修改,我們除了本身一般手機的功能另外還要加上其他進階功能,
加入Call Parent Mehod.vi到picture.vi裡面,去使用父物件的方法(Mehod)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521532107-1307828341.png&width=345&height=284)
完成後,這樣Beauty Cell Phone的照相功能,不只有原本一般手機的照相功能,另外還加上自己的額外進階美肌功能 (重載)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521532274-818647042.png&width=332&height=218)
接著要設定字體大小,打開Normal Cell Phone.ctl在裡面加入Front Size,重複在Beauty、Child也加上Front size,
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521532544-3392753413.png&width=593&height=237)
在Normal Cell Phone->Setting.vi中加入Bundle by Name
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521533644-696009075.png&width=462&height=115)
接上Class 接角,和Connector Output,完成後如下圖
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521533341-3882862178_n.png&width=600&height=253)
接著重複在Child Cell Phone Class、Beauty Phone Class 重複此動作,這樣就能達到在不同的物件下做不同的資料設定。
(這裡不使用Data Member Access,後面章節在介紹)
特別針對Beauty Class的功能再加上個別的參數,Beauty Setting
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521534519-2704049362.png&width=562&height=195)
這樣我們在使用Beauty物件裡的功能時候都有專屬的Beauty Setting參數可以使用,這是在其他的物件裡面不能看到的
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521534670-3895679535_n.png&width=566&height=411)
這樣我們就完成此專案的需求了,試著調換不同Class,就可以看就多型帶來的好處
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/a01832851/1521531141-2051517328.gif&width=300&height=188)
程式碼可在這邊下載,若下載請留言告知,謝謝
整理了一些國內中文的LVOOP參考資源:
LabVIEW_物件導向(LVOOP)資料、範例程式蒐集整理
如果覺得文章有幫助,請不要吝嗇按下側邊的廣告唷~ 謝謝~
學習相關LabVIEW文章:
進階程式學習
基礎程式學習
專題學習_LVOOP
專題學習_動態載入
程式分享相關LabVIEW文章:
基礎程式分享
進階程式分享
股票程式分享
考取LabVIEW證照相關:
CLAD
CLD
CLA準備、CLA考題分析