国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
使用NetBeans IDE開發(fā)Python應(yīng)用程序詳解

本教程的目的是向您介紹在 NetBeans IDE 中使用 Python 編程語(yǔ)言的基礎(chǔ)知識(shí)。這樣,程序員就可以繼續(xù)深入了解該語(yǔ)言的功能。NetBeans 添加了對(duì)多種 JVM 語(yǔ)言和非 JVM 語(yǔ)言的支持,從而為程序員開辟了新的編程途徑,同時(shí)也為他們以多種不同的語(yǔ)言(包括 Python)進(jìn)行開發(fā)提供了易于使用的環(huán)境。在本教程中,您將從頭開發(fā)一個(gè)小型應(yīng)用程序 (hockeyRoster.py)。開發(fā)出的應(yīng)用程序?qū)⒂糜谟涗浐凸芾砬蜿?duì)信息。

預(yù)計(jì)時(shí)間:20 分鐘

目錄

應(yīng)用程序概述

設(shè)置

添加新 Python 安裝

安裝并配置 Python 安裝

創(chuàng)建項(xiàng)目

添加代碼

運(yùn)行應(yīng)用程序

小結(jié)

要學(xué)習(xí)本教程,您需要具備以下軟件和資源。

軟件或資源 要求的版本
NetBeans IDE 6.5 或 6.7 Python EA 版本
Java Developer Kit (JDK) 版本 6 或版本 5
Python 2.5.x
樣例數(shù)據(jù)庫(kù) 無(wú)要求

應(yīng)用程序概述

在本基礎(chǔ)教程中,您將開發(fā)一個(gè)管理曲棍球隊(duì)信息的命令行應(yīng)用程序,通過該應(yīng)用程序可以將包含球員及其首選球場(chǎng)位置和當(dāng)前狀態(tài)("BENCHED" 或 "ACTIVE")的列表添加到外部文件中。這樣,您便可以通過 Python 列出該文件的內(nèi)容,并更新記錄。本教程涉及文件輸入/輸出內(nèi)容,但并未深入介紹有關(guān)數(shù)據(jù)庫(kù)的詳細(xì)信息。

設(shè)置

需要下載 NetBeans IDE 的 Python EA2 版本才能學(xué)完本教程。

注意:如果使用的是 NetBeans IDE 6.8,可通過以下操作從 Beta 更新中心安裝 Python 插件:

1. 在 IDE 的主菜單中,轉(zhuǎn)至“工具”>“插件”。

2. 在“插件”對(duì)話框中,單擊“可用插件”標(biāo)簽并在“類別”列中查找 Python。

3. 選中 Python 和 Jython 發(fā)行版本插件旁邊的“安裝”框,然后單擊“安裝”。

4. 在“NetBeans IDE 安裝程序”對(duì)話框中,確認(rèn)將要安裝 Python 和 Jython 插件,單擊“下一步”,然后接受許可證,安裝開始。

您可以在 "Python Platform Manager"(Python 平臺(tái)管理器)中配置 IDE 以支持 Python。此窗口允許您添加或刪除可用于 IDE 的 Python 安裝。使用 "Python Platform Manager"(Python 平臺(tái)管理器),還可以設(shè)置和配置 Python 路徑及 Java 路徑。通過執(zhí)行上述操作,可以將 Java jar 文件或 Python 模塊置于 Path 變量中,以便在每次啟動(dòng) IDE 時(shí)都可以使用這些文件或模塊。此外,您甚至還可以為所配置的每個(gè)安裝定義不同的 Java 路徑或 Python 路徑。

添加新 Python 安裝

1. 在主工具欄中,選擇“工具”> "Python Platforms Manager"(Python 平臺(tái)管理器)。

此時(shí)會(huì)打開 "Python Platform Manager"(Python 平臺(tái)管理器),如下圖所示。

2. 如果缺省情況下未顯示 Python 安裝,請(qǐng)單擊 "New"(新建),瀏覽至要使用的安裝,然后選擇“打開”。

IDE 會(huì)將該安裝添加到 "Platforms"(平臺(tái))列表中。如果您不確定安裝的位置,請(qǐng)單擊 "Auto Detect"(自動(dòng)檢測(cè))。

安裝并配置 Python 安裝

1. 選擇“工具”> "Python Installations"(Python 安裝)以打開 "Python Platform Manager"(Python 平臺(tái)管理器)。

2. 選擇要用于 IDE 的 Python 安裝,然后單擊 "Make Default"(設(shè)為缺省)。

3. 在 "Command Arguments"(命令參數(shù))文本字段中,輸入安裝的任何命令參數(shù)。如有必要,也可以對(duì)平臺(tái)重命名。

4. 單擊 "Close"(關(guān)閉)關(guān)閉 "Python Platform Manager"(Python 平臺(tái)管理器)。

創(chuàng)建項(xiàng)目

首先,在 IDE 中創(chuàng)建一個(gè)新 Python 項(xiàng)目?!靶陆?xiàng)目”向?qū)ОS多項(xiàng)目類型的內(nèi)置模板。訪問“新建項(xiàng)目”向?qū)в卸喾N方法??梢栽谥鞴ぞ邫谥羞x擇“文件”>“新建項(xiàng)目”,也可以在“項(xiàng)目”窗口中單擊鼠標(biāo)右鍵,然后選擇“新建項(xiàng)目”。

1. 在主工具欄中,選擇“文件”>“新建項(xiàng)目”。

此時(shí)將打開“新建項(xiàng)目”向?qū)?,其中顯示了 "Python" 類別。

注意:當(dāng)計(jì)算機(jī)中只是安裝了 IDE 的 Python EA 版本時(shí),"Python" 會(huì)顯示為項(xiàng)目類別。如果 Python EA 是作為插件添加到 IDE 中的,則可能會(huì)顯示其他類別。

2. 選擇 "Python Project"(Python 項(xiàng)目)作為項(xiàng)目類型,如下圖所示。單擊“下一步”。

3. 輸入 HockeyRoster 作為項(xiàng)目名稱,如下圖所示。選擇要用于該項(xiàng)目的 Python 版本,然后重命名主文件 HockeyRoster.py。

4. 將 "Set as Main Project"(設(shè)置為主項(xiàng)目)復(fù)選框和 "Create Main File"(創(chuàng)建主文件)復(fù)選框保留為選中狀態(tài)。單擊“完成”。

IDE 將基于項(xiàng)目的名稱來(lái)創(chuàng)建項(xiàng)目文件夾。您可以更改此文件夾的名稱和位置。

5. 從 "Python Platform"(Python 平臺(tái))下拉列表中選擇要使用的 Python 平臺(tái),然后單擊“完成”以創(chuàng)建項(xiàng)目。

IDE 將創(chuàng)建該項(xiàng)目。請(qǐng)注意在“項(xiàng)目”窗口中是如何顯示該項(xiàng)目的。

請(qǐng)注意,在 IDE 的源代碼編輯器中將打開顯示一些基本信息的 HockeyRoster.py 文件,如下圖所示。NetBeans IDE 會(huì)自動(dòng)記錄項(xiàng)目的作者和創(chuàng)建日期,并且還會(huì)提供一個(gè)輸出 "Hello" 的簡(jiǎn)短樣例程序。

6.. 現(xiàn)在,您要為該應(yīng)用程序創(chuàng)建第二個(gè)源文件。在“項(xiàng)目”窗口中,右鍵單擊該項(xiàng)目的 "Sources"(源)節(jié)點(diǎn),然后選擇“新建”> "Empty Module"(空模塊),如下圖所示。

此時(shí)會(huì)打開 "New Empty Module"(新建空模塊)向?qū)А?/p>

7. 鍵入 Player 作為文件名,然后單擊“完成”。

將在源代碼編輯器中打開該文件。此時(shí),“項(xiàng)目”窗口應(yīng)與下圖類似。

添加代碼

在此部分,您要將 Python 代碼添加到在本教程上一部分所創(chuàng)建的文件中。

1. 如果尚未打開 Player.py 文件,請(qǐng)?jiān)谠创a編輯器中將其打開。

2. 在 Player.py 文件中鍵入以下代碼。

  1. # Player.py 
  2. # Container to hold our player objects 
  3.  
  4.  
  5. class Player: 
  6.  
  7.  
  8.     # Player attributes 
  9.  
  10.      
  11.     id = 0 
  12.     first = None 
  13.     last = None 
  14.     position = None 
  15.  
  16.     
  17.     # Function used to create a player object 
  18.  
  19.  
  20.     def create(self, id, first, last, position): 
  21.         self.id = id 
  22.         self.first = first 
  23.         self.last = last 
  24.         self.position = position 

此時(shí),源代碼編輯器應(yīng)與下圖類似。請(qǐng)注意 IDE 是如何幫助您輸入括號(hào)和冒號(hào)的。

3. 在源代碼編輯器中打開 HockeyPlayer.py。

4. 刪除 Hockeyplayer.py 中的現(xiàn)有代碼,然后鍵入以下代碼。這是添加的第一段代碼,它主要由注釋組成,但還將執(zhí)行以下兩項(xiàng)重要操作:

◆ 從先前創(chuàng)建的 Player 模塊中導(dǎo)入 Player 類。

◆ 定義一個(gè)列表,以記錄每個(gè) Player 對(duì)象。

  1. # HockeyRoster.py 
  2. # Implementation logic for the HockeyRoster application 
  3.  
  4. # Import Player class from the Player module 
  5. from Player import Player 
  6.  
  7. # Define a list to hold each of the Player objects 
  8. playerList = [] 

5. 現(xiàn)在,您要向應(yīng)用程序中添加更多代碼。此段代碼將創(chuàng)建一個(gè)用于在“輸出”窗口中生成輸出的函數(shù)(該輸出基于用戶所輸入的內(nèi)容),從而為應(yīng)用程序創(chuàng)建一個(gè)選擇器。

  1. # makeSelection() 
  2. # Creates a selector for our application.  The function prints output to the  
  3. # command line.  It then takes a parameter as keyboard input at the command line 
  4. # in order to choose our application option. 
  5.  
  6.  
  7. def makeSelection(): 
  8.     validOptions = ['1','2','3','4'] 
  9.     print "Please choose an option\n" 
  10.  
  11.  
  12.     selection = raw_input("Press 1 to add a player, 2 to print the team roster, 
  13. 3 to search for a player on the team, 4 to quit: ") 
  14.     if selection not in validOptions: 
  15.         print "Not a valid option, please try again\n" 
  16.         makeSelection() 
  17.     else: 
  18.         if selection == '1': 
  19.             addPlayer() 
  20.         elif selection == '2': 
  21.             printRoster() 
  22.         elif selection == '3': 
  23.             searchRoster() 
  24.         else: 
  25.             print "Thanks for using the HockeyRoster application." 

6. 右鍵單擊源代碼編輯器中的任意位置,然后選擇“格式化代碼”,如下圖所示。

現(xiàn)在,您要添加一些代碼,以接受來(lái)自“輸出”窗口的鍵盤輸入。用戶可以輸入球員的名、姓及場(chǎng)上位置。

  1. # addPlayer() 
  2. # Accepts keyboard input to add a player object to the roster list.  This function 
  3. # creates a new player object each time it is invoked and appends it to the list. 
  4. def addPlayer(): 
  5.     addNew = 'Y' 
  6.     print "Add a player to the roster by providing the following information\n" 
  7.     while addNew.upper() == 'Y': 
  8.         first = raw_input("First Name: ") 
  9.         last = raw_input("Last Name: ") 
  10.         position = raw_input("Position: ") 
  11.         id = len(playerList) 
  12.         player = Player() 
  13.         player.create(id, first, last, position) 
  14.         playerList.append(player) 
  15.         print "Player successfully added to the team roster\n" 
  16.         addNew = raw_input("Add another? (Y or N)") 
  17.     makeSelection() 

請(qǐng)注意 IDE 的代碼完成功能是如何在您鍵入代碼時(shí)提供建議的,如下圖所示。

8. 在此步中,您要添加將列表內(nèi)容以報(bào)告形式輸出到“輸出”窗口的代碼。

  1. # printRoster() 
  2. # Prints the contents of the list to the command line as a report 
  3. def printRoster(): 
  4.     print "====================\n" 
  5.     print "Complete Team Roster\n" 
  6.     print "======================\n\n" 
  7.     for player in playerList: 
  8.         print "%s %s - %s" % (player.first, player.last, player.position) 
  9.     print "\n" 
  10.     print "=== End of Roster ===\n" 
  11.     makeSelection() 

9. 現(xiàn)在,您要輸入一些代碼,以提取從“輸出”窗口輸入的球員姓名,并在花名冊(cè)中搜索與其匹配的內(nèi)容。

  1. # searchRoster() 
  2. # Takes input from the command line for a player's name to search within the 
  3. # roster list.  If the player is found in the list then an affirmative message 
  4. # is printed.  If not found, then a negative message is printed. 
  5. def searchRoster(): 
  6.     index = 0 
  7.     found = False 
  8.     print "Enter a player name below to search the team\n" 
  9.     first = raw_input("First Name: ") 
  10.     last = raw_input("Last Name: ") 
  11.     position = None 
  12.     while index < len(playerList): 
  13.         player = playerList[index] 
  14.         if player.first.upper() == first.upper() or player.last.upper() == last.upper(): 
  15.             found = True 
  16.             position = player.position 
  17.         index = index + 1 
  18.     if found: 
  19.         print '%s %s is in the roster as %s' % (first, last, position) 
  20.     else: 
  21.         print '%s %s is not in the roster.' % (first, last) 
  22.     makeSelection() 

10. 此時(shí),可以為應(yīng)用程序入口點(diǎn)添加代碼了。此代碼會(huì)將應(yīng)用程序標(biāo)題輸出到“輸出”窗口中,然后調(diào)用 makeSelection() 函數(shù)。

  1. # main 
  2. # This is the application entry point.  It simply prints the applicaion title 
  3. # to the command line and then invokes the makeSelection() function. 
     
  4. if __name__ == "__main__": 
  5.     print "Hockey Roster Application\n\n" 
  6.     makeSelection() 

運(yùn)行應(yīng)用程序

現(xiàn)在可以測(cè)試應(yīng)用程序了。在 NetBeans IDE 中,Python 應(yīng)用程序的結(jié)果會(huì)輸出到“輸出”窗口中。

1. 在“項(xiàng)目”窗口中,右鍵單擊該項(xiàng)目節(jié)點(diǎn)并選擇“運(yùn)行”。

“輸出”窗口中將顯示該應(yīng)用程序,如下圖所示。

2. 要測(cè)試應(yīng)用程序,請(qǐng)鍵入 1,然后按 Enter 鍵。

系統(tǒng)將提示您輸入要添加的球員的名、姓及其球場(chǎng)位置,如下圖所示。

3. 嘗試添加更多球員。然后,在初始應(yīng)用程序提示符下鍵入 2 并按 Enter 鍵,以輸出球隊(duì)花名冊(cè)。

將在“輸出”窗口中輸出花名冊(cè),如下圖所示。

4. 現(xiàn)在,在初始應(yīng)用程序提示符下鍵入 3 并按 Enter 鍵,以搜索球員。

將在“輸出”窗口中再次顯示結(jié)果。

5. 現(xiàn)在,嘗試搜索已知的但未列在花名冊(cè)中的球員。

“輸出”窗口將告知您該球員未列在花名冊(cè)中。

小結(jié)

在本教程中,您使用 NetBeans IDE 創(chuàng)建了一個(gè)簡(jiǎn)單的用戶輸入 Python 應(yīng)用程序。您創(chuàng)建了一個(gè) Python 項(xiàng)目,在該項(xiàng)目中添加了一個(gè)空模塊,試用了代碼完成功能,并且運(yùn)行了應(yīng)用程序,同時(shí)在 IDE 的“輸出”窗口中查看了結(jié)果。

【編輯推薦】

【責(zé)任編輯:陳貽新 TEL:(010)68476606】

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
使用 NetBeans IDE 5.0 開發(fā) J2EE 應(yīng)用程序的快速入門指南
Chapter 17: Deployment Targets
NetBeans IDE Java快速入門教程
NetBeans IDE Ruby 快速入門教程
Play框架指南:配置你喜歡的IDE
使用NetBeans 6.0 在10分鐘內(nèi)創(chuàng)建一個(gè) Ruby 博客程序
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服