Python学习笔记--2--面向对象编程
生活随笔
收集整理的這篇文章主要介紹了
Python学习笔记--2--面向对象编程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 面向對象
- 類和裝飾器@
- #coding=gbkclass student:def __init__(self,name,grand):#初始化構造函數,self相當于java中的this,相當于一個student的一個對象self.name=nameself.grand=granddef introduce(self):print('hi ! i am '+self.name)print('my grade is : '+str(self.grand))def improve(self,amount):self.grand=self.grand+amountjim=student('jim',86)
jim.introduce()jim.improve(10)
jim.introduce()#python中的裝飾器
def add_candles(cake_func): #傳入的參數可以是一個函數 cake_func函數def insert_candles():#在一個大的函數中建立一個小函數去修改值return cake_func()+"candles"return insert_candles #返回的是insert的一個對象
@add_candles #裝飾器@ 就是在要修改的函數的上方加上:@+修改該函數需要用到的函數
def make_cake():return 'cake'print(make_cake())def add_a(b):def add_b():return b()+2return add_b
@add_a
def b():return 1print(b())
?GUI對話框
- #coding=gbk#圖形界面和參數字游戲
#利用tkinter庫創建圖形界面from tkinter import * #導入tkinter中所有的東西
import tkinter.simpledialog as dl
import tkinter.messagebox as mb#設置GUI,顯示對話框
root=Tk() #tkinter中的一個構造函數 創建一個顯示框
w=Label(root,text='Label Title') #創建愛你一個標簽 包含窗口和標簽標題
w.pack()#標簽自帶的函數 調整大小#輸入
mb.showinfo('welcome', 'welcome message')
guess=dl.askinteger('number','enter a number' )#提供一個用戶輸入的對話框,輸入一個整型的數,傳遞給guess#輸出
output='this is output message'
mb.showinfo('output', output)
?猜數字游戲
- 1.GUI ?form tkinter
- 2.邏輯層
- #coding=gbkfrom tkinter import * #導入tkinter中所有的東西
import tkinter.simpledialog as dl
import tkinter.messagebox as mb#設置GUI,顯示對話框
root=Tk() #tkinter中的一個構造函數 創建一個顯示框
w=Label(root,text='!猜數字游戲!') #創建愛你一個標簽 包含窗口和標簽標題
w.pack()#標簽自帶的函數 調整大小mb.showinfo('welcome', 'welcome to guess number game')#展示標簽信息number=59
while True:guess=dl.askinteger('number','what is your guess' )#提供一個用戶輸入的對話框,輸入一個整型的數,傳遞給guessif guess==number:output='bingo! you guessed it right,but you do not win any prizes~!'mb.showinfo('output', output)breakelif guess<number:output='you guessed it wrong,it is too small'mb.showinfo('output', output)else:output='you guessed it wrong,it is too big'mb.showinfo('output', output)
print('DONE')
?
- #coding=gbkclass student:def __init__(self,name,grand):#初始化構造函數,self相當于java中的this,相當于一個student的一個對象self.name=nameself.grand=granddef introduce(self):print('hi ! i am '+self.name)print('my grade is : '+str(self.grand))def improve(self,amount):self.grand=self.grand+amountjim=student('jim',86)
jim.introduce()jim.improve(10)
jim.introduce()#python中的裝飾器
def add_candles(cake_func): #傳入的參數可以是一個函數 cake_func函數def insert_candles():#在一個大的函數中建立一個小函數去修改值return cake_func()+"candles"return insert_candles #返回的是insert的一個對象
@add_candles #裝飾器@ 就是在要修改的函數的上方加上:@+修改該函數需要用到的函數
def make_cake():return 'cake'print(make_cake())def add_a(b):def add_b():return b()+2return add_b
@add_a
def b():return 1print(b())
- 類和裝飾器@
轉載于:https://www.cnblogs.com/Kobe10/p/5706613.html
總結
以上是生活随笔為你收集整理的Python学习笔记--2--面向对象编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 转: Ubuntu 安装字体方法
- 下一篇: websocket python爬虫_p