Python学习笔记--2--面向对象编程
生活随笔
收集整理的這篇文章主要介紹了
Python学习笔记--2--面向对象编程
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
- 面向?qū)ο?
- 類和裝飾器@
- #coding=gbkclass student:def __init__(self,name,grand):#初始化構(gòu)造函數(shù),self相當(dāng)于java中的this,相當(dāng)于一個(gè)student的一個(gè)對(duì)象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): #傳入的參數(shù)可以是一個(gè)函數(shù) cake_func函數(shù)def insert_candles():#在一個(gè)大的函數(shù)中建立一個(gè)小函數(shù)去修改值return cake_func()+"candles"return insert_candles #返回的是insert的一個(gè)對(duì)象
@add_candles #裝飾器@ 就是在要修改的函數(shù)的上方加上:@+修改該函數(shù)需要用到的函數(shù)
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對(duì)話框
- #coding=gbk#圖形界面和參數(shù)字游戲
#利用tkinter庫(kù)創(chuàng)建圖形界面from tkinter import * #導(dǎo)入tkinter中所有的東西
import tkinter.simpledialog as dl
import tkinter.messagebox as mb#設(shè)置GUI,顯示對(duì)話框
root=Tk() #tkinter中的一個(gè)構(gòu)造函數(shù) 創(chuàng)建一個(gè)顯示框
w=Label(root,text='Label Title') #創(chuàng)建愛(ài)你一個(gè)標(biāo)簽 包含窗口和標(biāo)簽標(biāo)題
w.pack()#標(biāo)簽自帶的函數(shù) 調(diào)整大小#輸入
mb.showinfo('welcome', 'welcome message')
guess=dl.askinteger('number','enter a number' )#提供一個(gè)用戶輸入的對(duì)話框,輸入一個(gè)整型的數(shù),傳遞給guess#輸出
output='this is output message'
mb.showinfo('output', output)
?猜數(shù)字游戲
- 1.GUI ?form tkinter
- 2.邏輯層
- #coding=gbkfrom tkinter import * #導(dǎo)入tkinter中所有的東西
import tkinter.simpledialog as dl
import tkinter.messagebox as mb#設(shè)置GUI,顯示對(duì)話框
root=Tk() #tkinter中的一個(gè)構(gòu)造函數(shù) 創(chuàng)建一個(gè)顯示框
w=Label(root,text='!猜數(shù)字游戲!') #創(chuàng)建愛(ài)你一個(gè)標(biāo)簽 包含窗口和標(biāo)簽標(biāo)題
w.pack()#標(biāo)簽自帶的函數(shù) 調(diào)整大小mb.showinfo('welcome', 'welcome to guess number game')#展示標(biāo)簽信息number=59
while True:guess=dl.askinteger('number','what is your guess' )#提供一個(gè)用戶輸入的對(duì)話框,輸入一個(gè)整型的數(shù),傳遞給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):#初始化構(gòu)造函數(shù),self相當(dāng)于java中的this,相當(dāng)于一個(gè)student的一個(gè)對(duì)象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): #傳入的參數(shù)可以是一個(gè)函數(shù) cake_func函數(shù)def insert_candles():#在一個(gè)大的函數(shù)中建立一個(gè)小函數(shù)去修改值return cake_func()+"candles"return insert_candles #返回的是insert的一個(gè)對(duì)象
@add_candles #裝飾器@ 就是在要修改的函數(shù)的上方加上:@+修改該函數(shù)需要用到的函數(shù)
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())
- 類和裝飾器@
轉(zhuǎn)載于:https://www.cnblogs.com/Kobe10/p/5706613.html
總結(jié)
以上是生活随笔為你收集整理的Python学习笔记--2--面向对象编程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 转: Ubuntu 安装字体方法
- 下一篇: mysql创建新用户并分配数据库权限