Java Reference
In-Depth Information
using the keyword
this
. The example below illustrates the essential difference
between the two ways of programming the
Volume
functionality.
Program 8.10
Object-oriented method for computing the volume of a 3D
box
class
Box
double
width , height , depth ;
Box(
double
w,
double
h,
double
d)
this
.width=w;
this
. height=h;
this
.depth=d;
}
double
Volume ()
{
return this
.width
∗
this
. height
∗
this
.depth;
}
}
class
OOstyle
static double
Volume(Box box )
{
return
box . width
∗
box. height
∗
box . depth ;
}
public static void
main( String [ ]
s )
Box myBox=
new
Box(5,2,1);
System . out . println (
"Volume by static method:"
+Volume (myBox) ) ;
System . out . println (
"Volume by object method:"
+myBox . V o l u m e ( ) )
;
}
}
8.5 Revisiting object-oriented style
data-structures
8.5.1 Object oriented priority queues
The former
maxHeap
/
add
/
removeTop
static functions operating on the array
can thus be translated as equivalent methods acting on the object. A heap
object now declares the array as an object field without that static keyword.
Therefore, we can build several heaps at a time. The prototypes for the heap
methods are as follows:
Search WWH ::
Custom Search