spacer
Yehuda Shiran June 29, 2002
Implementing the Integer Version of the ICopyObj Interface
Tips: June 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

One of the ways to support polymorphism is to define an interface that accepts an object of the type 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.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business