Requirements: Use Java to describe a car and garage two things, the car has the public properties: Number of wheels, name, color, also
Has the function of running the behavior. Before running to check whether the wheel is less than 4, if less than 4, then to be sent to the garage repair,
After repairing the garage, the number of wheels of the car has to be back up to 4. Then the car went on running.
Garage: With public properties: Name, address, telephone.
Public behavior: Repairing a car.
Class car{
Public properties
String name;//Name
String color;//Color
int num = 4;//java is allowed to define member variables and initialize values.
public void Run () {
if (num < 4) {
System.out.println (name+ "driving");
}
}
}
Garage
Class Carfactory
{
String name;//The name of the repair shop
String address; Address
String Tel; Phone
How to repair a car
A method with parameters
public void repair (car car) {//parameter variable also belongs to local variable
Car.num = 4;
System.out.println ("Car to repair");
}
}
Class Demo8
{
public static void Main (string[] args)
{
Create a car
Car car = new car ();
Car.name = "Mercedes-Benz";
Call the Run method
Car.run ();
After one months of driving, a wheel flew.
Car.num = 3;
Car.run ();
Need a maintenance Production object
Carfactory CF = new Carfactory ()
Cf.name = "Bever repair shop";
cf.address = "Jiangxi Institute of Engineering (Tiangong campus)";
Cf.tel = "10086";
Need to repair the car
Cf.repair (car);
And you can drive.
Car.run ();
}
}
Describe a car and garage in Java two things