<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      實驗7 面向對象編程與內置模塊

      實驗任務一

      task1:

      源代碼:

       1 class Account:
       2     def __init__(self,name,account_number,initial_amount = 10):
       3         self._name = name
       4         self._card_no = account_number
       5         self._balance = initial_amount
       6 
       7     def deposit(self,amount):
       8         self._balance += amount
       9 
      10     def withdraw(self,amount):
      11         if self._balance < amount:
      12             print('余額不足')
      13             return
      14 
      15         self._balance -= amount
      16 
      17     def info(self):
      18         print('持卡人姓名:',self._name)
      19         print('持卡人賬號:',self._card_no)
      20         print('持卡人賬戶余額:',self._balance)
      21 
      22     def get_balance(self):
      23         return self._balance
      24 
      25 def main():
      26     print('測試賬戶1:'.center(30,'*'))
      27     a1 = Account('Bob','5002311',20000)
      28     a1.deposit(5000)
      29     a1.withdraw(4000)
      30     a1.info()
      31 
      32     print()
      33 
      34     print('測試賬戶2:'.center(30,'*'))
      35     a2 = Account('Joe','5006692',20000)
      36     a2.withdraw(10000)
      37     a2.withdraw(5000)
      38     a2.info()
      39 
      40 if __name__ == '__main__':
      41     main()

      運行結果:

       

      總結:1.創建出來的對象叫做類的實例;創建對象的動作叫做實例化;對象的屬性叫做實例屬性;對象調用的方法叫做實例方法。

          類屬性是給類對象定義的屬性;類屬性用來記錄與這個類相關的特征;類屬性不會用于記錄具體對象的特征。

         2.封裝就是指隱藏對象中一些不希望外部所訪問到的屬性或方法,即為了保證安全。

       

       

      實驗任務二

      task2:
      源代碼:

       1 class Shape():
       2     def info(self):
       3         pass
       4 
       5     def area(self):
       6         pass
       7 
       8     def perimeter(self):
       9         pass
      10 
      11 class Rect(Shape):
      12     def __init__(self,x = 0, y = 0, length = 2,width = 1):
      13         self._x = x
      14         self._y = y
      15         self._width = width
      16         self._length = length
      17 
      18     def info(self):
      19         print(f'矩形左上角頂點坐標:({self._x},{self._y})')
      20         print(f'矩形長:{self._length}')
      21         print(f'矩形寬:{self._width}')
      22 
      23     def area(self):
      24         return self._length * self._width
      25 
      26     def perimeter(self):
      27         return (self._length+self._width)*2
      28 
      29 class Circle(Shape):
      30     def __init__(self,x = 0,y = 0,radius = 1):
      31         self._x = x
      32         self._y = y
      33         self._r = radius
      34 
      35     def info(self):
      36         print(f'圓心:({self._x},{self._y})')
      37         print(f'半徑:{self._r}')
      38 
      39     def area(self):
      40         return 3.14*self._r*self._r
      41 
      42     def perimeter(self):
      43         return 2*3.14*self._r
      44 
      45 class Triangle(Shape):
      46     def __init__(self,a = 1,b = 1,c = 1):
      47         self._a,self._b,self._c = a,b,c
      48 
      49     def info(self):
      50         print(f'三角形邊長:({self._a},{self._b},{self._c})')
      51 
      52     def area(self):
      53         s = (self._a + self._b + self._c)/2
      54         ans = (s*((s-self._a)*(s-self._b)*(s-self._c))**0.5)
      55 
      56         return ans
      57 
      58     def perimeter(self):
      59         return (self._a + self._b + self._c)
      60 
      61 def main():
      62     print('測試1:'.center(40,'*'))
      63 
      64     shapes_lst1 = [Circle(),Rect(),Triangle()]
      65 
      66     for t in shapes_lst1:
      67         t.info()
      68         print(f'面積:{t.area():.2f}')
      69         print(f'周長:{t.perimeter():.2f}')
      70         print()
      71 
      72     print('測試2:'.center(40,'*'))
      73 
      74     shapes_lst2 = [Circle(x = 2, y = 2, radius = 10),
      75                     Rect(x = 50, y = 50,length=10,width=5),
      76                     Triangle(a = 3,b = 4, c = 5)]
      77 
      78     for t in shapes_lst2:
      79         t.info()
      80         print(f'面積:{t.area():.2f}')
      81         print(f'周長:{t.perimeter():.2f}')
      82 
      83 if __name__ == '__main__':
      84     main()

      運行結果:

      總結:1.繼承即一個類可以派生出新的類,而且新的類能繼承基類的成員;多態性是指相同的操作或方法可作用于多種類型的對象,并獲得不同的結果,多態性是通過繼承來實現的。

         2.模塊就好比是工具包,要想使用這個工具包中的工具(就好比函數),就需要導入這個模塊。

      task2-2:

      源代碼:

      1 from Shape import Rect, Circle
      2 
      3 shape_lst = [Rect(5,5,10,5), Circle(), Circle(1,1,10)]
      4 
      5 for i in shape_lst:
      6     i.info()
      7     print(f'面積:{i.area():.2f}')
      8     print(f'周長:{i.perimeter():.2f}')
      9     print()

       

      運行結果:

       

       

       

      實驗任務三

      task3:

      源代碼:

       1 import math
       2 
       3 def func(x):
       4     m = 0
       5     s = 2
       6     fx = 1/(pow(2*math.pi,0.5)*s)*math.exp((-1/2)*pow((x-m)/s,2))
       7     return fx
       8 
       9 def main():
      10     x = [i for i in range(1,10,2)]
      11     for i in x:
      12         print(f'x = {i},f = {func(i):.8f}')
      13 
      14 main()

       

      運行結果:

       

       

      實驗任務四

      task4-1:

      源代碼:

       1 from random import choice
       2 
       3 class RandomWalk():
       4 
       5     def __init__(self,num_points = 5000):
       6 
       7         self.num_points = num_points
       8 
       9         self.x_values = [0]
      10         self.y_values = [0]
      11 
      12     def fill_walk(self):
      13 
      14         while len(self.x_values) < self.num_points:
      15             x_direction = choice([1,-1])
      16             x_distance = choice([0,1,2,3,4])
      17             x_step = x_direction*x_distance
      18 
      19             y_direction = choice([1,-1])
      20             y_distance = choice([0,1,2,3,4])
      21             y_step = y_direction*y_distance
      22 
      23             if x_step == 0 and y_step == 0:
      24                 continue
      25 
      26             next_x = self.x_values[-1] + x_step
      27             next_y = self.y_values[-1] + y_step
      28 
      29             self.x_values.append(next_x)
      30             self.y_values.append(next_y)
      31 
      32 def main():
      33     rw = RandomWalk(5)
      34     rw.fill_walk()
      35     print(rw.x_values)
      36     print(rw.y_values)
      37 
      38 if __name__ == '__main__':
      39     main()

       

      運行結果:

       

       

      task4-2:

      源代碼:

       1 from matplotlib import pyplot as plt
       2 from random_walk import RandomWalk
       3 from time import sleep
       4 
       5 n = 0
       6 while n< 2 :
       7     n += 1
       8 
       9     rw = RandomWalk(50000)
      10     rw.fill_walk()
      11 
      12     plt.figure(figsize = (10,6), dpi = 128)
      13     point_numbers = list(range(rw.num_points))
      14     plt.scatter(rw.x_values,rw.y_values,c = point_numbers,cmap = plt.cm.Blues,edgecolor = 'none',s=1)
      15 
      16     plt.scatter(0,0,c = 'grey',edgecolors = 'none',s = 100)
      17     plt.scatter(rw.x_values[-1],rw.y_values[-1],c = 'red',edgecolors = 'none',s = 100)
      18 
      19     plt.axis('off')
      20 
      21     plt.show()

       

      運行結果:

       

      posted on 2023-06-14 00:53  DTong  閱讀(28)  評論(0)    收藏  舉報

      導航

      主站蜘蛛池模板: 亚洲日本乱码熟妇色精品| 久久精品国产精品亚洲综合| 无码人妻斩一区二区三区| 毛片av在线尤物一区二区| 亚洲V天堂V手机在线| 欧美高清freexxxx性| 国内精品久久人妻无码网站| 久久综合久中文字幕青草| 国产午夜美女福利短视频| 国产欧美VA天堂在线观看视频 | 久久一亚色院精品全部免费| 国产网友愉拍精品视频| 土默特左旗| 免费看一区无码无a片www| 久爱www人成免费网站| 亚洲精品成人区在线观看| 亚洲综合av一区二区三区 | 国产农村激情免费专区| 最新国产精品中文字幕| 国产91麻豆精品成人区| 99久久国产精品无码| 亚洲人成网站在线在线观看| 国产情侣激情在线对白| 国产成人av电影在线观看第一页| 亚洲一区二区三区丝袜| 9丨精品国产高清自在线看| 人妻久久久一区二区三区| 久久这里只精品国产2| 4hu44四虎www在线影院麻豆| 国产v综合v亚洲欧美大天堂| 日韩精品一区二区三区日韩| 亚洲精品无码高潮喷水A| 插入中文字幕在线一区二区三区| 国产精品成人一区二区三区| 国产精品免费中文字幕| 一区二区三区在线 | 欧洲| 国产视频精品一区 日本| 亚洲日韩av无码中文字幕美国| 极品少妇的粉嫩小泬看片| 激情久久av一区二区三区| 国产成人精品一区二区秒拍1o|