Learn and familiar with the correct small car objects

xiaoxiao2021-03-06  60

Learn and familiar with the correct small car objects

Author: shuke Source: PHP Blue Book

Many books say that the class always likes to take a car to do an example. However, some examples are really stinky and bad, and they are lie to money, destroy people, mentally low-level, one-to-a -ty-style set_color () function to teach people. It is really white and bad. Today

PHPX.com also saw a victim and couldn't help but spent two hours. I wrote this tutorial.

Gossip less, we are coming, our trolley is not just a picture color is finished (only the color is a waste car). Our trolley can not only run everywhere, but also equipped with advanced GPS global positioning systems, oil gauges, mileage. Due to the use of object-oriented technology, it is not difficult to control such a small car.

Examples first provide some background materials. We have a car that can be arbitrarily driving in a map with XY coordinates, and you can set the direction and distance of the truck driving, and the trolley will report to you the coordinate position.

In fact, learning classes should be like learning other things, starting with learning, then learning his principles. So let's first familiarize you how to drive such a car:

setHeading ('s'); // Give the car setting direction S: South N: Northern W: Western E: Oriental. IF ($ mycar-> run (100)) // then let the trolley run 100 kilometers, if the task is successfully completed, and the fuel is displayed. If it is went halfway, we display alarm information. {Print ('
The car is normal, there is currently fuel:'. $ Mycar-> getgas (). ''); // Get fuel Number} else} else} else} else} else} else} else} > The cars have a problem: '. $ mycar-> getWarning ().' '); // Display alert information} $ myPosition = $ mycar-> getPosition (); // Get the car current Position Print ('
My car is now
x:'. $ MyPosition-> getX (). 'Y:'. $ MyPosition-> getY ()); // Show the coordinate position of the car?>

Let yourself make a car and give him a positioning object posient. Then set the direction and let the car run. Finally, check and output the orientation of the car. Is it complicated? Is it difficult to understand? Although we use two objects (classes): car and position but I believe that even beginners will not feel that the above code is very difficult.

After we learn how to drive, let's take a closer look at how this truck object is working. Define an object is actually simple, just need to use a keyword Class and a pair {}, so we define these two objects:

Class car {}

Class position {}

Of course, only such two classes can not be done, we also need to add some functions, first start from the car, we need to set the direction of the car and let the car run so we increase two methods, that is, 2 The function is only available in the trolley object, which can be used in the trolley object. SetHeading () Run ()

Class Car

{

Function SetHeading ($ DIRECTION)

{

}

Function Run ($ km)

{

}

}

Special note: Design a good class trick is from how to use it, that is to say what methods should be made first, instead of determining which attribute it has.

In order to better understand the car conditions, we also need these methods:

getgas () get the current fuel number getPosition () get the car current location getWarning () alarm information

In order to complete these features, our cars also require your own oil gauge, alert message, and positioner. We add these to the CAR class, and we also add this class to this class with this function name and the name of the class, which has a general framework.

GAS = $ GAS; / / Add Gasoline $ THIS-> Meter = 0; $ this-> warning = '; // Clear Alert Information $ THIS -> Position = $ position; // Set the departure position} Function getWarning () // Return the alert information {Return $ this-> warning;} Function getGas () // Return to the fuel gauge index {RETURN $ this-> GAS; } Function & getPosition () {RETURN $ this-> position; // Return the position of the current car} Function SetHeading ($ DIRECTION = 'E') {} / ** * Open Car * @ access public * @ param Int km number * / Function Run ($ km) {}}?> The most critical approach at this time is simple, because the trolley is equipped with the Position object $ this-> position, there is something about the coordinate positioning. Don't worry, hand it over to the Position object, just manage your own oil meter, the mileage can be. The CAR class after completion has changed this:

GAS = $ GAS; // Dipper Gasoline $ this-> meter = 0; $ this-> warning = ''; // Clear Alert Information $ this-> position = $ posient; // Setting the initial position} Function getWarning () // Return to the alert information {RETURN $ this-> warning;} function getGas () // Return to the fuel gauge index {RETURN $ this-> GAS Function & getPosition () {RETURN $ this-> position; // Return the location of the current car} Function setHeading ($ DIRECTION = 'E') {$ this-> position-> setdirection; // Because use Position object, cars don't need you to worry about the XY coordinate value, hand it over to the Position object. } / ** * Open Car * @ Access Public * @ param INT km * / function run ($ km) {$ goodrunflag = true; // Successfully completed the task. $ MAXDISTANCE = $ this-> GAS / $ this-> Engine; // The trolley can run the maximum distance. IF ($ MAXDISTANCE) <$ km) {$ this-> Warning = 'No gasoline! '; // Set a warning message, how far can you run how far. $ GoodRunflag = false; // But the task must not complete.

} Else {$ maxdistance = $ km; // There is no problem, you can stop after completing the task. } $ This-> Position-> Move ($ MAXDISTANCE); // Moved by the Position object on the coordinates, the car is responsible for your fuel consumption and km. $ this-> GAS - = $ maxDistance * $ this-> Engine; // Consume gasoline $ this-> meter = $ maxdistance; // Add kit count Return $ goodrunflag;}}?> Speaking here I think I think This article should also end. Don't worry, I will still remember that the Position class has not been completed, but there is an example of the above car should be very simple. If you understand the class of this car, now you have an exhibition, you come Complete this Position object, I believe you can complete it (in fact, this is the wonderful thing for object-oriented and packaged). You need to remember that the design from the Position method is first, for example:

Getx () getY () move () setdirection ()

The so-called class is to refer to things of a certain type, which can be specific (car) can also be abstract (Position), and we simplify use and operation through the package, just like we use TV, mobile phones are not complicated.

A good entry tutorial should

Vivid and true example. Not only provides the right concept, not only in variables and function names, functions, and calls on the call. Even if you are familiar with the object-oriented programming, you will not think that the original example is in any case. If you have finished reading the tutorial, you must deeply understand the wonderfulness of the tutorial, greatly reducing the opportunity to walk. A good code can be understood by a portrait of the portrait. What do you think?

(Editor: Beyond PHP)

转载请注明原文地址:https://www.9cbs.com/read-112917.html

New Post(0)