Java类继承与多态实践:Vehicle、Car、Bus等六个类的构建与测试

创始人
2024-12-25 04:46:54
0 次浏览
0 评论

java编程类的继承及多态

总共有六个类:Vehicle.java(汽车类)、Car.java(汽车类)、Bus.java(总线类)、Engine.java(引擎类)、VehicleFactory.java(汽车工厂))和VehicleTest.java(Test类),具体符号如下:***************************************************************总结classVehicle{Engineengine;doublespeed;doublemileage;publicVehicle(){}publicVehicle(Engineengine){this.engine=engine;}publicVehicle(Engineengine,doublemileage){this.engine=engine;this.mileage=mileage;}publicVehicle(Engineengine,doublespeed,doublemileage){this.engine=engine;this.speed=speed;this.mileage=mileage;}publicEnginegetEngine(){returnengine;}publicvoidsetEngine(Engineengine){this.engine=engine;}publicdoublegetSpeed(){returnspeed;}publicvoidsetSpeed(doublespeed){this.speed=speed;}publicdoublegetMileage(){returnmileage;}publicvoidsetMileage(doublemileage){this.mileage=里程;}publicdoubledrive(){this.setMileage(速度*(Math.round(Math.random()*6))+里程);returnmileage;}publicabstractdoublecalGC();publicStringtoString(){return"类型:"+this.getClass().getName()+",马力:"+engine.horsepower+",速度:"+速度+",里程:"+里程+",油耗:"+calGC();}}**********************************************************************publicclassCarextendsVehicle{publicCar()({}公共汽车)引擎){this.engine=engine;}publicCar(Engine,doublemileage){this.engine=engine;this.mileage=mileage;}publicCar(Engineengine,doublespeed,doublemileage){this.engine=engine;this.speed=speed;this.mileage=里程;}publicdoublecalGC(){returnmileage*2;}}***********************************************************************publicclassBusextendsVehicle{publicBus(){}publicBus(Engineengine){this.engine=engine;}publicBus(Engineengine,doublemileage){this.engine=engine;this.mileage=mileage;}publicBus(Engineengine,doublespeed,doublemileage){this.engine=engine;this.speed=speed;this.mileage=里程;}publicdoublecalGC(){returnmileage*3+20;}}*******************************************************************************publicclassEngine{doublehorsepower;publicEngine(){this.horsepower=1;}publicEngine(doublehorsepower){this.horsepower=马力;}publicdoublegetHorsepower(){returnhorsepower;}publicvoidsetHorsepower(doublehorsepower){this.horsepower=马力;}}****************************************************************公共类VehicleFactory{publicstaticVehiclemakeVehicle(Enginee){if((1<=e.getHorsepower())&&e.getHorsepower()<=50){returnnewCar(e,0);}elseif((50Enginee2=newEngine(88);Vehiclev2=VehicleeFactory.makeVehicle(e2);v2.setSpeed(88);v2.drive();v2.drive();System.out.println("第二辆车:");System.out.println(v2);*****************************************************************运行结果如下:第一辆车:类型:C,功率:28.0,速度:50.0,行驶距离:200.0,油耗:400.0第二辆车:类型:巴士,马力:88.0,速度:88.0,行驶距离:616.0,耗气量:1868.0************************************************************************************************************************************************************************************

JAVA编程:设计一个概念类“汽车”具有属性,类别,颜色,发动机,要求具有能设置类别属性的方法。

publicclassCar{Stringtype;Stringcolor;Stringengine;publicstaticvoidmain(String[]args){Caraodi=newCar();aodi.setColor("red");aodi.setType("A6");System.out.println("汽车类型:"+aodi.getType()+"汽车颜色颜色:"+aodi.getColor());}publicStringgetColor(){returncolor;}publicvoidsetColor(Stringcolor){this.color=color;}publicvoidsetType(Stringtype){this.type=type;}publicStringgetType(){returntype;}}

一道JAVA编程题:用类描述汽车中Engine(发动机)的功率和Gearbox(变速箱)的档位数目

--------引擎类------------publicclassEngine{intpower;publicintgetPower(){returnpower;}publicvoidsetPower(intpower){this.power=power;}}----------Gearbox类----------publicclassGearbox{intamount;publicintgetAmount(){returnamount;}publicvoidsetAmount(intamount){this.amount=amount;}}-----------细胞类型电机--------------publicclassCar{Engineengine=newEngine();Gearboxgearbox=newGearbox();publicEnginegetEngine(){returnengine;}publicvoidetEngine(Enginec){this.engine=c;}publicGearboxgetGearbox(){returngearbox;}publicvoidetGearbox(Gearboxh){this.gearbox=h;}publicvoidshow(){intpower=engine.getPower();intamount=gearbox.getAmount();System.out.println("功率为:"+power+"变速箱:"+金额);}}--------------------测试课-----公开课t{publicstaticvoidmain(String[]args){Engineengine=newEngine();engine.setPower(123);Gearboxgearbox=newGearbox();gearbox.setAmount(6);Carjeep=newCar();jeep.setEngine(engine);jeep.setGearbox(gearbox);jeep.show();}}这种问题太无聊了……看来你是个初学者。
我再次在线等待帮助您。
文章标签:
java 继承
热门文章
1
C语言字符串常量解析:区别、用途及存储方... 什么是字符串常量字符串常量是C语言中的一种数据类型,它是由一对双引号括起来的字符...

2
C语言printf函数:格式字符串与输出... printf函数中的格式与输出项有什么关系?在C语言中,printf函数中的格式...

3
Excel技巧:计算字符串起始位置与合并... 如何计算字符串在特定文本中的起始位置,怎么计算字符串在特定文本中的起始位置您可以...

4
Python发音全解:掌握正确的发音方法... python怎么读我的很多学习编程的朋友可能都知道Python这个词,但是他们中...

5
字符与字符串:编程中的基础文本类型解析 字符串什么意思字符串是由数字、字母和下划线组成的字符串,表示为s=“a1a2…a...

6
Python编程语言:多领域应用与开发优... Python的作用是什么?Python是一种跨平台计算机编程语言,是ABC语言的...

7
深度解析:C语言编程特点与应用领域 什么叫c语言C语言是一种编程语言。C编程语言应用广泛,具有以下特点和特点:1.语...

8
Java全解析:跨平台编程语言的魅力与多... java是什么Java是一种功能强大的编程语言,被称为“一次编写,随处运行”模型...

9
C语言printf输出常量类型与格式详解 C语言中如何输出各种常量?C语言的输出常量和输出变量是相同的。可以使用带有各种格...

10
Python发音指南:英式美式发音差异及... python怎么发音python的发音和注释如下:python英文[?pa?θ?...