導航:首頁 > 編程語言 > java構建類

java構建類

發布時間:2025-05-22 03:17:58

❶ 用java創建一個汽車類(Car),為其定義兩個屬性:顏色和型號,為該類創建兩個構造方法

public class Car {

private int color; // 顏色 0:未定義 1:紅 2:黃 3:藍 ...
private int type ; // 型號 0:未定義 1:轎車 2:卡車 3:大巴 4:越野車

// 無形參的構造方法
public Car(){
this.type = 1;
this.color = 1;
}

// 有形參的構造方法
public Car(int type, int color ){
this.type = type;
this.color = color;
}

// 顯示顏色
public void PrintColor(){
String strColor = "";
switch(this.color){
default:
case 0:
strColor = "未定義顏色";
break;

case 1:
strColor = "紅";
break;
case 2:
strColor = "黃";
break;
case 3:
strColor = "藍";
break;
}

System.out.print(strColor);
}

// 顯示型號
public void PrintType(){
String strType = "";
switch(this.type){
default:
case 0:
strType = "未定義型號";
break;

case 1:
strType = "轎車";
break;
case 2:
strType = "卡車";
break;
case 3:
strType = "大巴";
break;
case 4:
strType = "越野車";
break;
}

System.out.print(strType);
}

}

閱讀全文

與java構建類相關的資料

熱點內容
pythonweb操作資料庫 瀏覽:296
自動旋轉屏幕的app叫什麼 瀏覽:714
pubgmobile安卓怎麼獲得資格 瀏覽:761
變頻壓縮機代換 瀏覽:223
MAC智能文件夾使用方法 瀏覽:25
如何製作2b2t伺服器指令 瀏覽:944
專科程序員多大了 瀏覽:664
絕地求生如何加入伺服器 瀏覽:221
web前端開發和php的 瀏覽:500
核酸檢測app綁定是什麼意思 瀏覽:45
android線程池工具 瀏覽:841
美佳是什麼app 瀏覽:903
javat是什麼意思 瀏覽:423
androidgps模擬定位 瀏覽:599
編程貓會跑路嗎 瀏覽:190
在線看網站源碼 瀏覽:253
呼吸燈電腦編程 瀏覽:134
樓月小說app是什麼意思 瀏覽:331
掌上旅遊php源碼 瀏覽:946
演算法座右銘 瀏覽:748