2009-06-26 85 views
4

我为新项目编写接口,并希望得到一些建议。强制接口实现在c#中实现层次结构

我有一个类有一个子类,它有一个子类。这种类的树是这样的:

Class Car 
{ 
    Wheels Wheel; 
} 
Class Wheels 
{ 
    Rims Rim; 
} 

所以为了简化:一辆车有一个车轮,一个车轮有一个轮辋。 (不能做出其他更好的例子,对不起)。

所以我想强制这个层次结构在我的接口实现ICar,IWheels和IRims。

所以我做了这样的事情(在C#):

ICar 
{ 
    IWheels Wheel; 
} 
IWheels 
{ 
    IRims Rim; 
} 

,我有,我不能在接口实现字段错误。所以这开始了我的事情,也许它是错误的界面设计。我想强制接口实现来实现这种层次结构。但是也许应该以其他方式来设计模式和最佳实践呢?

你能告诉我如何设计我的系统,使对象将被迫实现这种层次?

也许有东西在我的问题不准确或遗漏我一些重要的信息。如果是,请在评论中提问。

回答

10

在你的界面,你必须说清楚,车轮应该是ICAR的属性,因为你不能声明哪些字段接口的实现应该有。 (字段是内部工作,所以界面不应该知道它)。

interface ICar 
{ 
    IWheels Wheels 
    { 
     get; 
    } 
} 
4

由于错误提示,您不能在接口中指定字段。您可以指定虽然性能:

interface ICar 
{ 
    IWheels Wheel { get; set; } 
} 

interface IWheels 
{ 
    IRims Rim { get; set; } 
} 
7

您可以在界面上没有指定(你不应该能够 - 这是一个实现的决定),但你可以指定一个属性

public interface ICar 
{ 
    IWheels Wheel { get; set; } 
} 

public interface IWheels 
{ 
    IRims Rim { get; set; } 
} 

您可能希望只放在界面,吸气,虽然 - 这是有点不同寻常,包括在接口二传手:

public interface ICar 
{ 
    IWheels Wheel { get; } 
} 

public interface IWheels 
{ 
    IRims Rim { get; } 
} 

(有古怪,如果你想覆盖现有的(或抽象的)财产只有一个getter添加一个二传手,但也没关系实现与setter方法“吸气剂只是”界面,以及,我相信。)

4

您不能声明字段,但可以声明属性。这将具有强制特定类提供另一个类的实例的相同最终效果。

ICar 
{ 
    IWheels Wheel { get; set; } 
} 
IWheels 
{ 
    IRims Rim { get; set; } 
} 
0

我没有那么多用于C#,但它的声音对我说,你可以强制通过使抽象类,你要使用的字段实现。 所以,如果你扩展这些抽象类,你会在他们可用的字段。 你将不得不作出一个抽象类和接口,但...

0

这是一个全功能的代码... 希望它可以帮助...

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace ConsoleApplication10 
{ 
    //Interfaces 

    public interface ICar 
    { 
     string name { get;} 
     IWheel wheel { get;} 
    } 


    public interface IWheel 
    { 
     string brand { get;} 
    } 

    //Implementations 

    public class Michelin : IWheel 
    { 
     #region IWheel Members 

     public string brand 
     { 
      get { return "michelin"; } 
     } 

     #endregion 
    } 


    public class Toyota : ICar 
    { 
     Michelin m = new Michelin(); 
     #region ICar Members 

     public string name 
     { 
      get { return "toyota"; } 
     } 

     public IWheel wheel 
     { 
      get { return m; } 
     } 

     #endregion 
    } 

    //A user of the interfaces. Only cares about ICar but knows implicitly about IWheel 

    public class Stand 
    { 
     public Stand() 
     { 
      cars = new List<ICar>(2); 
      cars.Add(new Toyota()); 
      cars.Add(new Toyota()); 
     } 
     List<ICar> cars; 

     public string ShowCars() 
     { 
      StringBuilder str = new StringBuilder(); 
      foreach (ICar iterCar in cars) 
      { 

       str.AppendLine(string.Format("car {0} with wheel {1}", 
        iterCar.name, iterCar.wheel.brand)); 
      } 
      return str.ToString(); 
     } 
    } 

    //entry point. creates a stand and shows the cars, testing that properties are visible 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Stand myLittleStand = new Stand(); 
      Console.WriteLine(myLittleStand.ShowCars()); 
     } 
    } 
} 
+0

当然,以下最佳实践属性应该是大写字母和字符串不应该被硬编码,但我认为这些都是小细节 – 2009-06-26 14:05:54