java实验stock类
时间: 2023-12-30 16:23:03 浏览: 91
java测试类
以下是Java实验Stock类的示例代码:
1. Stock类的定义
```java
public class Stock {
String symbol;
String name;
double previousPrice;
double currentPrice;
public Stock(String symbol, String name) {
this.symbol = symbol;
this.name = name;
}
public double getChangePercent() {
return ((currentPrice - previousPrice) / previousPrice) * 100;
}
}
```
2. TestStock类的使用
```java
public class TestStock {
public static void main(String[] args) {
Stock pfyh = new Stock("600000", "浦发银行");
pfyh.previousPrice = 25.50;
pfyh.currentPrice = 28.60;
System.out.println(pfyh.name);
System.out.println(pfyh.symbol);
System.out.printf("该股票的市值变化百分比为:" + "%.2f", pfyh.getChangePercent());
System.out.println("%");
}
}
```
阅读全文