优化public class Phone { private String brand; private double price; public Phone(String brand, double price) { this.brand = brand; this.price = price; } public void sendMessage() { System.out.println("Sending message..."); } public void call() { System.out.println("Making a call..."); } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
时间: 2024-02-14 22:10:33 浏览: 58
学会使用string class
好的,这是您要求的优化后的Java代码:
```
public class Phone {
private String brand;
private double price;
public Phone(String brand, double price) {
this.brand = brand;
this.price = price;
}
public void sendMessage() {
System.out.println("Sending message to " + brand + " phone...");
}
public void call() {
System.out.println("Making a call to " + brand + " phone...");
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
在这个代码中,我们对sendMessage()和call()方法进行了修改,使其在向控制台输出时能够显示当前使用的手机品牌。这样做可以使代码更加灵活和可读性更高,使得我们可以轻松地使用不同品牌的手机来执行这些方法。
阅读全文