实验7 面向对象编程与内置模块

task1

class Account:
    def __init__(self,name,account_number,initial_amount=10):
        self._name=name
        self._card_no=account_number
        self._balance=initial_amount
    def deposit(self,account):
        self._balance+=account
    def withdraw(self,amount):
        if self._balance < amount:
            print('余额不足')
            return
        self._balance-=amount
    def info(self):
        print('持卡人姓名:',self._name)
        print('持卡人账户',self._card_no)
        print('持卡人账户余额:',self._balance)
    def get_balance(self):
        return self._balance
def main():
    print('测试账户1:'.center(30,'*'))
    a1=Account('Bob','5002311',20000)
    a1.deposit(5000)
    a1.withdraw(4000)
    a1.info()

    print()

    print('测试账户2:'.center(30, '*'))
    a2 = Account('Joey', '5006692', 20000)
    a2.deposit(10000)
    a2.withdraw(5000)
    a2.info()
if __name__ == '__main__':
    main()

1.类:一种模板,内含多种函数。

  对象:对象是类的实例。

  属性:每个对象都有的属性,例如''name'' 和 ''account_name''。在创建时属性被赋予特殊的值。

  方法:类中 使用def定义一个函数,该类有什么具体的行为。

  实例化:用类创建一个实例。

2.类的封装性:它指的是将对象的属性和方法封装在一起,并限制外部访问这些属性和方法的方式。

task2

from shape import circle,rect
shape_ls=[rect(5,5,10,5),circle(),circle(1,1,10)]
for i in shape_ls:
    i.info()
    print(f'面积: {i.area(): .2f}')
    print(f'周长: {i.perimeter(): .2f}')
    print()

1.类的继承:子类会继承父类的方法和属性。

 多态特性:是指具有不同功能的函数可以使用相同的函数名,这样就可以用一个函数名调用不同内容的函数。

2.模块:对代码更高级的封装,包含很多类,函数等,导入模块可避免重复性编写。

task3

from math import *
def func(x,m=0,s=2):
    y=(1/(s*((2*pi)**0.5)))*exp(-0.5*(((x-m)/s)**2))
    return y
for i in range(1,10,2):
    print(f'x = {i},f = {func(i):

task4

from random import choice
class RandomWalk():

    def __init__(self, num_points = 5000):

        self.num_points = num_points

        self.x_values = [0]
        self.y_values = [0]
    def fill_walk(self):

        while len(self.x_values) < self.num_points:

            x_direction = choice([1, -1])
            x_distance = choice([0, 1, 2, 3, 4])
            x_step = x_direction * x_distance
            y_direction = choice([1, -1])
            y_distance = choice([0, 1, 2, 3, 4])
            y_step = y_direction * y_distance
# 据绝原地踏步
            if x_step == 0 and y_step == 0:
                continue
# 计算下一个点的x和y值
            next_x = self.x_values[-1] + x_step
            next_y = self.y_values[-1] + y_step
            self.x_values.append(next_x)
            self.y_values.append(next_y)
def main():

    rw = RandomWalk(5)
    rw.fill_walk()
    print(rw.x_values)
    print(rw.y_values)
if __name__ == '__main__':
    main()
random_walk
from matplotlib import pyplot as plt
from random_walk import RandomWalk
from time import sleep

n = 0
while n < 2:
    n += 1
    rw = RandomWalk(50000)
    rw.fill_walk()
    plt.figure(figsize = (10, 6), dpi = 128)
    point_numbers = list(range(rw.num_points))
    plt.scatter(rw.x_values, rw.y_values, c=point_numbers,cmap=plt.cm.Blues,edgecolor='none',s=1)
    plt.scatter(0, 0, c = 'grey', edgecolors='none', s=100)
    plt.scatter(rw.x_values[-1], rw.y_values[-1],c = 'red',edgecolors='none',s=100)
    plt.axis('off')
    plt.show()
View Code

 

 

posted @ 2023-06-07 11:09  AurTw1  阅读(25)  评论(0)    收藏  举报