Example 3 40 explicit member access public void

Info icon This preview shows pages 121–123. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 3-40. Explicit member access public void UpdatePosition(double minutesToAdvance) { double hours = minutesToAdvance / 60; double milesMoved = this. SpeedInMilesPerHour * hours; double milesToTower = this. Position.Distance; if ( this. Direction == DirectionOfApproach.Approaching) { milesToTower -= milesMoved; if (milesToTower < 0) { // We've arrived! milesToTower = 0; } } else { milesToTower += milesMoved; } PolarPoint3D newPosition = new PolarPoint3D( milesToTower, this. Position.Angle, this. Position.Altitude); } This is almost the same as Example 3-39 , except every member access goes through a variable called this . But we’ve not defined any such variable—where did that come from? The UpdatePosition method effectively has an implied extra argument called this , and it’s the object on which the method has been invoked. So, if our Main method were to call someBoeing777.UpdatePosition(10) , the this variable would refer to whatever ob- ject the Main method’s someBoeing777 variable referred to. Methods get a this argument by default, but they can opt out, because sometimes it makes sense to write methods that don’t apply to any particular object. The Main method of our Program class is one example—it has no this argument, because the .NET Framework doesn’t presume to create an object; it just calls the method and lets us decide what objects, if any, to create. You can tell a method has no this argument because it will be marked with the static keyword—you may recall from Chapter 2 that this means the method can be run without needing an instance of its defining type. Aside from our Main method, why might we not want a method to be associated with a particular instance? Well, one case comes to mind for our example application. Defining Methods | 97
Image of page 121

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
There’s a rather important feature of airspace management that we’re likely to need to cope with: ensuring that we don’t let two planes hit each other. So, another method likely to be useful is one that allows us to check whether one plane is too close to another one, within some margin of error (say, 5,000 feet). And this method isn’t associated with any single plane: it always involves two planes. Now we could define a method on Plane that accepted another Plane as an argument, but that’s a slightly misleading design—it has a lack of symmetry which suggests that the planes play different roles, because you’re invoking the method on one while pass- ing in the other as an argument. So it would make more sense to define a static method—one not directly associated with any single plane—and to have that take two Plane objects. Declaring Static Methods We’ll add the method shown in Example 3-41 to the Plane class. Because it is marked static , it’s not associated with a single Plane , and will have no implicit this argument.
Image of page 122
Image of page 123
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern