Software Engineering for Students A Programming Approach
Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach
15.2 Encapsulation 203 Often (though not in the Alien example shown above) there will need to be additional methods that the class needs in order to carry out its tasks. These are private methods that need not and therefore should not be accessible from outside the class. A class represents a real fusion of data and actions. A class extends the built-in data types provided by the language, so that the programmer can invent data suitable for the problem being solved. The programmer specifies how the data can be manipulated and thus creates truly abstract data. The advantages of encapsulation are: ■ to make useful classes generally available ■ to use a class without the need to know how it works ■ to have the flexibility to modify a class without affecting its users. Properties We have seen that it is very bad practice to make public any of the instance variables of a class. Some languages, for example, C# and Visual Basic, provide a mechanism that simulates accessing variables directly. This feature, called properties, enables users to have convenient but controlled access to the data associated with an object. In general there are two distinct kinds of access to data: 1. reading a value – called get access 2. writing the value – called set access. For example, suppose we want to allow a user of an alien object to refer to (get) the x coordinate of the alien and display its value in a text field. The value is held in the private variable named x at the top of the class. Using a property named xCoord, we can write: textField.setText(Integer.toString(alien.xCoord)); Suppose also that we want the user to be able to change (set) the value of the x coordinate. Using the property facility we can write: alien.xCoord = 56; The way to provide these facilities is to write a property. Here is the revised class that includes the property code: public class AlienWithProperties { private int x; private int y; private int size; private ImageIcon alienImage; >
204 Chapter 15 ■ Object-oriented programming > > } public Alien(int newX, int newY, int newSize) { x = newX; y = newY; size = newSize; alienImage = new ImageIcon("c:/alien.jpg"); } public void moveLeft(int amount) { x = x - amount; } public void moveRight(int amount) { x = x + amount; } public void display(JPanel panel) { Graphics paper = panel.getGraphics(); alienImage.paintIcon(panel, paper, x, y); } public int xCoord { get { return x; } set { x = value; } } The header for a property looks similar to a method header, except that there are no brackets to specify parameters. The property consists of two complementary components – one has get as the heading and the other has set as the heading. The get part is like a function method – it returns the desired value. The set part is like a method – it assigns the value using the special keyword value as shown. Note that this code is in the style of Java, but it is not Java, since Java does not support a property mechanism. If we only need a way of viewing a property (but not changing its value), we write the property declaration like this: public int xCoord { get { return x; } } >
- Page 176 and 177: 12.3 Delegation 153 The concepts of
- Page 178 and 179: 12.5 Factory method 155 The followi
- Page 180 and 181: 12.8 Model, view controller (observ
- Page 182 and 183: Figure 12.4 Pipe and Filter pattern
- Page 184 and 185: Figure 12.6 Layers in a distributed
- Page 186 and 187: Answers to self-test questions 163
- Page 188 and 189: CHAPTER 13 Refactoring This chapter
- Page 190 and 191: 13.3 ● Move Method 13.6 Inline Cl
- Page 192 and 193: class Sprite Instance variables x y
- Page 194 and 195: Summary Summary 171 it is making po
- Page 196: PART C PROGRAMMING LANGUAGES
- Page 199 and 200: 176 Chapter 14 ■ The basics and a
- Page 201 and 202: 178 Chapter 14 ■ The basics > > >
- Page 203 and 204: 180 Chapter 14 ■ The basics > Ear
- Page 205 and 206: 182 Chapter 14 ■ The basics > Cas
- Page 207 and 208: 184 Chapter 14 ■ The basics > > >
- Page 209 and 210: 186 Chapter 14 ■ The basics > } }
- Page 211 and 212: 188 Chapter 14 ■ The basics Unfor
- Page 213 and 214: 190 Chapter 14 ■ The basics Ada d
- Page 215 and 216: 192 Chapter 14 ■ The basics The w
- Page 217 and 218: 194 Chapter 14 ■ The basics In a
- Page 219 and 220: 196 Chapter 14 ■ The basics > str
- Page 221 and 222: 198 Chapter 14 ■ The basics Answe
- Page 223 and 224: CHAPTER 15 Object-oriented programm
- Page 225: 202 Chapter 15 ■ Object-oriented
- Page 229 and 230: 206 Chapter 15 ■ Object-oriented
- Page 231 and 232: 208 Chapter 15 ■ Object-oriented
- Page 233 and 234: 210 Chapter 15 ■ Object-oriented
- Page 235 and 236: 212 Chapter 15 ■ Object-oriented
- Page 237 and 238: 214 Chapter 15 ■ Object-oriented
- Page 239 and 240: 216 Chapter 15 ■ Object-oriented
- Page 241 and 242: 218 Chapter 15 ■ Object-oriented
- Page 243 and 244: 220 Chapter 15 ■ Object-oriented
- Page 245 and 246: 222 Chapter 16 ■ Programming in t
- Page 247 and 248: 224 Chapter 16 ■ Programming in t
- Page 249 and 250: 226 Chapter 16 ■ Programming in t
- Page 251 and 252: 228 Chapter 16 ■ Programming in t
- Page 253 and 254: 230 Chapter 16 ■ Programming in t
- Page 255 and 256: 232 Chapter 16 ■ Programming in t
- Page 257 and 258: 234 Chapter 16 ■ Programming in t
- Page 259 and 260: 236 Chapter 16 ■ Programming in t
- Page 261 and 262: 238 Chapter 17 ■ Software robustn
- Page 263 and 264: 240 Chapter 17 ■ Software robustn
- Page 265 and 266: 242 Chapter 17 ■ Software robustn
- Page 267 and 268: 244 Chapter 17 ■ Software robustn
- Page 269 and 270: 246 Chapter 17 ■ Software robustn
- Page 271 and 272: 248 Chapter 17 ■ Software robustn
- Page 273 and 274: 250 Chapter 17 ■ Software robustn
- Page 275 and 276: 252 Chapter 17 ■ Software robustn
15.2 Encapsulation 203<br />
Often (though not in the Alien example shown above) there will need to be additional<br />
methods that the class needs in order to carry out its tasks. These are private<br />
methods that need not and there<strong>for</strong>e should not be accessible from outside<br />
the class.<br />
A class represents a real fusion of data and actions. A class extends the built-in data<br />
types provided by the language, so that the programmer can invent data suitable <strong>for</strong> the<br />
problem being solved. The programmer specifies how the data can be manipulated and<br />
thus creates truly abstract data.<br />
The advantages of encapsulation are:<br />
■ to make useful classes generally available<br />
■ to use a class without the need to know how it works<br />
■ to have the flexibility to modify a class without affecting its users.<br />
Properties<br />
We have seen that it is very bad practice to make public any of the instance variables<br />
of a class. Some languages, <strong>for</strong> example, C# and Visual Basic, provide a mechanism that<br />
simulates accessing variables directly. This feature, called properties, enables users to<br />
have convenient but controlled access to the data associated with an object. In general<br />
there are two distinct kinds of access to data:<br />
1. reading a value – called get access<br />
2. writing the value – called set access.<br />
For example, suppose we want to allow a user of an alien object to refer to (get) the<br />
x coordinate of the alien and display its value in a text field. The value is held in the<br />
private variable named x at the top of the class. Using a property named xCoord, we<br />
can write:<br />
textField.setText(Integer.toString(alien.xCoord));<br />
Suppose also that we want the user to be able to change (set) the value of the x coordinate.<br />
Using the property facility we can write:<br />
alien.xCoord = 56;<br />
The way to provide these facilities is to write a property. Here is the revised class that<br />
includes the property code:<br />
public class AlienWithProperties {<br />
private int x;<br />
private int y;<br />
private int size;<br />
private ImageIcon alienImage;<br />
>