python 【问题描述】 设计一个名为Stock的类来表示一个公司的股票,包括以下内容: 1)股票代码、股票名称、前一天股票价格、当天股票价格4个私有属性; 2)构造方法,需初始化代码、名称、前一天价格和当天价格等属性; 3)返回股票名字的get方法; 4)返回股票代码的get方法; 5)获取和设置股票前一天价格的get和set方法; 6)获取和设置股票当前价格的get和set方法; 7)名为getChangePercent()方法,返回前日收市价至当前价格的变化百分比; 8). 包括文档字符串,其内容为:"Stock Information Class" 说明:各方法的名称及参数要求请参见测试程序。 【样例输入】 601318 Chinese PINAN 63.21 64.39 【样例输出】 Code:601318 Name:Chinese PINAN Price/Yesterday:63.21 Price/Today:64.39 Edit Price/Yesterday To:50.25 Price Change Percentage:28.14% Stock Information Class #class Stock sCode = input() #Stock Code sName = input() #Stock Name priceYesterday = float(input()) #Price/Yesterday priceToday = float(input()) #Price Today s = Stock(sCode,sName,priceYesterday,priceToday) print("Code:",s.getCode()) print("Name:",s.getName()) print("Price/Yesterday:%.2f\nPrice Today:%.2f" % (s.getPriceYesterday(),s.getPriceToday())) s.setPriceYesterday(50.25) print("Edit Price/Yesterday To:%.2f" % 50.25) print("Price Change Percentage:%.2f%%" % (s.getChangePercent()*100)) print(Stock.__doc__)
时间: 2024-03-10 21:44:17 浏览: 138
```python
class Stock:
"""
Stock Information Class
Attributes:
_code (str): Stock code
_name (str): Stock name
_price_yesterday (float): Yesterday's stock price
_price_today (float): Today's stock price
"""
def __init__(self, code, name, price_yesterday, price_today):
self._code = code
self._name = name
self._price_yesterday = price_yesterday
self._price_today = price_today
def getCode(self):
"""
Get stock code
Returns:
str: Stock code
"""
return self._code
def getName(self):
"""
Get stock name
Returns:
str: Stock name
"""
return self._name
def getPriceYesterday(self):
"""
Get yesterday's stock price
Returns:
float: Yesterday's stock price
"""
return self._price_yesterday
def setPriceYesterday(self, price):
"""
Set yesterday's stock price
Args:
price (float): Yesterday's stock price
"""
self._price_yesterday = price
def getPriceToday(self):
"""
Get today's stock price
Returns:
float: Today's stock price
"""
return self._price_today
def setPriceToday(self, price):
"""
Set today's stock price
Args:
price (float): Today's stock price
"""
self._price_today = price
def getChangePercent(self):
"""
Get the change percentage from yesterday's closing price to today's price
Returns:
float: The change percentage
"""
return (self._price_today - self._price_yesterday) / self._price_yesterday
sCode = input() # Stock Code
sName = input() # Stock Name
priceYesterday = float(input()) # Price/Yesterday
priceToday = float(input()) # Price Today
s = Stock(sCode, sName, priceYesterday, priceToday)
print("Code:", s.getCode())
print("Name:", s.getName())
print("Price/Yesterday:%.2f\nPrice Today:%.2f" % (s.getPriceYesterday(), s.getPriceToday()))
s.setPriceYesterday(50.25)
print("Edit Price/Yesterday To:%.2f" % 50.25)
print("Price Change Percentage:%.2f%%" % (s.getChangePercent()*100))
print(Stock.__doc__)
```
阅读全文