设计一个名为Stock的类来表示一个公司的股票,包括以下内容:
1)一个名为no的私有字符串数据域表示股票的符号(代码)
2)一个名为name的私有字符串数据域表示股票的符号
3)一个名为previousClosingPrice的四有浮点数据域表示前一天的股票价格
4)一个名为currentPrice的四有浮点数据域表示当前的股票价格
5)一个构造方法创建一支具有特点代码、名字、前一天价格和当前价格的股票
6)一个返回股票名字的get方法
7)一个返回股票代码的get方法
8)获取和设置股票前一天价格的get和set方法
9)获取和设置股票当前价格的get和set方法
10)一个名为getChangePercent()方法返回从previousClosingPrice到currentPrice变化的百分比。
11)编写一个程序,创建一个Stock类,它的代码是601318,名字是中国平安,前一天的价格是63.21,当前价格是64.39,显示该股票的相关信息及价格改变百分比。
class Stock:
def __init__(self,no, name , previousClosingPrice , currentPrice ):
self.__no = no
self.__name = name
self.__previousClosingPrice = previousClosingPrice
self.__currentPrice = currentPrice
def getStockName(self):
return self.__name
def getStockCode(self):
return self.__no
def setStockPreviousClosingPrice(self,previousClosingPrice):
__previousClosingPrice=previousClosingPrice
def getStockPreviousClosingPrice(self):
return self.__previousClosingPrice
def setStockCurrentPrice(self,currentPrice):
__currentPrice = currentPrice
def getStockCurrentPrice(self):
return self.__currentPrice
def getChangePercent(self):
return ((self.__currentPrice-self.__previousClosingPrice)/self.__previousClosingPrice)* 100
if __name__=="__main__":
my_stock =Stock('601318','中国平安',63.21,64.39)
print("股票代码:", my_stock.getStockCode())
print("股票名称:", my_stock.getStockName())
print("previousClosingprice:", my_stock.getStockPreviousClosingPrice())
print("StockCurrentPrice:", my_stock.getStockCurrentPrice())
print("价格变化百分比:{:.2f}%".format(my_stock.getChangePercent()))