class Account(object): def __init__(self, id, balance): self.id = id self.balance = balance def deposit(self, inMoney): try: assert inMoney > 0 self.balance += inMoney print("你向账户成功存入" + str(inMoney) + "元!") except: print("存入的钱必须大于0!") def withdraw(self, outMoney): try: assert outMoney > 0 assert outMoney <= self.balance self.balance -= outMoney print("你已经成功取出" + str(outMoney) + "元!") except AssertionError: if outMoney <= 0: print("取出的钱必须大于0!") else: print("余额不足,无法完成取款操作!") # 测试取款功能 myAccount = Account("123456", 1000) myAccount.withdraw(200) print(myAccount.balance)
时间: 2024-02-15 13:27:06 浏览: 37
这是一个 Python 类,名为 Account,它有三个方法:\_\_init\_\_、deposit 和 withdraw。
\_\_init\_\_ 方法是构造方法,用于初始化类的实例。它接收两个参数:id 和 balance,分别表示账户的 ID 和余额。在该方法中,将这两个参数分别赋值给实例属性 self.id 和 self.balance。
deposit 方法用于向账户存款。它接收一个参数 inMoney,表示存入的金额。在该方法中,首先使用 assert 来判断 inMoney 是否大于 0,如果不是,则会抛出异常。如果 inMoney 大于 0,则将其加到 self.balance 上,并输出一条成功存款的信息。
withdraw 方法用于从账户取款。它接收一个参数 outMoney,表示取出的金额。在该方法中,首先使用 assert 来判断 outMoney 是否大于 0 和是否小于等于 self.balance,如果不是,则会抛出异常。如果 outMoney 大于 0 且小于等于 self.balance,则将其从 self.balance 中减去,并输出一条成功取款的信息。如果 outMoney 不大于 0,则输出一条取款金额必须大于 0 的信息。如果 outMoney 大于 0 但是大于 self.balance,则输出一条余额不足无法完成取款的信息。
最后,代码创建了一个 Account 类的实例 myAccount,并对其进行了一次取款操作,取出了 200 元。最后输出了账户的余额。
相关问题
优化代码class DeliveryRequest: def __init__(self, name, location, time): self.name = name self.location = location self.time = time class DeliveryStation: def __init__(self, location): self.location = location self.requests = [] def add_request(self, request): self.requests.append(request) def match_deliveryman(self, deliverymen): for request in self.requests: for deliveryman in deliverymen: if request.location == deliveryman.location and request.time == deliveryman.available_time: print(f"您有一个快递将送给{request.name},请您按时送货") print(f"您的快递将由{deliveryman.name}在{request.time}送达,请您按时等候") deliverymen.remove(deliveryman) break class DeliveryMan: def __init__(self, name, location, available_time): self.name = name self.location = location self.available_time = available_time class Dispatcher: def __init__(self, delivery_stations, deliverymen): self.delivery_stations = delivery_stations self.deliverymen = deliverymen def dispatch(self): for station in self.delivery_stations: station.match_deliveryman(self.deliverymen)
可以考虑优化代码的可读性和效率,例如使用列表推导式和更好的命名:
```python
class DeliveryRequest:
def __init__(self, name, location, time):
self.name = name
self.location = location
self.time = time
class DeliveryStation:
def __init__(self, location):
self.location = location
self.requests = []
def add_request(self, request):
self.requests.append(request)
def match_deliveryman(self, deliverymen):
for request in self.requests:
matching_deliverymen = [dm for dm in deliverymen if dm.location == request.location and dm.available_time == request.time]
for deliveryman in matching_deliverymen:
print(f"您有一个快递将送给{request.name},请您按时送货")
print(f"您的快递将由{deliveryman.name}在{request.time}送达,请您按时等候")
deliverymen.remove(deliveryman)
break
class DeliveryMan:
def __init__(self, name, location, available_time):
self.name = name
self.location = location
self.available_time = available_time
class Dispatcher:
def __init__(self, delivery_stations, deliverymen):
self.delivery_stations = delivery_stations
self.deliverymen = deliverymen
def dispatch(self):
for station in self.delivery_stations:
station.match_deliveryman(self.deliverymen)
```
这样,我们使用列表推导式来创建了一个匹配的快递员列表,然后使用一个 for 循环来遍历它。我们也使用更好的变量名来提高代码的可读性。
class Person: def __init__(self, name, age): self.name = name self.age = age
def introduce(self):
print(f"My name is {self.name} and I am {self.age} years old.")
class Student(Person):
def __init__(self, name, age, major):
super().__init__(name, age)
self.major = major
def study(self):
print(f"I am studying {self.major}.")
class Teacher(Person):
def __init__(self, name, age, subject):
super().__init__(name, age)
self.subject = subject
def teach(self):
print(f"I am teaching {self.subject}.")
s = Student("John", 20, "Computer Science")
s.introduce() # Output: My name is John and I am 20 years old.
s.study() # Output: I am studying Computer Science.
t = Teacher("Jane", 35, "Math")
t.introduce() # Output: My name is Jane and I am 35 years old.
t.teach() # Output: I am teaching Math.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)