Tag: 静态

Java错误:无法对非静态方法进行静态引用

我正在编写一个Android应用程序,并得到这个错误,但我不知道为什么。 有人可以帮我理解为什么我得到这个错误? Cannot make a static reference to the non-static method updateScores(List<Score>) from the type DatabaseHandler 这是相关的代码。 public class ScoreList extends SherlockFragmentActivity { List<Score> listScore = new ArrayList<Score>(); public void updateListView() { listViewScore.setAdapter(new ScoreListAdapter(ctx, R.layout.score_row_item, listScore)); DatabaseHandler.updateScores(listScore); } } 这是DatabaseHandler类。 我试着让这个函数成为静态的,但是由于错误而不能这样工作。 public class DatabaseHandler extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private […]

C#静态成员“inheritance” – 为什么这存在呢?

在C#中,超类的静态成员被“inheritance”到子类作用域中。 例如: class A { public static int M() { return 1; } } class B : A {} class C : A { public new static int M() { return 2; } } […] AM(); //returns 1 BM(); //returns 1 – this is equivalent to AM() CM(); //returns 2 – this is not equivalent […]

C ++初始化类中的静态variables?

我注意到我的一些函数实际上并没有访问这个对象,所以我把它们变成static 。 然后,编译器告诉我,他们访问的所有variables也必须是静态的 – 到目前为止,这是可以理解的。 我有一堆stringvariables如 string RE_ANY = "([^\\n]*)"; string RE_ANY_RELUCTANT = "([^\\n]*?)"; 等等。 然后我使它们都是static const因为它们永远不会改变。 但是,我的程序只编译,如果我把它们从类中移出:否则,MSVC ++ 2010抱怨“只有静态常量积分variables可能在一个类中初始化”。 那很不幸。 有没有解决方法? 我想把他们留在他们所属的class级里面。

头文件中的variables声明 – 是否是静态的?

当重构一些#defines我在C ++头文件中遇到类似下面的声明: static const unsigned int VAL = 42; const unsigned int ANOTHER_VAL = 37; 问题是,静态会有什么不同呢? 请注意,由于经典的#ifndef HEADER #define HEADER #endif技巧(如果有的话),多个包含头文件是不可能的。 静态意味着只创build一个VAL副本,以防头文件被多个源文件包含在内?

类方法中的静态variables

有人可以请解释如何静态方法variables在C ++中工作…如果我有以下类: class A { void foo() { static int i; i++; } } 如果我声明A的多个实例,在一个实例上调用foo()是否会在所有实例上增加静态variablesi? 还是只有被召唤的那个? 我假定每个实例都有自己的i副本,但是通过一些我似乎已经指出的代码。

C ++重载静态函数与非静态函数

我想打印两个不同的东西,取决于是否用Foo::print()静态调用函数或从Foo foo; foo.print();的实例中调用函数Foo foo; foo.print(); Foo foo; foo.print(); 编辑:这是一个类定义,绝对不能正常工作,已经有几个人回答了。 class Foo { string bla; Foo() { bla = "nonstatic"; } void print() { cout << bla << endl; } static void print() { cout << "static" << endl; } }; 但是,有没有一个很好的方法来达到这个效果呢? 基本上我想这样做: if(this is a static call) do one thing else do another thing 换句话说,我知道PHP可以检查*thisvariables是否被定义,以确定函数是否被静态调用。 […]

C#中的静态类初始化的顺序是确定性的吗?

我做了一些search,我认为下面的代码是保证产生输出: BX = 7 BX = 0 AX = 1 A = 1,B = 0 static class B { public static int X = 7; static B() { Console.WriteLine("BX = " + X); X = AX; Console.WriteLine("BX = " + X); } } static class A { public static int X = BX + 1; […]

静态块和静态variables以什么顺序执行?

可能重复: Java静态类的初始化 为什么stringvariables在初始化块中更新,而不是整数(即使块先写入) class NewClass { static { System.out.println(NewClass.string+" "+NewClass.integer); } final static String string="static"; final static Integer integer=1; public static void main(String [] args)//throws Exception { } } 我的输出是 static null PS:还注意到stringvariables初始化发生在块之前,只有当我插入最后修改。 为什么呢?为什么不是整数?我也宣布它是最后的静态

PHP:静态和非静态函数和对象

这些对象调用有什么区别? 非静态: $var = new Object; $var->function(); 静态的: $var = User::function(); 而且还在一个class里面,为什么我应该使用静态属性的function? 例: static public function doSomething(){ …code… }

返回一个指向静态局部variables安全的指针?

我正在使用一些广泛使用返回指向静态局部variables的方式的代码。 例如: char* const GetString() { static char sTest[5]; strcpy(sTest, "Test"); return sTest; } 我是否认为这是安全的? PS,我知道这是做同样事情的一个更好的方法: char* const GetString() { return "Test"; } 编辑:道歉,function签名当然应该是: const char* GetString();