init
This commit is contained in:
69
day01/src/com/inmind/test10/Smartphone.java
Normal file
69
day01/src/com/inmind/test10/Smartphone.java
Normal file
@@ -0,0 +1,69 @@
|
||||
package com.inmind.test10;
|
||||
|
||||
// 智能手机类 - 实现两个接口
|
||||
public class Smartphone extends Device implements Networkable, Chargeable {
|
||||
private int storage; // 存储容量(GB)
|
||||
private int batteryLevel; // 电池电量(%)
|
||||
|
||||
public Smartphone(String brand, String model, double price, int storage) {
|
||||
super(brand, model, price);
|
||||
this.storage = storage;
|
||||
this.batteryLevel = 50; // 初始电量50%
|
||||
}
|
||||
|
||||
@Override
|
||||
public void introduce() {
|
||||
System.out.println("这是一部" + brand + " " + model + "智能手机,价格" + price +
|
||||
"元,存储容量" + storage + "GB");
|
||||
}
|
||||
|
||||
// 特有方法:拍照
|
||||
public void takePhoto() {
|
||||
if (isPowerOn()) {
|
||||
System.out.println(brand + " " + model + " 正在拍照");
|
||||
batteryLevel -= 5; // 拍照消耗电量
|
||||
} else {
|
||||
System.out.println("请先开机再拍照");
|
||||
}
|
||||
}
|
||||
|
||||
// 实现Networkable接口方法
|
||||
@Override
|
||||
public void connectToNetwork(String networkName) {
|
||||
if (isPowerOn() && Networkable.isNetworkAvailable()) {
|
||||
System.out.println(brand + " " + model + " 已连接到 " + networkName);
|
||||
} else if (!isPowerOn()) {
|
||||
System.out.println("请先开机再连接网络");
|
||||
} else {
|
||||
System.out.println("网络不可用,无法连接到 " + networkName);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnectFromNetwork() {
|
||||
System.out.println(brand + " " + model + " 已断开网络连接");
|
||||
}
|
||||
|
||||
// 实现Chargeable接口方法
|
||||
@Override
|
||||
public void charge(int minutes) {
|
||||
if (minutes <= 0) {
|
||||
System.out.println("充电时间必须为正数");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println(brand + " " + model + " 正在充电" + minutes + "分钟");
|
||||
int chargeAmount = minutes / 2; // 每2分钟充1%
|
||||
batteryLevel += chargeAmount;
|
||||
|
||||
if (batteryLevel > 100) {
|
||||
batteryLevel = 100;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBatteryPercentage() {
|
||||
return batteryLevel;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user