Java定义一个手机接口类,不同品牌的生产手机功能不一样如照相听歌导航手电筒等等 华为:打电话,发短信,照相,听歌 * 小米:打电话,发短信,导航,手电筒 OPPO:电话,发短信,听歌,导航
时间: 2024-05-03 16:18:17 浏览: 139
可以定义一个手机接口类,如下所示:
```java
public interface Phone {
void call();
void sendMessage();
void takePhoto();
}
```
然后,我们可以定义不同品牌的手机类来实现这个接口,并添加各自的功能,如下所示:
```java
public class HuaweiPhone implements Phone {
public void call() {
System.out.println("Huawei Phone call.");
}
public void sendMessage() {
System.out.println("Huawei Phone send message.");
}
public void takePhoto() {
System.out.println("Huawei Phone take photo.");
}
public void listenMusic() {
System.out.println("Huawei Phone listen music.");
}
}
public class XiaomiPhone implements Phone {
public void call() {
System.out.println("Xiaomi Phone call.");
}
public void sendMessage() {
System.out.println("Xiaomi Phone send message.");
}
public void takePhoto() {
System.out.println("Xiaomi Phone take photo.");
}
public void navigate() {
System.out.println("Xiaomi Phone navigate.");
}
public void flashlight() {
System.out.println("Xiaomi Phone flashlight.");
}
}
public class OppoPhone implements Phone {
public void call() {
System.out.println("Oppo Phone call.");
}
public void sendMessage() {
System.out.println("Oppo Phone send message.");
}
public void takePhoto() {
System.out.println("Oppo Phone take photo.");
}
public void listenMusic() {
System.out.println("Oppo Phone listen music.");
}
public void navigate() {
System.out.println("Oppo Phone navigate.");
}
}
```
这样,不同品牌的手机就可以通过实现同一个接口来实现不同的功能。
阅读全文