package cn.itcast.chapter04.task02;
/*
* 交通工具類
*/
publicabstract class Transportation {
private String number ; // 編號
private String model ; // 型號
private String admin ; // 運貨負責人
public Transportation() {
super (); // 可省略
}
public Transportation(String number, String model, String admin) {
this . number = number;
this . model = model;
this . admin = admin;
}
// 運輸方法
publicabstractvoid transport();
// 編號
publicvoid setNumber(String number) {
this . number = number;
}
public String getNumber() {
return number ;
}
// 型號
publicvoid setModel(String model) {
this . model = model;
}
public String getModel() {
return model ;
}
// 負責人
publicvoid setAdmin(String admin) {
this . admin = admin;
}
public String getAdmin() {
return admin ;
}
}
/*
* 定義保養介面,具備保養功能。
*/
Public ninterface Careable {
// 保養方法
public abstractvoid upKeep();
}
/*
* 專用運輸車類
*/
Public class ZTransportation extends Transportation implements Careable{
// 無參構造
public ZTransportation() {
super ();
}
// 有參構造:車輛編號、型號、負責人
public ZTransportation(String number, String model, String admin) {
su per (number, model, admin);
}
// 運輸方法
publicvoid transport() {
System. out .println( " 運輸進行中。。。 " );
}
// 重寫車輛保養方法
publicvoid upKeep() {
System. out .println( " 貨物運輸車輛保養完畢 !" );
}
}
/*
* 快遞任務類
*/
public class SendTask {
private String number ; // 快遞單號
privatedouble goodsWeight ; // 貨物重量
public SendTask() {
super (); // 可省略
}
public SendTask(String number, double goodsWeight) {
this . number = number;
this . goodsWeight = goodsWeight;
}
// 送前準備
publicvoid sendBefore () {
System. out .println( " 訂單開始處理,倉庫驗貨中。。。 " );
System. out .println( " 貨物重量: " + this .getGoodsWeight()+ "kg" );
System. out .println( " 貨物檢驗完畢 !" );
System. out .println( " 貨物填裝完畢 !" );
System. out .println( " 運貨人已通知 !" );
System. out .println( " 快遞單號: " + this .getNumber());
}
// 發送貨物
publicvoid send(Transportation t,GPS tool) {
System. out .println( " 運貨人 " +t.getAdmin()
+ " 正在駕駛編號為 " +t.getNumber()
+ " 的 " +t.getModel()+ " 發送貨物! " );
t. transport();
String showCoordinate = tool.showCoordinate();
System. out .println( " 貨物當前的坐標為: " +showCoordinate);
}
// 送后操作
publicvoid sendAfter(Transportation t) {
System. out .println( " 貨物運輸任務已完成! " );
System. out .println( " 運貨人 " +t.getAdmin()
+ " 所駕駛的編號為 " +t.getNumber()
+ " 的 " +t.getModel()+ " 已歸還! " );
}
public String getNumber() {
return number ;
}
publicvoid setNumber(String number) {
this . number = number;
}
publicdouble getGoodsWeight() {
return goodsWeight ;
}
publicvoid setGoodsWeight( double goodsWeight) {
this . goodsWeight = goodsWeight;
}
}
/*
* 定義 GPS 介面,具備 GPS 定位功能。
*/
Public interface GPS{
// 顯示坐標的方法
public String showCoordinate();
}
/*
* 隨意定義一個物品,實作 GPS 介面,擁有定位功能。
*/
class Phone implements GPS{
public Phone() { // 空參構造
super ();
}
// 定位方法
public String showCoordinate() {
String location = "193,485" ;
return location;
}
} /*
* 定義測驗類
*/
public class Task02Test {
publicstaticvoid main(String[] args) {
// 快遞任務類物件
SendTask task = new SendTask( "HYX600235" ,76.34);
// 呼叫送前準備方法
task.sendBefore();
System. out .println( " ======================" );
// 創建交通工具物件
ZTransportation t = new ZTransportation( "Z025" , " 大奔 " , " 小韓 " );
// 創建 GPS 工具物件
Phone p = new Phone();
// 將交通工具與 GPS 工具傳入送貨方法
task.send(t,p);
System. out .println( "======================" );
// 呼叫送后操作方法
task.sendAfter(t);
t.upKeep();
}
}
uj5u.com熱心網友回復:
FileWriter fw = new FileWriter("F:\\a.txt", true);//F:\\a.txt為檔案位置BufferedWriter bw = new BufferedWriter(fw);
bw.write(data);//data為你要寫到TXT的資料,String型別
bw.close();
fw.close();
uj5u.com熱心網友回復:
如果你只是想把結果列印到txt里參考1樓,如果你是想把控制臺顯示的資訊列印到txt里的話,可以用cmd方式運行java程式,然后列印的內容重定向到自新建的txt檔案。1.先編譯類 javac 類名.java ,會生成個class檔案,2.執行 java class檔案名(別加后綴) >test.txt ,這樣控制臺資訊的內容就會列印到test.txt檔案里了轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/63201.html
標籤:Java相關
下一篇:pdf.js相關問題
