// 定義無人駕駛車輛的類別 class AutonomousVehicle { // 物件屬性 private String model; // 車型 private double speed; // 當前速度 (km/h) private String direction; // 方向 (e.g., "北", "南", "東", "西") // 建構子 (Constructor) public AutonomousVehicle(String m) { this.model = m; // 使用 this 指定物件屬性 this.speed = 0.0; // 預設速度為 0 this.direction = "北"; // 預設方向為北 } // 方法: 設定速度 public void setSpeed(double s) { if (s >= 0) { this.speed = s; System.out.println(this.model + " 設定速度為: " + this.speed + " km/h"); } else { System.out.println("速度不能為負數!"); } } // 方法: 變更方向 public void changeDirection(String d) { this.direction = d; System.out.println(this.model + " 改變方向至: " + this.direction); } // 方法: 自動駕駛導航 public void navigate(String dest) { System.out.println(this.model + " 正在前往 " + dest + "..."); } } // 主程式 public class Main { public static void main(String[] args) { // 創建無人駕駛車輛物件 AutonomousVehicle av = new AutonomousVehicle("AI-Car 1"); // 設定速度 av.setSpeed(50.0); // 變更方向 av.changeDirection("東"); // 自動駕駛前往目的地 av.navigate("市中心"); } }
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up