1. 程式人生 > >[18/11]Python類的

[18/11]Python類的

這週上課又學回到了Python面向物件程式設計以及類的封裝等,這裡寫個關於類的應用, 也不是很難,不過相對於基礎來說以及夠夠了。

寫一個類,通過這個工具類能夠畫一個長方形(用*畫)工具類:
長方形類:
屬性:
    寬 private double width
    高 private double height
    面積private double area
    周長private double perimeter
    是不是空心 private boolean empty
方法:
    通過邊長計算面積
    通過邊長計算周長
    獲取一個長方形:寬度、高度、面積、周長、是否空心的方法(get方法)
    修改一個長方形:寬度、高度、是否空心的方法(set方法)
    繪製圖形方法:通過屬性判斷是否空心,然後進行圖形繪製

首先定義一個類,名稱就簡單明瞭地用長方形的縮寫好了。

import math

Class Cfx():

要求裡對屬性的型別有要求,定義下屬性,之後建立構造器,給屬性賦初值。

Class Cfx():

	__width = 0.0
	__height = 0.0
	__area = 0.0
	__perimeter = 0.0
	__empty = True
	
	def __init__(self,width,height):
		self.__width = width
		self.__height = height

接下來是方法的建立,第一個方法要求計算長方形的面積,根據面積的定義可知area=width*height

def Area(self):
	self.__area = self.__width*self.__height
	return self.__area

第二個方法是求長方形的周長,定義可知perimeter=(width+height)*2

def Perimeter(self):
	self.__perimeter = self.__width*2+self.__height*2
	return self.__perimeter

第三個方法是獲取長方形的屬性,這裡我通過屬性值的配對來返回相對應的結果。要注意輸出時都要轉成string型,否則會報錯。

def __get__(self,key):
    if key =="width":
        return "寬度:" + str(self.__width)
    if key =="height":
        return "\n高度:"+str(self.__height)
    if key == "area":
        return "\n面積:"+str(self.Area())
    if key =="perimeter":
        return "\n周長:"+str(self.Perimeter())
    if key =="empty":
        return "\n是否空心:"+str(self.__empty)

如果要返回所有屬性,則用get_all方法。

def get_all(self):
    return "寬度:" + str(self.__width)+"\n高度:"+str(self.__height)+"\n面積:"+str(self.Area())+ "\n周長:"+str(self.Perimeter())+"\n是否空心:"+str(self.__empty)

第四個方法是改變相應屬性的值,這裡也是通過屬性名的配對來修改。

def __set__(self,key,value):
    if key == "width":
        self.__width = value
    if key == "height":
        self.__height = value
    if key == "empty":
        self.__empty = value

第五個方法是畫出長方形,這裡首先要注意長方形的長寬可能是小數,然後要判斷是不是空心。 對於帶小數的長方形,我考慮的是將一個*代表的大小改變來畫圖。

def Paint(self):
    w = self.__width
    h = self.__height
    tempw = w - math.floor(w)
    temph = h - math.floor(h)
    if temph > 0 or tempw > 0:#小數部分
        index = 0
        while tempw * (10 ** index) < 1: #迴圈直到小數部分大於1為之,最終的index決定一個*代表的大小。
            wi = index
            index += 1

        while temph * (10 ** index) < 1:
            hi = index
            index += 1

        if wi >= hi: #比較長寬哪個的小數更小,選擇小的來作為*所代表的大小
            whi = wi
        else:
            whi = hi
        standard = 10 ** (-whi)
        print("每個*號代表單位長度%f" % standard) #圖注
        w = w * (10 ** whi)
        h = h * (10 ** whi)

之後是判斷是否空心以及畫圖,這裡邏輯比較簡單,就不詳細解釋了。

if self.__empty == True:

    i = 1
    while i<=h:
        if i > 1 and i < h:
            j = 1
            while j <= w:
                if j == 1 or j==w:
                    print("*", end="")
                if j>1 and j<w:
                    print(end=" ")
                j += 1
            print()

        if i == 1 or i == h:
            j = 1
            while j <= w:
                print("*", end="")
                j += 1
            print()
        i += 1

else:
    i = 1
    while i<=h:
        j = 1
        while j<=w:
            print("*",end="")
            j += 1
        i += 1
        print()

最後是對這個類的測試,測試資料如下: 在這裡插入圖片描述 在這裡插入圖片描述

在這裡插入圖片描述 圖太長了放不下,不過沒什麼問題。