跳到主要內容


Python-類別

Python

類別

基本概念說明


參考 程式語言概念-類別內容


基本語法


class className:
      def __init__(self, var1):
              this.Name
      def myMethod(self, var1):
              print('helloworld')
obj = className('a')


語法說明


class         定義類別
className 類別名稱
def            定義函數
__init__      初始化函數
myMethod  自訂函數
this            本類別中自訂的變數或函數存放位置
self            導入本類別自訂變數或函數
var1            外部導入的變數

在函數括弧中加入self、var,就表示該函數會導入self、var,var為在使用的時候需要給予其職
類別在使用的時候,直接使用變數存放即可

繼承

class Name1:
    ....

class Name2(Name1):
    ....

語法說明

在新的類別的名稱後方括弧中,加入被繼承的名稱即可

封裝

不討論,因為如使用者能夠得到編寫的程式,封裝其實沒有意義,而python大多的腳本都是OpenSource的所以實在意義不大,即便使用.pyc的方式,也是有程式能夠輕易反向工程出原本的程式碼。

多型

class Name1:
    def A():
      print('Hello World')

class Name2(Name1):
    def A():
       print('Hello')

class Name3(Name1):
   def A():
      super().A()
      print('hello')

語法說明

在繼承之後對同樣的名稱函數進行覆寫被稱為多型,若要連同原先的指令流程加上新的流程要使用suer()。

 投影片-slideshare:Python_函數
 影片-youtube:Python_函數
 程式碼-Github:Python_函數
下一單元:Python-開啟檔案

留言

這個網誌中的熱門文章

Python-資料庫-mongodb-pymongo

Python 資料庫 mongodb-pymongo 安裝: linux、mac:pip3 install pymongo windows: import pymongo client = pymongo.MongoClient("mongodb://localhost:27017/") db = client['demo_db'] col = db['demo_col'] dict1 = { "name": "ab123ab456g", "day": "1890-04-05" } result = col.insert_one(dict1)  dict2 = [   { "name": "ki", "day": "1666-1-1"},   { "name": "aa", "day": "1222-11-11"},   { "name": "gg-gg", "day": "1333-02-22"},   { "name": "T-T", "day": "1444-03-02"},   { "name": "f-f", "day": "1555-01-01"} ] result = col.insert_many(dict2) result = col.find_one() print(result) results = col.find() for result in col.find(): print(result) results = col.find() query = {'...

語言學習-English-Lights

Song Lyric Title : Lights Singer :Ellie Goulding Album :  Bright Lights Release Date :  2011 I had a way then losing it all on my own I had a heart then but the queen has been overthrown And I'm not sleeping now the dark is too hard to beat And I'm not keeping up the strength I need to push me You show the lights that stop me turn to stone You shine it when I'm alone And so I tell myself that I'll be strong And dreaming when they're gone 'Cause they're calling, calling, calling me home Calling, calling, calling home You show the lights that stop me turn to stone You shine them when I'm alone Noises, I play within my head Touch my own skin and hope they'll still be there And I think back to when my brother and my sister slept In and on my place the only time I feel safe You show the lights that stop me turn to stone You shine it when I'm alone And so I tell myself that I'...

程式語言學習概論(1)

程式語言 介紹