Get 20M+ Full-Text Papers For Less Than $1.50/day. Start a 14-Day Trial for You or Your Team.

Learn More →

A Programmer’s Introduction to C# 2.0Interfaces

A Programmer’s Introduction to C# 2.0: Interfaces CHAPTER 10 ■ ■ ■ Interfaces are closely related to abstract classes that have all members abstract. A Simple Example The following code defines the interface IScalable and the class TextObject, which implements the interface, meaning that it contains implementations of all the functions defined in the interface: public class DiagramObject public DiagramObject() {} interface IScalable void ScaleX(float factor); void ScaleY(float factor); // A diagram object that also implements IScalable public class TextObject: DiagramObject, IScalable public TextObject(string text) this.text = text; // implementing IScalable.ScaleX() public void ScaleX(float factor) // scale the object here. // implementing IScalable.ScaleY() public void ScaleY(float factor) // scale the object here. 85 86 CH APTER 10 ■ INTERFACES private string text; class Test public static void Main() TextObject text = new TextObject("Hello"); IScalable scalable = (IScalable) text; scalable.ScaleX(0.5F); scalable.ScaleY(0.5F); This code implements a system for drawing diagrams. All the objects derive from DiagramObject, so they can implement common virtual functions (not shown in this example). Some of the objects can be scaled, and this is expressed by the presence of an implementation of the IScalable interface. Listing the interface name with the base class name for TextObject indicates that TextObject implements the interface. This means http://www.deepdyve.com/assets/images/DeepDyve-Logo-lg.png

A Programmer’s Introduction to C# 2.0Interfaces

Editors: Gunnerson, Eric; Wienholt, Nick

Loading next page...
 
/lp/springer-journals/a-programmer-s-introduction-to-c-2-0-interfaces-5b5XelA3lf
Publisher
Apress
Copyright
© Apress 2005
ISBN
978-1-59059-501-5
Pages
85 –97
DOI
10.1007/978-1-4302-0035-2_10
Publisher site
See Chapter on Publisher Site

Abstract

CHAPTER 10 ■ ■ ■ Interfaces are closely related to abstract classes that have all members abstract. A Simple Example The following code defines the interface IScalable and the class TextObject, which implements the interface, meaning that it contains implementations of all the functions defined in the interface: public class DiagramObject public DiagramObject() {} interface IScalable void ScaleX(float factor); void ScaleY(float factor); // A diagram object that also implements IScalable public class TextObject: DiagramObject, IScalable public TextObject(string text) this.text = text; // implementing IScalable.ScaleX() public void ScaleX(float factor) // scale the object here. // implementing IScalable.ScaleY() public void ScaleY(float factor) // scale the object here. 85 86 CH APTER 10 ■ INTERFACES private string text; class Test public static void Main() TextObject text = new TextObject("Hello"); IScalable scalable = (IScalable) text; scalable.ScaleX(0.5F); scalable.ScaleY(0.5F); This code implements a system for drawing diagrams. All the objects derive from DiagramObject, so they can implement common virtual functions (not shown in this example). Some of the objects can be scaled, and this is expressed by the presence of an implementation of the IScalable interface. Listing the interface name with the base class name for TextObject indicates that TextObject implements the interface. This means

Published: Jan 1, 2005

There are no references for this article.