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((50JAVA编程:设计一个概念类“汽车”具有属性,类别,颜色,发动机,要求具有能设置类别属性的方法。
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();}}这种问题太无聊了……看来你是个初学者。我再次在线等待帮助您。
上一篇:
C语言字符串定义与显示技巧解析
相关文章
解析System.FormatExcep...
2024-12-14 21:07:53C语言chdir函数:轻松实现工作目录切...
2024-12-25 20:24:00专业设计经验分享:6篇心得体会深度解析
2024-12-25 01:27:06C语言系统时间输出技巧与函数多值返回方法...
2024-12-17 05:52:42Java字符串替换技巧:replace(...
2024-12-25 14:41:33C语言深入解析:字符数组即字符串,无需额...
2024-12-24 00:29:23C语言define关键字与条件编译深度解...
2024-12-21 16:26:57Python%运算符详解:数字模除与字符...
2024-12-16 16:24:28C语言编程入门:100个核心代码示例解析
2024-12-15 00:54:02C语言基础:常见数据类型字节占用详解
2024-12-16 06:53:37最新文章
25
2024-12
25
2024-12
25
2024-12
25
2024-12
25
2024-12
25
2024-12
25
2024-12
25
2024-12
25
2024-12
25
2024-12
热门文章
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?θ?...