Define automobile engine class to output automobile information

The code is as follows:

public class Demo { public static void main(String[] args) { // TODO Auto-generated method stub Engine benzEngine=new Engine(); Car myCar=new Car("Benz",130,"red","suv",18,benzEngine); myCar.showInfo(); Car myCar2=new Car(); myCar2.showInfo(); }

}

//Automobile

Public class car {string carbrand; int price; string color; string motorcycletype; double oilconsumption; engine carengine; / * construct code block {system.out.println ("I am constructing code block");} * /// Construction method public car() {carengine = new engine (4.5220);} public Car(String _carBrand,int _price,String _color,String _motorcycleType,double _oilConsumption,Engine _engine){ carBrand=_carBrand; price=_price; color=_color; motorcycleType=_motorcycleType; oilConsumption=_oilConsumption; carEngine=_engine; } Public void start() {system. Out. Println ("car starts, engine actually ignites"); carengine. Start();} Public void stop() {system. Out. Println ("the car stops and the engine actually stalls"); carengine. Stop();} Public void showinfo() {system. Out. Println ("car information -----------"); system. Out. Println ("carbrand:" + carbrand); system. Out. Println ("price:" + price); system. Out. Println ("color:" + color); system. Out. Println ("motorcycletype:" + motorcycletype); system. Out. Println ("oilconsumption:" + oilconsumption); carengine. Showinfo();}

}

//Engine class

public class Engine { double displacement; int powerSize; public Engine(){ } public Engine(double _displacement,int _powerSize){ displacement=_displacement; powerSize=_powerSize; } Public void start() {system. Out. Println ("engine ignition");} Public void stop() {system. Out. Println ("engine off");} public void showInfo(){ System.out.println("displacement:"+displacement); System.out.println("powerSize:"+powerSize); } }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>