June 27, 2002 - Defining a Polymorphic Interface
June 27, 2002 Defining a Polymorphic Interface Tips: June 2002
Yehuda Shiran, Ph.D.
|
Object
, and implement it in classes that support actual types as int
, double
, and user-defined classes. Interface names usually start with I
, to distinguish them from classes.
The following example defines the ICopyObj
interface. It includes one member, Copy()
, which returns a copy of the object of type Object
:
interface ICopyObj {
function Copy() : Object;
}
The class CopyInt
implements the ICopyObj
interface for integers. It includes one property (i
), and two methods. One method is the class constructor, CopyInt()
. The other method is the interface method, Copy()
:
class CopyInt implements ICopyObj {
public var i : int;
public function CopyInt(i : int) {
this.i = i;
}
public function Copy() : Object {
return new CopyInt(i)
}
}
To learn more about JScript .NET, go to Column 111, JScript .NET, Part V: Polymorphism.