June 30, 2002 - Implementing a Polymorphic Utility
June 30, 2002 Implementing a Polymorphic Utility Tips: June 2002
Yehuda Shiran, Ph.D.
|
CopyMe()
,
accepts an object of type Object
,
and returns a copy of it by using the Copy()
method. Here is the utility function:
function CopyMe(ic : Object) : Object {
return ic.Copy();
}
The Copy()
method is defined in the interface ICopyObj
:
interface ICopyObj {
function Copy() : Object;
}
The following two classes, CopyInt
and CopyDouble
, implement this interface for integer and double values, respectively:
class CopyInt implements ICopyObj {
public var i : int;
public function CopyInt(i : int) {
this.i = i;
}
public function Copy() : Object {
return new CopyInt(i)
}
}
class CopyDouble implements ICopyObj {
public var d : double;
public function CopyDouble(d : double) {
this.d = d;
}
public function Copy() : Object {
return new CopyDouble(d)
}
}
To learn more about JScript .NET, go to Column 111, JScript .NET, Part V: Polymorphism.