实验题目: 1、分别实现产品类和用户类,产品对象包含产品名称、产品重量和产品价格等;用户对象包含用户名称;用户对象可以向产品对象询问价格,产品对象接到询价时会回答该产品的价格。 2、类图 3、序列图
时间: 2024-01-27 21:04:33 浏览: 35
3类与对象基础实验(一)
1. 代码实现:
```
class Product:
def __init__(self, name, weight, price):
self.name = name
self.weight = weight
self.price = price
def get_price(self):
return self.price
class User:
def __init__(self, name):
self.name = name
def ask_price(self, product):
return product.get_price()
```
2. 类图:
```
+----------+ +-----------+
| Product | | User |
+----------+ +-----------+
| name | | name |
| weight | +-----------+
| price |
+----------+
| get_price()|
+----------+
| ask_price()|
+-----------+
```
3. 序列图:
```
+-----------+ +----------+
| User | | Product |
+-----------+ +----------+
| |
| ask_price(product) |
|------------------------>|
| |
| get_price() |
|<------------------------|
| |
```
阅读全文