Enough chatter, we are here to get serious. Our cars are not just for people to paint the color of (a car that can only paint the color is a useless car). Our little car can not only run around, but is also equipped with an advanced GPS global positioning system, fuel gauge, and odometer. Due to the use of object-oriented technology, it is not difficult at all to drive such a small car.
To give an example, we first need to provide some background material. We have a car that can travel in any direction from southeast to northwest on a map with xy coordinates. You can set the direction and distance of the car, and the car will report its coordinate position to you.
In fact, learning about categories should be the same as when we learn about other things. We start by learning how to use it, and then learn its principles. So let’s first get familiar with how to drive such a car correctly:
$startPoint = & new Position(3,9); //Initial starting point coordinates x=3,y=9
$myCar = & new Car(500,$startPoint); //I got a new car, the new car’s initial fuel is 500 liters, and the starting point is $startPoint.
$myCar->setHeading('s'); //Set the direction of the car s: south n: north w: west e: east.
if($myCar->run(100)) //Then let the car run 100 kilometers. If the task is successfully completed, the fuel amount will be displayed. If it fails halfway, we display an alert message.
{
print('
Everything in the car is normal, and there is still fuel: '.$myCar->getGas().'');//Get the fuel number
}
else
{
print('
There is a problem with the car: '.$myCar->getWarning().'');//Display alarm information
}
$myPosition=$myCar->getPosition();//Get the current position of the car
print('
My car is now
?>
First make yourself a car and equip it with a positioning object Position. Then set the direction and let the car run. Finally check and output the position of the car. Is it complicated? Is it difficult to understand? Although we use two objects (classes) here: Car and Position, I believe that even beginners will not find the above code difficult.
After we learn how to drive, let's take a closer look at how this car object works. Defining an object is actually very simple. You only need to use a keyword class and a pair of {}, so we define these two objects like this:
class Car {}
class Position{}
Of course, just these two classes can't do anything. We also need to add some functions to them. Starting with the car, we need to be able to set the direction of the car and make the car run, so we add two methods, that is, 2 Function It’s just that these two functions are included in the car object and can only be used through the car object.
setHeading()
run()
class Car
{
function setHeading($direction)
{
}
function run($km)
{
}
}
Special tip: The trick to designing a good class is to start with how to use it, that is to say, first consider what methods the object should have, rather than first determining what properties it has.
In order to better understand the condition of the car, we also need these methods:
getGas() gets the current fuel quantity of the car
getPosition() gets the current position of the car
getWarning() Alert information
In order to complete these functions, our car also needs its own fuel gauge, alarm message, and locator. We added these to the Car class, and we also added an initialization function to this class. The name of the function is the same as the name of the class, so that we have a general framework.
class Car
{
/**
* The amount of gasoline in the car
*
*@var
*@access
*/
var $gas;
/**
* Mileage record