To implement an interface, you declare a class or structure that inherits from the interface and that implements all the methods specified by the interface . For example, suppose you are defining the Mammal hierarchy but you need to specify that land-bound mammals provide a method named NumberOfLegs that returns as an int the number of legs that a mammal has . (Sea-bound mammals do not implement this interface .) You could define the ILandBound interface that contains this method as follows:
interface ILandBound
{
int NumberOfLegs();
}
You could then implement this interface in the Horse class . You inherit from the interface and provide an implementation of every method defined by the interface .
class Horse : ILandBound
{
...
public int NumberOfLegs()
{
return 4;
}
}
interface ILandBound
{
int NumberOfLegs();
}
You could then implement this interface in the Horse class . You inherit from the interface and provide an implementation of every method defined by the interface .
class Horse : ILandBound
{
...
public int NumberOfLegs()
{
return 4;
}
}
No comments:
Post a Comment