python——物件導向

來源:互聯網
上載者:User

標籤:float   根據   close   建立   launch   enter   定義   height   form   

一、面向過程,是按照問題解決步驟,一步一步的進行,思路比較簡單,但是不易維護,物件導向編程是將問題分塊,各個模組搭建最後的總的解決方案

1、鉛球問題的物件導向編程

 1 from math import sin, cos, radians 2  #建立類 3 class Projectile: 4     def __init__(self, angle, velocity, height): 5          6         #建立狀態,根據給定的發射角度、初始速度和位置建立一個投射體對象 7         self.xpos = 0.0 8         self.ypos = height 9         theta = radians(angle)10         self.xvel = velocity * cos(theta)11         self.yvel = velocity * sin(theta)12 13     #建立類的方法14     def update(self, time):15         #更新投射體的狀態16         self.xpos = self.xpos + time * self.xvel17         yvell = self.yvel - 9.8 * time18         self.ypos = self.ypos + time * (self.yvel + yvell) / 2.019         self.yvel = yvell20  21     def getY(self):22         #返回投射體的角度23         return self.ypos24  25     def getX(self):26         #返回投射體的距離27         return self.xpos

調用類

 1 from Projectile import * 2   3 def getInputs(): 4     a = eval(input("Enter the launch angle (in degrees):")) 5     v = eval(input("Enter the initial velocity (in meters/sec):")) 6     h = eval(input("Enter the initial height (in meters):")) 7     t = eval(input("Enter the time interval: ")) 8     return a,v,h,t 9  10 def main():11     angle,vel,h0,time = getInputs()#輸入參數12     shot = Projectile(angle,vel,h0)#調用類,注意只用三個參數13     while shot.getY() >=0:14         shot.update(time)15     print("\nDistance traveled:{0:0.1f}meters.".format(shot.getX()))16    17 if __name__ == "__main__":18     main()

2、GPA

 1 # 找到GPA最高的學生 2  3  #定義類 4 class Student: 5     def __init__(self, name, hours, qpoints): 6         self.name = name 7         self.hours = float(hours) 8         self.qpoints = float(qpoints) 9         10     #定義方法 11     def getName(self):12         return self.name13      14     def getHours(self):15         return self.hours16      17     def getQPoints(self):18         return self.qpoints19      20     def gpa(self):21         return self.qpoints/self.hours22 23 #定義函數,擷取每一行資料   24 def makeStudent(infoStr):25     name, hours, qpoints = infoStr.split("\t")26     return Student(name, hours, qpoints)27      28 def main():29     # 開啟輸入檔案30     filename = input("Enter name the grade file: ")31     infile = open(filename, ‘r‘)32     # 設定檔案中第一個學生的記錄為best33     best = makeStudent(infile.readline())34  35     # 處理檔案剩餘行資料36     for line in infile:37         # 將每一行資料轉換為一個記錄38         s = makeStudent(line)39         # 如果該學生是目前GPA最高的,則記錄下來40         if s.gpa() > best.gpa():41             best = s42     infile.close()43  44     # 列印GPA成績最高的學生資訊45     print("The best student is:", best.getName())46     print("hours:", best.getHours())47     print("GPA:", best.gpa())48  49 if __name__ == ‘__main__‘:50     main()

 

python——物件導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.