python_套件中引入模組_模組中導入類_私有及公有屬性_程式範例

一個作業 增強類的實作、模組及套件的概念

編譯練習環境位在  pycharm







一份專案檔的目錄下

有一個主程式區塊 main.py 、 一個SuperCalculator.py的模組






main.py程式碼



#coding by 冠羽
#from myCalculator import  * ---->  python3 不能使用
#python 3 中  print 變成  print() 函數形式
'''https://read01.com/M7JAM.html'''
#寫法1. 從myCalculator(套件)(package) 中導入兩個文件(模組)(module) 
from myCalculator import BMIHelper , ScoreHelper
#寫法2. 與main.py同一目錄下 可直接導入該模組
import SuperCalculator
#指定類別與法 :變數 = 該模組.某類
cal = SuperCalculator.Calculator()
print("計算機名稱:"+cal.name)
print ("計算機價格:"+str(cal.price))
cal.add(8,4)
cal.sub(24,6)
cal.multi(8,6)
cal.divide(16,8)

b = BMIHelper.BMIcalculator(175,67)
print('BMI數值:'+ str(b.BMI()))
s1 = ScoreHelper.Student('資傳系','周冠羽','22','男')
print(s1.mathScore)
s1.showSex()
#使用另法訪問私有屬性
print(dir(s1))#打印s1我們用Student類建立之物件內部細節
#我們也可以用 一下槓+類+雙下槓私有屬性  直接訪問
print(s1._Student__sex)
#print(s1._Student__id)
#輸出學生物件的寫法1 調用父類構造函數內部預設之參數
#針對實體(例)化對象之變數
print(s1.name + '/' + s1.sex+'/' + s1.age+'/'+s1.major)
#輸出學生物件的寫法2. 定義好的方法呼叫
s1.showStuInfo()

#輸出ScoreHelper檔案中的Student類別資訊
s1.showClass()

print(type(s1.name)) #為str
s1.name = '小明'
print(s1.name)
print(type(s1.englishScore)) #為int
print('英文成績:'+ str(s1.englishScore) )
s1.englishScore = 80
print('英文成績:'+ str(s1.englishScore) )



SuperCalculator.py


class Calculator:
    name = 'Good calculator' #類之屬性1
    price = 18 #類之屬性2
    #每一個功能以def來定義方法
    def add(self,x,y): #在類為一個默認參數
        #print (self.name) #針對Calculator此類之名字
        resultAdd = x+y
        print (resultAdd)
    def sub(self,x,y):
        resultSub = x-y
        print (resultSub)
    def multi(self,x,y):
        print (x*y)
    def divide(self,x,y):
        print (x/y)


一個Folder 名為 myCalculator

在當中因為有 一份 __init__.py 的檔案

因此已被視為 一整個 套件






__init__.py  程式碼


#-*- coding: UTF-8 -*-


BMIHHelper.py  程式碼


class BMIcalculator: #定義父類
    def __init__(self, h=0, w=0):
        print('調用BMIcalculator父類構造函數')
        self.height = h
        self.weight = w
    def BMI(self):
        return self.weight / ((self.height / 100) ** 2)


ScoreHelper.py  程式碼


class Student(object): #當Student這個類沒有捨麼父類好繼承就繼承object這個類
    '''Student類之說明'''   #類之說明
    # 私有屬性(兩下行槓) 無法在類外被使用or直接訪問
    __sex = 'male' # 預設 男性
    __id = '02160270' #學號
    #公有屬性(沒加兩下行槓)
    grade = 106
    mathScore = 0
    englishScore = 0

    def  __init__(self,maj,name,age, sex):#self 似 C++  this
        print('調用Student父類構造函數')
        self.major = maj
        self.name = name
        self.age = age
        self.sex = sex
        print('init Student object')

    def showSex(self):
        print('不能直接s1.槓槓秀私有屬性')
        print('這裡借助ScoreHelper類內部之'
              'showSex方法秀__sex')
        print (self.__sex)

    def setName(self , newName):
        self.name = newName

    def getName(self):
        return self.name

    def showStuInfo(self):
        #打印父類構造函數內部預設之參數
        print("學生的系所:" + self.major)
        print("學生的名字:" + self.name)
        print("學生的年齡:" + self.age)
        print("學生的性別:" + self.sex)
        #這裡故意嘗試輸出定義在最上層的公有屬性
        print("學生的年級:" + str(Student.grade) ) #grade預設為int數值要用str轉型
        # 這裡故意嘗試輸出定義在最上層的私有屬性
        print("學生的ID號:" + Student.__id)

    #=============以上一般方法都是針對實體(例)化對象之變數進行修改===========

    # 類方法修飾符 又可稱為 "裝飾(器)/函數"
        # @ 符號後面的classmethod就是 decorator(裝飾函數),
        # 而 function updateGrade 跟 showClass
        # 則是被裝飾的物品(decoration).

    #可操作 類之屬性
    @classmethod
    def updateGrade(cls, newGrade):
        cls.grade = newGrade

    @classmethod
    def showClass(cls): #輸出 類之資訊
        print("__name__=" , cls.__name__)
        print("__dict__=" , cls.__dict__)
        print("__class__=" , cls.__class__)








留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題