本人承接各種高校C語言、C++、Java語言等課程設計以及ppt等制造,有需要的私信我或者微信18476275715
定義一個汽車類Vehicle,要求如下:(知識點:類的繼承 方法的覆蓋)
(a)屬性包括:汽車品牌brand(String類型)、顏色color(String類型)和速度speed(double類型)。
(b)至少提供一個有參的構造方法(要求品牌和顏色可以初始化為任意值,但速度的初始值必須為0)。
(c)為屬性提供訪問器方法。注意:汽車品牌一旦初始化之后不能修改。
(d)定義一個一般方法run(),用打印語句描述汽車奔跑的功能。
定義測試類VehicleTest,在其main方法中創建一個品牌為“benz”、顏色為“black”的汽車。
(2)定義一個Vehicle類的子類轎車類Car,要求如下:
(a)轎車有自己的屬性載人數loader(int 類型)。
(b)提供該類初始化屬性的構造方法。
(c)重新定義run(),用打印語句描述轎車奔跑的功能。
(d)定義測試類Test,在其main方法中創建一個品牌為“Honda”、顏色為“red”,載人數為2人的轎車。
public class Vehicle { public String brand; public String color; public double speed=0; void setVehicle(String brand,String color) { this.brand=brand; this.color=color; } void access(String brand,String color,double speed) { this.brand=brand; this.color=color; this.speed=speed; } void run() { System.out.println("該汽車的品牌為:"+this.brand+"顏色為:"+this.color+"速度為"+this.speed); } }
public class VehicleTest { public static void main(String args[]) { Vehicle c; c=new Vehicle(); c.setVehicle("benz", "yellow"); c.run(); c.access("benz", "black", 300); c.run(); } }
public class Car extends Vehicle { int loader; void access(String brand,String color,double speed,int loader) { this.brand=brand; this.color=color; this.speed=speed; this.loader=loader; } void run() { System.out.println("該汽車的品牌為:"+this.brand+"顏色為"+this.color+"速度為"+this.speed+"核載人數"+this.loader);; } }
public class Test { public static void main(String args[]) { Car c; c=new Car(); c.access("Honda", "red", 300, 2); c.run(); } }
本題相對簡單,復雜程度較低!主要考察類的繼承和方法的覆蓋