Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   Inheriting an Interface

In this section, I’ll show how to define a type that implements an interface, and then I’ll show how to create an instance of this type and use the object to call the interface’s methods. C# actually makes this pretty simple, but what happens behind the scenes is a bit more complicated. I’ll explain what is happening behind the scenes later in this chapter.

The System.IComparable<T> interface is defined (in MSCorLib.dll) as follows.

 

public interface IComparable<in T> { Int32 CompareTo(T other);

}

 

The following code shows how to define a type that implements this interface and also shows code

that compares two Point objects.

 

using System;

 

// Point is derived from System.Object and implements IComparable<T> for Point. public sealed class Point : IComparable<Point> {

private Int32 m_x, m_y;

 

public Point(Int32 x, Int32 y) { m_x = x;

m_y = y;

}

 

// This method implements IComparable<T>.CompareTo() for Point public Int32 CompareTo(Point other) {

return Math.Sign(Math.Sqrt(m_x * m_x + m_y * m_y)

­ Math.Sqrt(other.m_x * other.m_x + other.m_y * other.m_y));

}

 

public override String ToString() {

return String.Format("({0}, {1})", m_x, m_y);

}

}


public static class Program { public static void Main() {

Point[] points = new Point[] { new Point(3, 3),

new Point(1, 2),

};

 

// Here is a call to Point's IComparable<T> CompareTo method if (points[0].CompareTo(points[1]) > 0) {

Point tempPoint = points[0]; points[0] = points[1]; points[1] = tempPoint;

}

Console.WriteLine("Points from closest to (0, 0) to farthest:"); foreach (Point p in points)

Console.WriteLine(p);

}

}

 

The C# compiler requires that a method that implements an interface be marked as public. The CLR requires that interface methods be marked as virtual. If you do not explicitly mark the method as virtual in your source code, the compiler marks the method as virtual and sealed; this prevents a derived class from overriding the interface method. If you explicitly mark the method as virtual, the compiler marks the method as virtual (and leaves it unsealed); this allows a derived class to override the interface method.

If an interface method is sealed, a derived class cannot override the method. However, a derived class can re-inherit the same interface and can provide its own implementation for the interface’s methods. When calling an interface’s method on an object, the implementation associated with the object’s type is called. Here is an example that demonstrates this.

 

using System;

 

public static class Program { public static void Main() {

/************************* First Example *************************/ Base b = new Base();

 

// Calls Dispose by using b's type: "Base's Dispose" b.Dispose();

 

// Calls Dispose by using b's object's type: "Base's Dispose" ((IDisposable)b).Dispose();



 

 

/************************* Second Example ************************/ Derived d = new Derived();

 

// Calls Dispose by using d's type: "Derived's Dispose" d.Dispose();

 

// Calls Dispose by using d's object's type: "Derived's Dispose" ((IDisposable)d).Dispose();


/************************* Third Example *************************/ b = new Derived();

 

// Calls Dispose by using b's type: "Base's Dispose" b.Dispose();

 

// Calls Dispose by using b's object's type: "Derived's Dispose" ((IDisposable)b).Dispose();

}

}

 

// This class is derived from Object and it implements IDisposable internal class Base : IDisposable {

// This method is implicitly sealed and cannot be overridden public void Dispose() {

Console.WriteLine("Base's Dispose");

}

}

 

// This class is derived from Base and it re­implements IDisposable internal class Derived : Base, IDisposable {

// This method cannot override Base's Dispose. 'new' is used to indicate

// that this method re­implements IDisposable's Dispose method new public void Dispose() {

Console.WriteLine("Derived's Dispose");

 

// NOTE: The next line shows how to call a base class's implementation (if desired)

// base.Dispose();

}

}

 

 


Date: 2016-03-03; view: 800


<== previous page | next page ==>
Nbsp;   Defining an Interface | Nbsp;   More About Calling Interface Methods
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)