Use the correct car object to learn and be familiar with the concept of class _ PHP Tutorial

Source: Internet
Author: User
Use the correct car object to learn and be familiar with the concept of class. Let's talk less about it. we're serious about it. our car isn't just about making people picture the color (only pictures of cars are useless ). Our car can not only run around, but also let us talk less about it, we are serious, our car is not just let people figure the color of the picture to complete (only the color of the car is the waste car ). Our car not only runs everywhere, but also is equipped with advanced GPS global positioning system, oil table, and ODPS. Because of the use of object-oriented technology, it is not difficult to control such a car.

For example, you must first provide some background materials. We have a car that can be traveling in any direction in the southeast and northwest of a map with xy coordinates. you can set the direction and distance of the car, the car will report its coordinates to you.

In fact, the learning class should be the same as learning other things, starting from learning to use, and then learning his principles. So let's get familiar with how to drive a car correctly:

$ StartPoint = & new Position (3,9); // initial starting point coordinate x = 3, y = 9

$ MyCar = & new Car (500, $ startPoint); // I get a new Car with an initial 500 litre fuel and a departure location of $ startPoint.

$ MyCar-> setHeading ('s); // set the direction for the car s: South n: North w: West e: East.

If ($ myCar-> run (100) // run the car for 100 kilometers. if the task is completed successfully, the fuel volume is displayed. If we quit halfway, we will display the alarm information.
{
Print ('
Everything is normal for the car, and there is still fuel: '. $ myCar-> getGas ().''); // Get the number of fuel
}
Else
{
Print ('
The car goes wrong: '. $ myCar-> getWarning ().''); // Displays alarm information
}

$ MyPosition = $ myCar-> getPosition (); // Obtain the current location of the car

Print ('
My car now
X: '. $ myPosition-> getX (). 'Y:'. $ myPosition-> getY (); // displays the coordinates of the car.
?>

Create a car for yourself and equip him with a Position object. Then set the direction and let the car run. Finally, check and output the vehicle location. Complicated? Is it hard to understand? Although we use two objects (classes): Car and Position, I believe that even beginners will not find the above code very difficult.

After learning how to drive, let's take a closer look at how the car object works. To define an object, you only need to use a key word class and a pair of {}. Therefore, we can define these two objects as follows:
Class Car {}
Class Position {}
Of course, we can't do anything for these two categories. we also need to add some features to them, starting with a car, we need to be able to set the direction for the car and let the car run, so we add two methods, namely two functions, but 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)
{

}
}
Note: The trick to design a good class is to start with how to use it. that is to say, we should first consider the methods for this object, rather than determining what attributes it has.
To better understand the situation of the car, we also need these methods:
GetGas () obtains the current fuel quantity of the vehicle.
GetPosition () obtains the current location of the vehicle.
GetWarning () alert information

To complete these features, our car also needs its own oil table, alarm message, and locator. We also added these to the Car class, and added an initialized function to the class. the function name is the same as the class name, so we have a general framework.

Class Car
{
/**
* Car gasoline volume
*
* @ Var
* @ Access
*/
Var $ gas;

/**
* Mileage record

Vehicle (only cars in the color of the image are scrapped ). Our car can not only run around, but also...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.