java创建一个游戏机类,其中包含私有属性品牌、型号和价格, 然后创建方法去访问和修改这些属性的测试类
时间: 2024-06-09 07:10:41 浏览: 5
下面是一个Java创建游戏机类及测试类的示例代码:
```
public class GameConsole {
private String brand;
private String model;
private double price;
public GameConsole(String brand, String model, double price) {
this.brand = brand;
this.model = model;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
测试类:
```
public class GameConsoleTest {
public static void main(String[] args) {
GameConsole console = new GameConsole("Nintendo", "Switch", 299.99);
System.out.println("Brand: " + console.getBrand());
System.out.println("Model: " + console.getModel());
System.out.println("Price: " + console.getPrice());
console.setPrice(249.99);
System.out.println("New price: " + console.getPrice());
}
}
```
运行测试类,输出结果应该如下:
```
Brand: Nintendo
Model: Switch
Price: 299.99
New price: 249.99
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)