June 29, 2002 - Implementing the Integer Version of the ICopyObj Interface
June 29, 2002 Implementing the Integer Version of the ICopyObj Interface Tips: June 2002
Yehuda Shiran, Ph.D.
|
Object
, and implement it in classes that support actual types as integer, double, and user-defined classes. The code below defines the interface ICopyObj
which is implemented by the class CopyInt
. This class defines two methods: the constructor CopyInt()
and the interface function Copy()
. The main program creates two variables, h1
and h2
. The variable h1
is generated through the CopyInt()
constructor. The variable h2
is generated through the Copy()
operator. We print both variables at the end. Here is the code listing:
// compile with: jsc interface.js
interface ICopyObj {
function Copy() : Object;
}
class CopyInt implements ICopyObj {
public var i : int;
public function CopyInt(i : int) {
this.i = i;
}
public function Copy() : Object {
return new CopyInt(i)
}
}
var h1 : CopyInt = new CopyInt(8);
var h2 : CopyInt = h1.Copy();
print("Original: " + h1.i + " Copy: " + h2.i);
And here is the Command Prompt window, showing the code again (interface.js
), the jsc
compiler's log messages, and the output of interface.exe
:To learn more about JScript .NET, go to Column 111, JScript .NET, Part V: Polymorphism.