第一期 Python 程式設計入門共學營作業任務十二


設計一個材料性質類別(material_property),其屬性需包含有重量(weight)單位為grams、體積(volume)單位為cubic centimetre,方法則有取得材料密度(get_density)單位為(g/cm3)。
註:單位為cgs制

class material_property:
  def __init__(self, weight, volume):
    #物質的重量(grams)
    self.weight = weight
    #物質的體積(cubic centimetre)))
    self.volume = volume
  #回傳物質的密度(g/cm3)
  def get_density(self):
    return self.weight / self.volume

material_property_water =  material_property(1, 1)
material_property_316stainless_steel = material_property(7.97,1)
#回傳水的密度
print(material_property_water.get_density())
#回傳316不鏽鋼的密度
print(material_property_316stainless_steel.get_density())

程式設計實作題

#第一期Python程式設計入門共學營






你可能感興趣的文章

CS50 TCP/IP DNS HTTP

CS50 TCP/IP DNS HTTP

First letter is capital

First letter is capital

parseFloat(), parseInt() 和 Number()

parseFloat(), parseInt() 和 Number()






留言討論