电竞比分网-中国电竞赛事及体育赛事平台

分享

python類相關(guān)的練習(xí)題_python關(guān)于類的練習(xí)題

 沒有開始 2023-10-10 發(fā)布于重慶

1、寫一個(gè)矩形的類,那么這個(gè)類默認(rèn)有長(zhǎng)和寬兩個(gè)參數(shù),當(dāng)傳入一個(gè)叫square的屬性時(shí),值就等于邊長(zhǎng),由于是正方形那么長(zhǎng)和寬就等于邊長(zhǎng)。

class Rectangle(object):
  
    # init object
    def __init__(self,width,height):
        self.width = width
        self.height = height
  
  
    def getacreage(self):
        return self.width * self.height
  
    def __setattr__(self,name,value):
        if name == 'square':
            self.width = value
            self.height = value
  
        else:
            super().__setattr__(name,value)

2、完成如下要求:

  • 定制一個(gè)計(jì)時(shí)器類

  • start和stop方法代表啟動(dòng)和停止計(jì)時(shí)

  • 假設(shè)計(jì)時(shí)器對(duì)象t1,定制t1對(duì)象直接執(zhí)行和打印時(shí)的輸出信息

  • 當(dāng)計(jì)時(shí)器未啟動(dòng)或已經(jīng)停止計(jì)時(shí),調(diào)用stop方法會(huì)給予溫馨提示

  • 兩個(gè)計(jì)時(shí)器對(duì)象可以進(jìn)行相加: t1 + t2

import time
  
class Mytimer():
  
    # 初始化
    def __init__(self):
        self.prompt = 'Timer is not start'    # 提示信息
        self.start_time = None 
        self.stop_time = None
      
    # 啟動(dòng)定時(shí)器
    def start(self):
        if self.start_time:
            print('Please Use Stop() to stop Timer')   # 屬性存在,表示已經(jīng)啟動(dòng)計(jì)時(shí)
        else:
            self.start_time = time.time()
            print('The start of Timer')
  
    # 關(guān)閉定時(shí)器
    def stop(self):
        if self.start_time:
            self.stop_time = time.time()
            print('The stop of Timer')
            self._calc()
        else:
         print('Please Use Start() to start Timer')   # 沒有執(zhí)行start(),檢測(cè)不到屬性,進(jìn)行提示
  
    # 計(jì)算耗時(shí)
    def _calc(self):
        self.prompt = 'Total time : '
        self.end_time = self.stop_time - self.start_time
        self.prompt += str('{:.2f}'.format(self.end_time))
        self.start_time = None
        self.stop_time = None
  
    # 直接執(zhí)行對(duì)象時(shí),打印提示信息
    def __repr__(self):
        return self.prompt
  
    # 打印對(duì)象和執(zhí)行對(duì)象相同
    __str__ = __repr__
  
    # 計(jì)時(shí)器相加
    def __add__(self,other):
        self.prompt = 'All Time : '
        add_time = self.end_time + other.end_time
        self.prompt += str('{:.2f}'.format(add_time))
        return self.prompt

3、完成如下需求

  • 定義一個(gè)溫度類,然后定義兩個(gè)描述符類用于描述攝氏度和華氏度兩個(gè)屬性。
  • 兩個(gè)屬性會(huì)自定進(jìn)行轉(zhuǎn)換,也就是說可以給攝氏度這個(gè)屬性賦值,然后打印的華氏度屬性是自動(dòng)轉(zhuǎn)換后的結(jié)果。
class Celsius:
  
    def __init__(self,value = 37):
        self.value = value
  
    def __set__(self,instance,value):
        self.value = value
  
    def __get__(self,instance,owner):
        return self.value
  
  
class Fahrenheit:
  
    def __set__(self,instance,value):
         instance.cel = (float(value) - 32 )  / 1.8
           
    def __get__(self,instance,owner):
        return instance.cel * 1.8  + 32   # 通過instance實(shí)例訪問,實(shí)例化后對(duì)象的cel屬性
  
class Temperature:
    cel = Celsius()
    fah = Fahrenheit()
  
#Python小白學(xué)習(xí)交流群:725638078      
>>> a = Temperature()
>>> a.cel
37
>>> a.fah
98.60000000000001
>>> a.fah = 100
>>> a.cel
37.77777777777778
>>>

由于instance表示的是實(shí)例化的對(duì)象本身(a),所以這里使用instance,指代a,來訪問設(shè)置的cel屬性,來進(jìn)行換算。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多