30
Sep
// paint all rectangles void paint(Graphics g) {
Figure 2-8 DancingRect object The danceStep() and paint() methods provide the public interface to a DancingRect object. All other objects can access these methods. By contrast, the data inside a DancingRect, such as its location, is shielded from the outside, or encapsulated. Encapsulation means that data inside an object is not visible from the outside. By hiding information inside objects and providing interfaces to these objects, you protect yourself when you decide to make modifications to an object s internal structure. For example, if you decide to change the variable name of the location from locx to XPosition, you want to be sure that any changes are isolated in the definition of DancingRect. By doing this, code outside DancingRect won t be affected. This makes it easier for you to manage complex programs. Let s provide the class definition, in Listing 2-3, for DancingRect. Listing 2-3 DancingRect class ///////////////////////////////////////////////////////////////// class DancingRect { // instance variables: int locx, locy; // (locx,locy) are coordinates of upper // left corner of rectangle int width, height; // width and height of rectangle Color myColor; // color of rectangle ///////////////////////////////////////////////////////////////// // constructor public DancingRect(int locx,int locy, int width,int height, Color myColor) { this.locx = locx; this.locy = locy; this.width = width; this.height = height; this.myColor = myColor; } // methods: public void danceStep() { // does nothing }
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services
