⑴ 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()+"個百分點!");
}
我也是學生啊 可以進行技術交流哦 希望這能幫助到你....