定义抽象类Vehicles具有抽象方法wheel, 接口MoneyFare具有方法charge,类Bus和Taxi实现抽象类和接口,Car实现接口,测试其特性。

【问题描述】
定义抽象类Vehicles具有抽象方法wheel, 接口MoneyFare具有方法charge,类Bus和Taxi实现抽象类和接口,Car实现接口,测试其特性。

【输入形式】
请输入公交车线次:347请输入出租车名字:yellow请输入你的车品牌:BMW
【输出形式】公交车347有6个轮子公交车347票价2元每人出租车yellow有4个轮子出租车yellow为2元每公里如果这辆BMW车是你的就免费乘坐
【样例输入】
347YellowBMW
【样例输出】
The bus 347 has 6 wheels.
The bus 347 2 yuan per people
The taxi Yellow has 4 wheels.
The taxi Yellow 2 yuan per km
The car BMW is free if it is yours

import  java.util.Scanner;

abstract  class  Vehicles  {

        abstract  void  wheel(String  name);

}

interface  MoneyFare  {

        void    charge(String  name);

}

class  Bus  extends  Vehicles  implements  MoneyFare  {

        void  wheel(String  name)  {

                System.out.println("The bus "+name+" has 6 wheels.");

        }

        public  void  charge(String  name)  {

                System.out.println("The bus "+name+" 2 yuan per people");

        }

}

class  Taxi  extends  Vehicles  implements  MoneyFare{

        void  wheel(String  name)  {

                System.out.println("The taxi "+name+" has 4 wheels.");

        }

        public    void  charge(String  name)  {

                System.out.println("The taxi "+name+" 2 yuan per km");

        }

}

class  Car  implements  MoneyFare{

        public  void  charge(String  name)  {

                System.out.println("The car "+name+" is free if it is yours");

        }

}

public  class  P4T3  {

        public  static  void  main(String  args[])  {

                

                Bus    bus  =  new  Bus();

                Taxi  taxi  =  new  Taxi();

                Car  car  =  new  Car();

                

                String  busname,taxiname,carname;

                Scanner  s  =  new  Scanner(System.in);

                

                System.out.println("Please  input  the  bus  line:  ");

                busname  =  s.nextLine();

                System.out.println("Please  input  the  taxi  name:  ");

                taxiname  =  s.nextLine();

                System.out.println("Please  input  the  car  brand:  ");

                carname  =  s.nextLine();

                bus.wheel(busname);

                bus.charge(busname);

                taxi.wheel(taxiname);

                taxi.charge(taxiname);

                car.charge(carname);

        }

}
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值