设计一个名为Stock的类来表示一个公司的股票,包括以下内容:
1)股票代码、股票名称、前一天股票价格、当天股票价格4个私有属性;
2)构造方法,需初始化代码、名称、前一天价格和当天价格等属性;
3)返回股票名字的get方法;
4)返回股票代码的get方法;
5)获取和设置股票前一天价格的get和set方法;
6)获取和设置股票当前价格的get和set方法;
7)名为getChangePercent()方法,返回前日收市价至当前价格的变化百分比;
8). 包括文档字符串,其内容为:"Stock Information Class"
说明:各方法的名称及参数要求请参见测试程序。
类接口定义:
classStock ...
裁判测试程序样例:
sCode = input() #输入代码sName = input() #输入名称priceYesterday = float(input()) #输入昨日价格priceToday = float(input()) #输入今日价格s = Stock(sCode,sName,priceYesterday,priceToday)
print("代码:",s.getCode())
print("名称:",s.getName())
print("昨日价格:%.2f\n今天价格:%.2f" % (s.getPriceYesterday(),s.getPriceToday()))
s.setPriceYesterday(50.25)
print("修正昨日价格为:%.2f" % 50.25)
print("价格变化百分比:%.2f%%" % (s.getChangePercent()*100))
print(Stock.__doc__)
输入样例:
601318
中国平安
63.21
64.39
输出样例:
代码: 601318
名称: 中国平安
昨日价格:63.21
今天价格:64.39
修正昨日价格为:50.25
价格变化百分比:28.14%
Stock Information Class
其中答案之一;
class Stock:
"""Stock Information Class"""
def __init__(self, sCode, sName, priceYesterday, priceToday):
self.__scode = sCode
self.__sname = sName
self.__priceyesterday = priceYesterday
self.__pricetoday = priceToday
def getCode(self):
return self.__scode
def set_scode(self,scode):
self.__scode = scode
def getName(self):
return self.__sname
def set_sname(self, sname):
self.__sname = sname
def getPriceYesterday(self):
return self.__priceyesterday
def set_priceyesterday(self, priceyesterday):
self.__priceyesterday = priceyesterday
def getPriceToday(self):
return self.__pricetoday
def set_pricetoday(self, pricetoday):
self.__pricetoday = pricetoday
def setPriceYesterday(self,newy):
self.__priceyesterday = newy
def getChangePercent(self):
return (self.__pricetoday - self.__priceyesterday) / self.__priceyesterday
执行结果: