Now the radius of these balls is going to stay the same throughout thesimulation, while their coordinates constantly change. To express this distinction and to simplify the creation of instances of Ball, a programmer addsan initialization “equation” directly to a field declaration: see RADIUS infigure 4. By convention, we use uppercase letters for the names of fields toindicate that they are constant, shared attributes.If a field comes with an initialization “equation”, the constructor doesnot contain an equation for that field. Thus, the Ball constructor consumestwo values, x and y, and contains two “equations:” one for x and one fory. To create instances of Ball, you can now write expressions such as newBall(10,20), which produces an object with three attributes: x with value10; y with value 20; and RADIUS with value 5. Explore the creation of suchobjects in the interactions window.
đang được dịch, vui lòng đợi..