如果我有一个大的.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)
编辑:用实例替换全局
我有这些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静态变量之间共享A和B,或将他们中的每一个都有它自己的独立x变量(这是我想要什么)?
如果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)
谢谢.
问候
如果我有:
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()但不需要模板参数的语法?
对不起这个问题没有学问的性质.如果有一个简单的答案,只需要一个解释链接就会让我感到高兴.
编程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)
我的问题是:为什么有必要将这些静态方法包装在静态类中?它似乎没有任何意义.有没有办法让我们可以将这些方法单独包装在一个类中?我知道封装是有益的,但我没有看到静态类包含静态方法的用法.有没有我风格或其他方面缺少的东西?我完全吠了一棵傻树吗?我想的太多了吗?
出于好奇,我测量了静态块和静态方法初始化器之间的性能.首先,我在两个独立的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中抽象化
我有点像Scala的新人,这可能吗(可能有特征或什么)?
我尝试让我的基类扩展一个特征,但是当我真的希望它们需要在伴随对象中实现时,需要子类来实现抽象静态方法作为成员方法.
我有这个静态方法:
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进入了这个方法,但无法一起完成它们.
在标准的ember mixin示例中,我们添加了实例方法/属性:http://emberjs.com/api/classes/Ember.Mixin.html
使用reopenClass我们可以添加类方法(静态方法),给我们类似的东西:
UninstantiatedClass.findAll()
Run Code Online (Sandbox Code Playgroud)
我可以创建一个添加类方法的mixin吗?
static-methods ×10
c# ×2
c++ ×2
class-method ×2
android ×1
ember.js ×1
findviewbyid ×1
inheritance ×1
java ×1
javascript ×1
methods ×1
objective-c ×1
refactoring ×1
scala ×1
templates ×1