站内搜索: 请输入搜索关键词
当前页面: 图书首页 > NET For Java Developers Migrating To C#

NET For Java Developers Migrating To C#

[ directory ] Previous Section Next Section

6.3 Inheritance in Interfaces

In both C# and Java, a subclass inherits all the implemented interfaces and the corresponding implementation of its superclass. Listing 6.9 produces the same output as Listing 6.7. The BusyMan subclasses the Man class and inherits all the interfaces of the Man class.

Listing 6.9 Subclass BusyMan of Class Man (C#)
using System;
namespace Interfaces {
  class BusyMan : Man {

    public BusyMan (string name) {
      this.name = name;
    }

    static void Main(string[] args) {
      BusyMan per = new BusyMan("John");
      ILife life = (ILife)per;
      life.Happy();
      IEmotion emo = (IEmotion)per;
      emo.Happy();
    }

  }
}

To make the subclass implement specialized behavior for the ILife.Happy() and ILife.Sad() methods, it would have to implement ILife and have those implementations in those methods, as shown in Listing 6.10.

Listing 6.10 ILife Interface Implementation (C#)
using System;
namespace Interfaces {
  class BusyMan : Man, ILife {

    public BusyMan (string name) {
      this.name = name;
    }
    void ILife.Happy() {
      Console.WriteLine("Happy {0} ",name);
    }
    void ILife.Sad() {
      Console.WriteLine("Sad {0} ",name);
    }
    static void Main(string[] args) {
      BusyMan per = new BusyMan("John");
      ILife life = (ILife)per;
      life.Happy();
      IEmotion emo = (IEmotion)per;
      emo.Happy();
    }

  }
}

As in C#, in Java to get specific behavior for the happy() and sad() methods for the BusyMan class, BusyMan would have to implement the Life interface and implement those methods (see Listing 6.11).

Listing 6.11 BusyMan Extending the Man Class in Java
public class BusyMan extends Man {
  public BusyMan(String name) {
    this.name = name;
  }
  static final void main(String[] args) {
    BusyMan per = new BusyMan("John");
    per.happy();
    Life life = (Life)per;
    life.happy();
    Emotion emo = (Emotion)per;
    emo.happy();
  }

}

In C#, just as in Java, interfaces can extend other interfaces. Listing 6.12 shows an IRichLife interface that extends the ILife interface.

Listing 6.12 IRichLife Extending the ILife Interface (C#)
namespace Interfaces {
  interface IRichLife : ILife {
    void BuyMansion();
  }
}

Let's write a new class called RichMan that implements IRichLife. Listing 6.13 shows the code for the RichMan class. Note that all the methods are prefixed with the interface name. This is a good C# coding practice because it eliminates any ambiguity regarding which interface the method came from.

Listing 6.13 RichMan Implementing the IRichLife Interface (C#)
using System;
namespace Interfaces {
  class RichMan : IRichLife {

    protected string name;
    public RichMan(string name) {
      this.name = name;
    }

    void ILife.Birth() {
      Console.WriteLine("Birth of {0}",this.name);
    }

    void ILife.Death() {
      Console.WriteLine("Death of {0}",this.name);
    }
    void ILife.Happy() {
      Console.WriteLine("{0} is happy",this.name);
    }
    void ILife.Sad() {
      Console.WriteLine("{0} is sad",this.name);
    }
    void IRichLife.BuyMansion() {
      Console.WriteLine("{0} buys a mansion",this.name);
    }

    static void Main(string[] args) {

      RichMan per = new RichMan("John");
      ILife life = (ILife)per;
      life.Happy();
      IRichLife richlife = (IRichLife)per;
                              richlife.Happy();
      richlife.BuyMansion();
    }
  }
}

The output of Listing 6.13 is as follows:

John is happy
John is happy
John buys a mansion

When you specify an explicit interface declaration (such as the Interface.Method name in the method signature), the method must be a member of the interface. For the interface IRichLife, the only member is the BuyMansion() method. The Sad(), Happy(), Birth(), and Death() methods are members of the ILife interface and not the IRichLife interface. Hence, a declaration such as IRichLife.Happy() would not be compiled.

Note that although you cannot explicitly declare the IRichLife.Happy() method, the IRichLife interface does inherit that method from the ILife interface. In fact, in the Main method the richlife.Happy() method is as valid as the life.Happy() call, and both print output.

    [ directory ] Previous Section Next Section