注册 登录
编程论坛 JAVA论坛

关于类的继承的习题。麻烦帮忙看一下应该怎么作。谢谢[

wl20000 发布于 2022-12-06 16:59, 2703 次点击
分析以下车辆的属性与方法,找出共性,然后设计对应的父类与子类。

公交车,出租车、家用轿车、自行车、摩托车、水泥搅拌车、绿皮火车、高铁、地铁

划分方式有多种:

使用性质:运营类、家用类、商用工具类、娱乐类。
车辆性质:机动车、非机动车、两轮车、四轮车、多轮车等.


你可选择其中一种进行设计,要求如下:

完整性,能完整的描述车辆的性质与行为
简单清晰,如出租车有所属公司属性、是否载客的状态、有打表计价的方法
可扩展性,能够再扩展新的车型
3 回复
#2
sohu10252023-01-16 09:41
就是选择其中的一种分类方式,找出它们的共性创建父类,再继承父类添加特有属性方法的资料,比如按车辆性质分,都有行驶速度,车轮等公共属性
#3
deagoal2023-02-20 11:10
我觉得大概可以这样设计吧?

程序代码:


 class vehicle {
    //共性,长宽高重,轮数,载员数,类型,主人
    float height;
    float width;
    float length;
    float weight;//车重
    float load;//载重
    int person;//载员数
    int wheel;//轮数
    int type;//类型
    String owner;//主人
   
//行为
    move(){}
    moveBack(){}
    turn(direct){}
}

 enum taxi_status{//未可用,空闲,运营,占用,报废
     taxi_unused,
     taxi_free,
     taxi_running,
     taxi_ocupied,
     taxi_scrapped

 }

 class taxi extends vehicle{
//性质
     String company;//所属公司
     String curLocation;//目前地址
     String startLocat;//始发地
     String dest;//目的地
     taxi_status status;//状态,
     Integer distance;//计价路程 ,米
     Integer time_span;//计价时长,分钟
     Integer start_distance;//起始计费距离
     Integer start_time_span;//起始计费时长
     double price1;//每公里价格
     double price2;//每分钟价格
     double payment;
//getters and setters
     taxi(){
         start_distance = 5;
         start_time_span = 60;
         price1 = 1.1;
         price2 = 0.1;
     }

 //行为
     void loadingPassenger() {//载客,或载客途中
         status = taxi_status.taxi_ocupied;
     }
     void load() {//开始运客
         status = taxi_status.taxi_running;
         distance = 0;         time_span = 0;
         //setstartlocation, setDest()
     }
     void unload() {//下客
         status = taxi_status.taxi_free;
        //setstartlocation, setDest()
         
     }
     void tick() {
         //totaltime++; 统计工作时长
        if(status == taxi_status.taxi_running) time_span++;//分钟 计算运客时长
     }
     void moveOn() {//每前进一米执行一次
         distance++;
     }
     double getPayment() {//计算当前费用
         return price1 * ((distance - start_distance) > 0 ? (distance - start_distance) : 0) / 1000
                 + price2 * ((time_span - start_time_span) > 0 ? (time_span - start_time_span) : 0)
                 + price1 * start_distance ;
     }
     void moveAround(String curLocate) {//时刻记录当前位置, 参数可以是经纬度,或是街道及距离等复杂对象
         this.curLocation = curLocate;
     }

 }

#4
停车场一2023-02-22 16:48
居然还有这样的题目呀,感觉实在是做不出来哦
1