You need only provide the implementation for the width property here. The implementation for the Area()
and Perimeter()
methods is inherited from the Square
class.
The last implementation is the ICircle
interface, for which you will implement the radius
property as well as the Perimeter()
and Area()
methods:
public class Circle : ICircle {
public double radius { get; set; }
public double Perimeter() {
return (2 * Math.PI * (this.radius));
}
//---provide the implementation for the virtual method---
public double Area() {
return (Math.PI * Math.Pow(this.radius, 2));
}
}
Figure 6-10 shows the classes that you have implemented for these three interfaces.
Figure 6-10
Explicit Interface Members Implementation
A class can implement one or more interfaces. To implement a member in an interface, you simply need to match the member name and its signature with the one defined in the interface. However, there are times when two interfaces may have the same member name and signature. Here's an example:
public interface IFileLogging {
void LogError(string str);
}
public interface IConsoleLogging {
void LogError(string str);
}
In this example, both IFileLogging
and IConsoleLogging
have the same LogError()
method. Suppose that you have a class named Calculation
that implements both interfaces:
public class Calculation : IFileLogging, IConsoleLogging {}
The implementation of the LogError()
method may look like this:
public class Calculation : IFileLogging, IConsoleLogging {
//---common to both interfaces---
public void LogError(string str) {
Console.WriteLine(str);
}
}
In this case, the LogError()
method implementation will be common to both interfaces and you can invoke it via an instance of the Calculation
class:
Calculation c = new Calculation();
//---prints out: Some error message here---
c.LogError("Some error message here");
In some cases, you need to differentiate between the two methods in the two interfaces. For example, the LogError()
method in the IFileLogging
interface may write the error message into a text file, while the LogError()
method in the IConsoleLogging
interface may write the error message into the console window. In that case, you must explicitly implement the LogError()
method in each of the two interfaces. Here's how:
public class Calculation : IFileLogging, IConsoleLogging {
//---common to both interfaces---
public void LogError(string str) {
Console.WriteLine(str);
}
//---only available to the IFileLogging interface---
void IFileLogging.LogError(string str) {
Console.WriteLine("In IFileLogging: " + str);
}
//---only available to the IConsoleLogging interface---
void IConsoleLogging.LogError(string str) {
Console.WriteLine("In IConsoleLogging: " + str);
}
}
This example has three implementations of the LogError()
method:
□ One common to both interfaces that can be accessed via an instance of the Calculation
class.
□ One specific to the IFileLogging
interface that can be accessed only via an instance of the IFileLogging
interface.
□ One specific to the IConsoleLogging
interface that can be accessed only via an instance of the IConsoleLogging
interface.
You cannot use the public
access modifier on the explicit interface methods' implementation.
To invoke these implementations of the LogError()
method, use the following statements:
//---create an instance of Calculation---
Calculation c = new Calculation();
//---prints out: Some error message here---
c.LogError("Some error message here");
//---create an instance of IFileLogging---
IFileLogging f = c;
//---prints out: In IFileLogging: Some error message here---
f.LogError("Some error message here");
//---create an instance of IConsoleLogging---
IConsoleLogging l = c;
//---prints out: In IConsoleLogging: Some error message here---
l.LogError("Some error message here");
Another use of explicit interface member implementation occurs when two interfaces have the same method name but different signatures. For example:
public interface IFileLogging {
void LogError(string str);
}
public interface IConsoleLogging {
void LogError();
}
Here, the LogError()
method in the IFileLogging
interface has a string input parameter, while there is no parameter in the IConsoleLogging
interface. When you now implement the two interfaces, you can provide two overloaded LogError()
methods, together with an implementation specific to each interface as illustrated here:
public class Calculation : IFileLogging, IConsoleLogging {
//---common to both interfaces---
public void LogError(string str) {
Console.WriteLine("In LogError(str): " + str);
}
public void LogError() {
Console.WriteLine("In LogError()");
}
//---only available to the IFileLogging interface---
void IFileLogging.LogError(string str) {
Console.WriteLine("In IFileLogging: " + str);
}
//---only available to the IConsoleLogging interface---
void IConsoleLogging.LogError() {
Console.WriteLine("In IConsoleLogging");
}
}
As you can see , the first two LogError()
methods are overloaded
and are common to both interfaces. This means that you can access them via an instance of the Calculation
class. The next two implementations are specific to the IFileLogging
and IConsoleLogging interfaces and can be accessed only via an instance of each interface:
//---create an instance of Calculation---
Calculation c = new Calculation();
//---prints out: In LogError()---
c.LogError();
//---prints out: In LogError(str)---
c.LogError("Some error message here");
//---create an instance of IFileLogging---
Читать дальше