Learn and familiarize yourself with the concept of class with the correct car object

Source: Internet
Author: User
Article Title: use the correct car object to learn and be familiar with the concept of class. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Our car is not just about making people figure the color (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) // then let the car run 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 keyword 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 best way to design a good class is to start with how to use it. That is to say, you should first consider the methods of this object, rather than first determine 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
*
* @ Var
* @ Access
*/
Var $ meter;
  
/**
* Vehicle location (automatically controlled by GPS)
*
* @ Var Object position
* @ Access private
*/
Var $ position;
    
/**
* The engine consumes fuel every kilometer. This car is 0.1 liters.
*
* @ Var Integer
* @ Access private
*/
Var $ engine = 0.1;
  
/**
* Alarm information
*
* @ Var
* @ Access
*/
Var $ warning;
    
/**
Vehicle initialization. New cars are required
1. add gasoline.
2. the mileage table is set to zero.
3. Clear alarm information.
4. Set the departure position.
*/
Function Car ($ gas, & $ position)
{
$ This-> gas = $ gas; // Add Gasoline
$ This-> meter = 0;
$ This-> warning = ''; // clear alarm information
$ This-> position = $ position; // you can specify the starting position.
}
    
Function getWarning () // returns alert information
{
Return $ this-> warning;
}
    
Function getGas () // returns the gasoline table Index
{
Return $ this-> gas;
}
  
Function & getPosition ()
{
Return $ this-> position; // return the current location of the car.
}
  
Function setHeading ($ direction = 'E ')
{
      
}
  
/**
* Start a car
* @ Access public
* @ Param INT kilometer count
*/
Function run ($ km)
{
      
}
    
}
?>
At this time, the two most critical methods setHeading and run become simple. Because the car is equipped with the Position object $ this-> position, it does not need to worry about coordinate positioning, hand it over to the Position object. He only needs to manage his own oil table and the ODPS table. The Car class will look like this after completion:
  
   Class Car
{
/**
* Car gasoline volume
*
* @ Var
* @ Access
*/
Var $ gas;
  
/**
* Mileage record
*
* @ Var
* @ Access
*/
Var $ meter;
  
/**
* Vehicle location (automatically controlled by GPS)
*
* @ Var Object position
* @ Access private
*/
Var $ position;
    
/**
* The engine consumes fuel every kilometer. This car is 0.1 liters.
*
* @ Var Integer
* @ Access private
*/
Var $ engine = 0.1;
  
/**
* Alarm information
*
* @ Var
* @ Access
*/
Var $ warning;
       
/**
Vehicle initialization. New cars are required
1. Fill in gasoline.
2. the mileage table is set to zero.
3. Clear alarm information.
4. Set the departure position.
*/
Function Car ($ gas, & $ position)
{
$ This-> gas = $ gas; // fill the gasoline
$ This-> meter = 0;
$ This-> warning = ''; // clear alarm information
$ This-> position = $ position; // you can specify the initial position.
}
  
Function getWarning () // returns alert information
{
Return $ this-> warning;
}
    
Function getGas () // returns the gasoline table Index
{
Return $ this-> gas;
}
  
Function & getPosition ()
{
Return $ this-> position; // return the current location of the car.
}
  
Function setHeading ($ direction = 'E ')
{
$ This-> position-> setDirection ($ direction); // because the Position object is used, the car does not need to worry about the XY coordinate value. Hand it over to the Position object.
}
  
/**
* Start a car
* @ Access public
* @ Param INT kilometer count
*/
    
Function run ($ km)
{
$ GoodRunFlag = true; // whether the task is successfully completed.
$ MaxDistance = $ this-> gas/$ this-> engine; // The maximum distance that a car can run.
      
If ($ maxDistance) <$ km)
{
$ This-> warning = 'no more gasoline! '; // Set the warning information. If you can run far, run far.
$ GoodRunFlag = false; // but the task cannot be completed.
}
Else
{
$ MaxDistance = $ km; // No problem. After completing the task, you can stop and take a rest.
}
$ This-> position-> move ($ maxDistance); // moving the Position object on the coordinate is done by the position object. The car only needs to take charge of its own fuel consumption and the internal table.
$ This-> gas-= $ maxDistance * $ this-> engine; // consumes gasoline
$ This-> meter + = $ maxDistance; // increase the table count
Return $ goodRunFlag;
   
Related Article

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.