2011-09-13 121 views
7

我需要一种方法来创建一个静态类,其中一些常量可以是特定的,但硬编码。静态抽象类

我真正想要做的是有一个类,其中提供了几个常量时扩展类 - 我想'常量'硬编码。我想我会做一些抽象的属性,并定义get {return constant; }扩展类时。

我知道这是不可能的,所以现在我面临两个选择,我想知道什么是最好的,为什么(如果有我错过选项,请让我知道!)

  1. 创建带有空字段的静态类,如果在调用静态方法时字段为空,则抛出异常。
  2. 放弃静态类。具有抽象属性的非静态类,并在需要的地方创建对象的实例,即使所有功能都是静态的。

我知道这可能是主观的和依赖于个案的,但是当我想到这个问题时我会绕着圈子走,并且真的可以用一些外部输入。我希望可以不做我想做的事情,我只是想着这个错误。

更新:代码:我将尝试编写一些描述我想完成的代码。 我知道这段代码无法工作!

想象一下,抽象类Calculation是在一个dll中,被许多项目使用。它们的功能都是一样的,只是常量因项目而异。

public abstract static class Calculation 
{ 
    private abstract int Constant { get; } //The constant is unknown at this time 

    public static int Calculate(int inputValue) 
    { 
     return inputValue * Constant; 
    } 
} 

计算值是在一个单独项目在需要的功能和常数是已知的定义的类。

public static class Calc : Calculation 
{ 
    private override int Constant { get { return 2; } 
} 

... 

static class Program 
{ 
    [STAThread] 
    static void Main() 
    { 
     //At some point: 
     int result = Calc.Calculate(6); 
    } 
} 

我想最简单的方法是创建一个非静态类,并创建一个实例,但是我怕具有类的几个实例可能是昂贵的,并且想阻止,如果可能的。

我看不出如何将它写成单例模式,而无需在每个项目中再次写入 - 在dll中只有嵌套类。这并不妨碍实现者创建一个普通的类,并有可能重新开始使用代码的每个项目的辩论。

更新#2:我与方案一彪是这样的:

类在DLL:

public static class Calculation 
{ 
    public int? Constant {get; set;} 

    public static int Calculate(int inputValue) 
    { 
     if (Constant == null) 
      throw new ArgumentNullException(); 

     return inputValue * (int)Constant; 
    } 
} 

在一个单独的项目中的功能的使用方法:

static class Program 
{ 
    [STAThread] 
    static void Main() 
    { 
     //At some point: 
     Calculation.Constant = 2; 
     int result = Calc.Calculate(6); 
    } 
} 

选项一是非常简单和优雅,什么都困扰我,没有什么实现者设置常量。我担心一个(不可能的)情景,在这种情况下,一个不起眼的角落案件将导致财产不被设置,并且代码失败(并且最后一名嫌疑人不断)...

+0

为什么你认为你需要一个静态类?而“特定案例,但硬编码”是什么意思?你能发表一个具体的例子吗? – svick

+0

您无法扩展(继承)静态类,那么您的场景究竟是什么?一小段代码值1000字。 –

+0

你可以用一些示例来描述 - 可能不是可编译的代码吗? –

回答

6

您可以创建跟随单例的非静态类,确保只有一个对象实例存在。我想这可能是下一个最好的事情。

+1

我没有看到单身人士在这种情况下如何提供帮助。问题是关于继承(不是单点的强点)和静态属性。 –

+0

[Anne Lagang]的评论(http://stackoverflow.com/users/906815/anne-lagang):“如果你需要Singleton的帮助,请查看Jon Skeet的[Singleton](http://csharpindepth.com/Articles/ General/Singleton.aspx)文章。“ –

5

你不能在同一时间想要静态和继承!它只是做出敏感!

如果您需要重写行为,您需要继承!

如果你想调用(静力学的优势之一)的简单,你可以使用厂(或单身人士,如果只需要一个实例)

我的猜测是,你可能不得不重新考虑你的模型。你的这一组常量可能代表你可以在一个单独的类中提取的东西,然后将这个类传递给你的静态方法。这是否符合您的需求?

1

编辑

为了您的代码示例:

public abstract static class Calculation 
{ 
    public static int Constant { get; set; } 
    public static int Calculate(int i) { return i * Constant; } 
} 

// ... 

Calculation.Constant = 6; 
Calculation.Calculate(123); 

有点更普遍的:

public abstract static class Calculation 
{ 
    public struct Context 
    { 
     public int Constant, SignificantDigits; 
     public bool Radians; 
    } 
    public static int Calculate(int i, Context ctx) { return i * ctx.Constant; } 
} 

// ... 
Calculation.Calculate(123, new Calculate.Context { Constant = 6 }); 

第一个想法:

我能想到的最接近的是仿制药:

public interface ISpecifics 
{ 
    void DoSomething(); 
    string SomeProp { get; } 
} 

public static class Static<S> 
    where S : ISpecifics, new() 
{ 

     public static string ExerciseSpecific() 
     { 
      var spec = new S(); 
      spec.DoSomething(); 
      return spec.SomeProp; 
     } 
} 

或者,如果你真的需要单一的静态类型

public static class Static 
{ 
     public static string ExerciseSpecific<S>() 
      where S : ISpecifics, new() 
     { 
      var spec = new S(); 
      spec.DoSomething(); 
      return spec.SomeProp; 
     } 
} 

这是否帮助?

+0

要编辑:这是我在原始问题中通过选项1进行修改。该属性是int吗?常量{get; set;}和Calculate(int inputValue){if(Constant == null)throw new NullArgumentException();返回inputValue *(int)常量; } –

+0

@David:我想我还在编辑中。现在检查我的答案。 (如果更合适,你可以使用'int?'无论我说'int') – sehe

+0

实际的类型不会是int ?.它可能是字节[],我试图做的一点是它可以为空,这将表明一个错误。上下文的更一般选项不适用于我。请阅读我的问题的底线,以了解为什么我不想使用此选项。 –

0

我需要几乎相同的东西,所以首先我做了一个具有所有功能的非静态类。 然后,在其静态构造函数中实例化一个这样的非静态类的静态类。 然后任何静态方法调用相应的实例方法。

事情是这样的:

public class CalculationInstance 
{ 
    private int constant; 

    public int Calculate(int inputValue) 
    { 
     return inputValue * constant; 
    } 

    public void AnyOtherMethod() 
    { 
    .... 
    }  

    public CalculationInstance(int constant) 
    { 
    this.constant=constant;  
    } 
} 


public static class Calculation 
{ 
    const int CONSTANT=2; 
    private CalculationInstance calc; 

    static Calculation() 
    { 
     calc=new CalculationInstance(CONSTANT); 
    } 

    public static int Calculate(int inputValue) 
    { 
     return calc.Calculate(inputValue); 
    } 

    public static void AnyOtherMethod() 
    { 
     calc.AnyOtherMethod(); 
    }  

} 

static class Program 
{ 
    [STAThread] 
    static void Main() 
    { 
     //At some point: 
     int result = Calculation.Calculate(6); 
    } 
}