當前位置:編程學習大全網 - 編程語言 - java用最簡潔的代碼編寫,如圖。

java用最簡潔的代碼編寫,如圖。

class?Phone{

//手機的音樂存儲器?

private?List<Music>?sounds?=?new?ArrayList<Music>();

//手機播放音樂的方法?

public?void?play(){

for(Music?m:sounds){

m.play();//調用音樂的播放方法

}?

}

//手機下載音樂的方法

public?void?download(Music?m){

sounds.add(m);

}?

//手機充電的方法

public?void?charge(Cell?c){

c.charge();//調用插入電池的充電方法

}?

}

class?Cell{

private?String?brand;//電池的品牌

Cell(String?brand){

this.brand=brand;

}?

//提供查看電池品牌的方法,不提供修改品牌的方法

public?String?getBrand(){

return?brand;

}?

//電池的續電方法

public?void?charge(){

System.out.println(brand+"品牌的電池正在為手機充電");

}?

}

//附送給妳的音樂類

class?Music{

private?String?name;//歌名?

private?String?lyric;//歌詞?

private?String?player;//演唱者

Music(String?lyric,String?player,String?name){

this.lyric=lyric;

this.player=player;?

this.name=name;?

}

//該音樂的播放方法?

public?void?play(){

System.out.println(name);?

System.out.println(player);

System.out.println(lyric);?

}

}

public?class?Test{

public?static?void?main(String...?args){

Phone?iphone?=?new?Phone();//壹只蘋果get!

//臥槽手機沒有電池TAT趕快買塊電池壓壓驚

Cell?cell?=?new?Cell("三星");?

//看看電池什麽牌子

System.out.println(cell.getBrand());

//把電池插上

iphone.charge(cell);

//下載首歌

iphone.download(new?Music("妳在南方的艷陽裏...","張磊","<南山南>"));

//聽聽看吧balabala

iphone.play();?

}?

}

  • 上一篇:怎樣用java開發ftp客戶端
  • 下一篇:家家福燃氣竈全國統壹客服熱線24小時售後維修中心2022已更新
  • copyright 2024編程學習大全網