用java写一段关于多功能手机
时间: 2023-12-31 18:03:38 浏览: 27
好的,以下是一个简单的关于多功能手机的Java代码示例:
```java
public class MultiFunctionalPhone {
private String brand;
private String model;
private boolean isOn;
private int batteryLevel;
public MultiFunctionalPhone(String brand, String model) {
this.brand = brand;
this.model = model;
this.isOn = false;
this.batteryLevel = 100;
}
public void turnOn() {
this.isOn = true;
System.out.println(this.brand + " " + this.model + " is now turned on.");
}
public void turnOff() {
this.isOn = false;
System.out.println(this.brand + " " + this.model + " is now turned off.");
}
public void makeCall(String phoneNumber) {
if (this.isOn && this.batteryLevel > 0) {
System.out.println("Dialing " + phoneNumber + "...");
this.batteryLevel -= 5;
System.out.println("Call ended. Battery level: " + this.batteryLevel + "%");
} else if (!this.isOn) {
System.out.println("Cannot make a call. Phone is turned off.");
} else {
System.out.println("Cannot make a call. Battery level is too low.");
}
}
public void sendTextMessage(String phoneNumber, String message) {
if (this.isOn && this.batteryLevel > 0) {
System.out.println("Sending text message to " + phoneNumber + "...");
this.batteryLevel -= 2;
System.out.println("Message sent. Battery level: " + this.batteryLevel + "%");
} else if (!this.isOn) {
System.out.println("Cannot send text message. Phone is turned off.");
} else {
System.out.println("Cannot send text message. Battery level is too low.");
}
}
public void takePhoto() {
if (this.isOn && this.batteryLevel > 0) {
System.out.println("Taking photo...");
this.batteryLevel -= 10;
System.out.println("Photo taken. Battery level: " + this.batteryLevel + "%");
} else if (!this.isOn) {
System.out.println("Cannot take photo. Phone is turned off.");
} else {
System.out.println("Cannot take photo. Battery level is too low.");
}
}
public void playMusic(String song) {
if (this.isOn && this.batteryLevel > 0) {
System.out.println("Playing " + song + "...");
this.batteryLevel -= 8;
System.out.println("Music stopped. Battery level: " + this.batteryLevel + "%");
} else if (!this.isOn) {
System.out.println("Cannot play music. Phone is turned off.");
} else {
System.out.println("Cannot play music. Battery level is too low.");
}
}
}
```
这个类包含了一些基本的手机功能,例如开关机、拨打电话、发送短信、拍照和播放音乐。当手机被打开时,可以执行这些功能。当手机的电池电量不足时,某些功能可能无法执行。
您可以使用这个类作为基础,添加更多的功能和特性,以创建一个更全面的多功能手机应用程序。