You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/ch6_definingclasses.ptx
+17-12Lines changed: 17 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -62,11 +62,11 @@
62
62
</p>
63
63
64
64
<p>
65
-
Here is a mostly complete implementation of a Fraction class in Python that we will refer to throughout this section:
65
+
<xrefref="python-fraction-class-all"text="type-global"/> is a mostly complete implementation of a Fraction class in Python that we will refer to throughout this section.
The instance variables (<term>data members</term>) we will need for our fraction class are the numerator and denominator. Of course in Python we can add instance variables to a class at any time by simply assigning a value to <c>objectReference.variableName</c>, whereas in Java all data members must be declared up front.
133
134
</p>
134
135
135
136
<p>
136
-
The declarations of instance variables can come at the beginning of the class definition or the end. Cay Horstman, author of <urlhref="https://horstmann.com/corejava/index.html"visual="https://horstmann.com/corejava/index.html">the “Core Java” books</url> puts the declarations at the end of the class. I like them at the very beginning so you see the variables that are declared before you begin looking at the code that uses them. With that in mind the first part of the Fraction class definition is as follows:
137
+
The declarations of instance variables can come at the beginning of the class definition or the end. Cay Horstman, author of <urlhref="https://horstmann.com/corejava/index.html"visual="https://horstmann.com/corejava/index.html">the “Core Java” books</url> puts the declarations at the end of the class. I like them at the very beginning so you see the variables that are declared before you begin looking at the code that uses them. With that in mind <xrefref="java-fraction-class-start"text="type-global"/> shows the first part of the Fraction class definition.
Notice that we have declared the numerator and denominator to be <term>private</term>.
151
-
This means that the compiler will generate an error if another method tries to write code like the following:
154
+
This means that the compiler will generate an error if another method tries to write code like <xrefref="java-private"text="type-global"/>.
152
155
</p>
153
156
154
-
155
-
<programxml:id="java-private"language="java">
157
+
<listingxml:id="java-private">
158
+
<programinteractive="activecode"language="java">
156
159
<code>
157
160
Fraction f = new Fraction(1,2);
158
161
Integer y = f.numerator * 10;
159
162
</code>
160
163
</program>
164
+
</listing>
161
165
162
166
<p>
163
167
<idx>getter method</idx>
164
168
<idx>setter method</idx>
165
169
Direct access to instance variables is not allowed in Java.
166
170
Therefore if we legitimately want to be able to access information such as the numerator or the denominator for a particular fraction we must have a <term>getter method</term> that returns the needed value.
167
-
Hence, it is a very common programming practice to both provide <term>getter methods</term> and <term>setter methods</term> when needed for instance variables in Java.
171
+
Hence, it is a very common programming practice to both provide <term>getter methods</term> and <term>setter methods</term> when needed for instance variables in Java. <xrefref="java-getter-and-setter"text="type-global"/> shows how the getter and setter methods are written.
0 commit comments