⑴ python定义一个Dog类,实例化两个小狗对象。类属性有名字,颜色,体重。实现以下描述
程序写给你
classdogClass():
def__init__(self,name,color,weight):
self.name=name
self.color=color
self.weight=weight
def__del__(self):
self.name=""
self.color=""
self.weight=""
defname(self):
returnself.name
defcolor(self):
returnself.color
defweight(self):
returnself.weight
a=dogClass("旺财","棕色","5斤")
b=dogClass("来福","黑色","8斤")
print("小狗名字叫",a.name,",颜色是",a.color,",体重",a.weight)
print("小狗名字叫",b.name,",颜色是",b.color,",体重",b.weight)
执行结果:
⑵ 编写一个类Stock表示股票,成员变量有: string型symbol,表示股票代码. String型name,表示股票名称. double
private String symbol;//string型symbol,表示股票代码.
private String name;//String型name,表示股票名称.
private double previousClosingPrice;//double型previousClosingPrice,表示上期收盘价
private double currentPrice;//double型currentPrice,表示当前价格.
//下列为Set和get方法
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public String getName() {
return name;
}
public void setNameString(String nameString) {
this.name = nameString;
}
public double getPreviousClosingPrice() {
return previousClosingPrice;
}
public void setPreviousClosingPrice(double previousClosingPrice) {
this.previousClosingPrice = previousClosingPrice;
}
public double getCurrentPrice() {
return currentPrice;
}
public void setCurrentPrice(double currentPrice) {
this.currentPrice = currentPrice;
}
//Stock(String symbol,String name),用来创建股票对象
Stock(String symbol,String name,double previousClosingPrice,double currentPrice) {
this.symbol = symbol;
this.name = name;
this.previousClosingPrice = previousClosingPrice;
this.currentPrice = currentPrice;
}
double changePercent(){
return (currentPrice - previousClosingPrice) /previousClosingPrice;
}
public static void main(String[] args){
Stock stock = new Stock("IBM", "Intermational Business Manufacture Inc",176 , 153);
System.out.println(stock.name+"股票本月同比上月上涨"+stock.changePercent()+"个百分点!");
}
我也是学生啊 可以进行技术交流哦 希望这能帮助到你....