标签: static-methods

在.NET中重构大型方法

如果我有一个大的.NET方法,我想知道将它拆分成多种方法是否是一种好的做法.我担心的是:1 - 如果只调用一次方法,是否有任何创建方法的点?
2 - 如果我在我的类中调用私有方法,我尽量不使用实例变量.这是好事还是坏事?例如:

var a, b;
public void TestMe
{
   FirstTest(a,b);
}
private void FirstTest(paramA, paramB)
{
         //code here with passed in parameters
}

//instead of:
private void FirstTest()
{
      //code here with instance variables a,b
}  
Run Code Online (Sandbox Code Playgroud)

编辑:用实例替换全局

language-agnostic methods refactoring static-methods

8
推荐指数
3
解决办法
379
查看次数

静态方法中的静态变量在基类和继承中

我有这些C++类:

class Base
{
protected:
    static int method()
    {
        static int x = 0;
        return x++;
    }
};

class A : public Base
{

};

class B : public Base
{

};
Run Code Online (Sandbox Code Playgroud)

x静态变量之间共享AB,或将他们中的每一个都有它自己的独立x变量(这是我想要什么)?

c++ inheritance static-methods static-variables

8
推荐指数
1
解决办法
3389
查看次数

如何避免字典中的空键错误?

如果key为null,如何避免错误?

//Getter/setter
public static Dictionary<string, string> Dictionary
{
    get { return Global.dictionary; }
    set { Global.dictionary = value; }
}
Run Code Online (Sandbox Code Playgroud)

更新:

Dictionary.Add("Key1", "Text1");
Dictionary["Key2"] <-error! so what can I write in the GET to avoid error?
Run Code Online (Sandbox Code Playgroud)

谢谢.

问候

c# static-methods

8
推荐指数
2
解决办法
1万
查看次数

如何调用模板类的静态成员?

如果我有:

template <class T>
class A
{
 static void f()
 {
  // not using template parameter T
 }
};
Run Code Online (Sandbox Code Playgroud)

在这种情况下A<int>::f()是相同的A<double>::f(),但我不希望A::f()通过模板参数调用.是否有允许调用f()但不需要模板参数的语法?

c++ templates static-methods

8
推荐指数
1
解决办法
9242
查看次数

为什么需要将静态方法包装到类中?

对不起这个问题没有学问的性质.如果有一个简单的答案,只需要一个解释链接就会让我感到高兴.

编程6个月后,我发现静态类对于存储适用于许多不同类的例程有些用处.这是我如何使用静态类的简化示例,它是一个用于将文本解析为各种内容的类

public static class TextProcessor 
{
    public static string[] GetWords(string sentence)
    {
        return sentence.Split(' '); 
    }

    public static int CountLetters(string sentence)
    {
        return sentence.Length; 
    }

    public static int CountWords(string sentence)
    {
        return GetWords(sentence).Length; 
    }
}
Run Code Online (Sandbox Code Playgroud)

我用这个明显的方式使用它

    class Program
{
    static void Main(string[] args)
    {
        string mysentence = "hello there stackoverflow.";
        Console.WriteLine("mysentence has {0} words in it, fascinating huh??", TextProcessor.CountWords(mysentence)); 

        Console.ReadLine(); 
    }
} 
Run Code Online (Sandbox Code Playgroud)

我的问题是:为什么有必要将这些静态方法包装在静态类中?它似乎没有任何意义.有没有办法让我们可以将这些方法单独包装在一个类中?我知道封装是有益的,但我没有看到静态类包含静态方法的用法.有没有我风格或其他方面缺少的东西?我完全吠了一棵傻树吗?我想的太多了吗?

c# static-methods

8
推荐指数
1
解决办法
1302
查看次数

静态块与静态方法 - 初始化静态字段

出于好奇,我测量了静态块和静态方法初始化器之间的性能.首先,我在两个独立的java类中实现了上述方法,如下所示:

第一:

class Dummy {
    static java.util.List<Integer> lista = new java.util.ArrayList<Integer>();
    static {
        for(int i=0; i < 1000000; ++i) {
            lista.add(new Integer(i));
        }
    }
}

public class First {
    public static void main(String[] args) { 
        long st = System.currentTimeMillis();
            Dummy d = new Dummy();
        long end = System.currentTimeMillis() - st;
        System.out.println(end);    
    }
}
Run Code Online (Sandbox Code Playgroud)

第二:

class Muddy {
    static java.util.List<Integer> lista = new java.util.ArrayList<Integer>();
    public static void initList() {
        for(int i=0; i < 1000000; ++i) {
            lista.add(new Integer(i));
        }
    }
} …
Run Code Online (Sandbox Code Playgroud)

java static-methods static-members performance-testing

8
推荐指数
1
解决办法
5665
查看次数

Scala中的抽象静态方法

我已经阅读了这篇相关文章,但没有太多具体的答案(或多或少的语言设计不好): 为什么静态方法不能在Java中抽象化

我有点像Scala的新人,这可能吗(可能有特征或什么)?

我尝试让我的基类扩展一个特征,但是当我真的希望它们需要在伴随对象中实现时,需要子类来实现抽象静态方法作为成员方法.

abstract-class static-methods scala

8
推荐指数
1
解决办法
6170
查看次数

目标C中的静态方法(不是类方法)

在阅读这个问题并接受问题的答案时,我无法区分这两种方法.实际上通过阅读示例得到了重点,但后来,我无法编写自己的静态方法.

我尝试在目标c静态方法中使用googling 创建静态方法

这让我回到了这个这个问题的链接.但是,这里的例子是CLASS方法,根据问题的第一个链接.这让我感到困惑.

这里的任何人都可以告诉我如何创建一个不是类方法静态方法

任何关于此的亮点都将受到赞赏.

static-methods objective-c class-method

8
推荐指数
1
解决办法
8389
查看次数

findViewById在静态方法中

我有这个静态方法:

public static void displayLevelUp(int level, Context context) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.custom_level_coast,
            (ViewGroup) findViewById(R.id.toast_layout_root));  // this row

    TextView text = (TextView) layout.findViewById(R.id.toastText);
    text.setText("This is a custom toast");

    Toast toast = new Toast(context);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();

    Toast.makeText(context, String.valueOf(level), Toast.LENGTH_SHORT)
            .show();

}
Run Code Online (Sandbox Code Playgroud)

但是,我无法弄清楚如何让第一个findViewById玩得很好,因为它说它是一种非静态方法.我理解为什么会这么说,但必须有一个解决方法?我context进入了这个方法,但无法一起完成它们.

android static-methods findviewbyid

8
推荐指数
1
解决办法
1万
查看次数

我可以使用EmberJS mixins添加(静态)类方法

在标准的ember mixin示例中,我们添加了实例方法/属性:http://emberjs.com/api/classes/Ember.Mixin.html

使用reopenClass我们可以添加类方法(静态方法),给我们类似的东西:

UninstantiatedClass.findAll()
Run Code Online (Sandbox Code Playgroud)

我可以创建一个添加类方法的mixin吗?

javascript static-methods class-method ember.js

8
推荐指数
1
解决办法
2654
查看次数