实现手机类,电池类和测试类
参考代码:
import java.util.Scanner;
class Phone{
String brand;
String music;
int power=100;
Phone(String brand,int power){
this.power=power;
this.brand=brand;
}
void downloadMusic(String music){
// static int i=0;
System.out.println("开始下载音乐:"+music);
this.music=music;
this.power-=10;
}
void playMusic(String music){
System.out.println("正在播放音乐---"+music);
this.power-=20;
}
void batteryCharging(String id){
if(this.power>20) System.out.println("电量充足,无需充电!");
else
{
System.out.println("电量不足,请及时充电!");
Cell cell=new Cell(id);
cell.batteryCharging();
}
}
}
class Cell{
String id;
Cell(String id){
this.id=id;
}
void batteryCharging(){
System.out.println("正在充电!");
}
}
public class FirstTest {
private static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
String brand=new String(scan.next());
int power=scan.nextInt();
Phone phone=new Phone(brand,power);
String music=new String(scan.next());
phone.downloadMusic(music);
phone.playMusic(music);
String id=new String(scan.next());
phone.batteryCharging(id);
}
}