Possible Duplicate:
C#.NET - Why do members of a static class need to be declared as static? Why isn't it just implicit?
I am getting an interesting error, in that when I call a method (which I don't explicitly declare as static) from within a statically declared class, I get a message saying
An object reference is required for the non-static field, method, or property 'MangoTree.Twitter.OAuthClient.PerformRequest(System.Collections.Generic.Dictionary, string, string, string, MangoTree.Twitter.OAuthClient.RequestType)'
当我将该方法显式声明为静态时,错误就会消失,并且我可以从类声明中删除 static 修饰符,错误就会消失。让我感到困惑的是,我的印象是,当我将类声明为静态时,类中的所有内容都应该自动是静态的,而无需我显式声明它。
我使用静态方法来处理我真正想要静态的事情。我使用 ReSharper 来提高代码质量。有时 ReSharper 建议可以将方法设为静态。
当我上以下课时:
public class WhatEverClass {
private string DoSomethingFancy(string input)
{
string fancyStuff;
// Fancy Stuff here
return fancyStuff;
}
public WhatEverClass() {
string awesome=DoSomethingFancy("some fancy string");
}
}
Run Code Online (Sandbox Code Playgroud)
ReSharper 可能会说“DoSomethingFancy 可以变为静态”。
我知道它可以变成静态的,但是真的有充分的理由这样做吗?或者我应该忽略这些建议?
我在静态方法中读取 cookie 时遇到问题。我试过:
static void Method()
{
Page page = HttpContext.Current.Handler as Page;
HttpCookie reader= page.Request.Cookies["myCookie"];
}
Run Code Online (Sandbox Code Playgroud)
但我认为这不起作用。
你有什么想法我该怎么做吗?
我的代码被 AJAX UI(多线程)调用,并在数据处理后发送 Json 输出。最近在重构代码时,我们将许多常见和重复的方法转移到一个单独的文件中,在那里我们将它们设为静态,因为我们没有处理任何静态/共享数据。以下是我们静态方法的示例设计:
public class Helper
{
public static C Method1(List<A> aList, List<B> bList)
{
C objC = new C();
// Create ObjC based on inputs aList and bList
return objC;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我的理解是以下调用不会有问题,在 Parallel.foreach 或任何其他多线程场景中调用时,请验证。
C resultC = Helper.Method1(aList, bList);
Run Code Online (Sandbox Code Playgroud)
但是我们怀疑,是否有一种罕见的情况可能是两个线程进行上述调用并且aList,bList的一个线程数据被另一个线程替换,从而给出有缺陷的结果(可能是异常),这可以为问题将无法调试和重复,因为两个线程必须在精确的毫秒内同时执行/执行该方法所需的时间
请分享您的观点,我们是否在正确的轨道上创建上述设计,或者有我们无法看到的坑。我们可以很容易地通过实例方法替换,在这种情况下它们肯定是线程安全的,因为每个线程都有自己的实例可以使用,但是我觉得可能不需要并且继续创建实例很麻烦,当我们可以方便地使用时静态调用。
请注意,到目前为止我还没有看到代码运行出现问题,但正如我所说,如果发生这种情况,这将是极端情况,两个线程同时出现,一个线程替换输入参数,而另一个线程仍在处理结果。
c# multithreading static-methods task-parallel-library parallel.foreach
我有一个测试类,其中包含一些静态方法和普通方法。问题是pytest没有收集静态方法。我在文档中找不到与此相关的任何内容。我怎样才能让它也收集静态方法?
class TestFoo(object):
@staticmethod
def test_bar():
assert 1 == 1
def test_bar2(self):
assert 1 == 1
Run Code Online (Sandbox Code Playgroud)
在上面的类中,只有test_bar2被收集而test_bar()没有被收集。
我在跑步Python 2.7.13, pytest-3.1.2, py-1.4.34, pluggy-0.4.0
插件有xdist-1.17.1, leaks-0.2.2, cov-2.5.1
假设我有一个带有静态方法的类,并且我希望将类属性设置为该方法返回的值:
class A:
@staticmethod
def foo():
return 12
baz = foo()
Run Code Online (Sandbox Code Playgroud)
但是这样做我得到一个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in A
TypeError: 'staticmethod' object is not callable
Run Code Online (Sandbox Code Playgroud)
我找到了解决这个问题的方法:
class A:
class B:
@staticmethod
def foo():
return 2
baz = B.foo()
Run Code Online (Sandbox Code Playgroud)
但例如,如果我写:
class A:
class B:
@staticmethod
def foo():
return 2
class C:
baz = B.foo()
Run Code Online (Sandbox Code Playgroud)
我也收到一个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in …Run Code Online (Sandbox Code Playgroud) 众所周知,可以在不实例化类的情况下调用静态方法。所以我想知道静态方法在我使用它们之前是否会加载到内存中。如果那样的话,在我看来,我应该使用更多的实例方法而不是太多的静态方法。有人建议吗?我不熟悉PHP的底层机制。
我正在阅读 Oracle 网站上 Java 8 Programmer I 考试的示例问题,并遇到以下问题:
abstract class Writer {
public static void write() {
System.out.println("Writing...");
}
}
class Author extends Writer {
public static void write() {
System.out.println("Writing book");
}
}
class Programmer extends Writer {
public static void write() {
System.out.println("Writing code");
}
public static void main(String[] args) {
Writer w = new Author();
w.write();//What would be the ouput here?
}
}
Run Code Online (Sandbox Code Playgroud)
正确答案是调用了抽象类的方法。
现在,我的理解是,在多态中,如果父类类型的变量包含对子类对象的引用,则将调用子类的方法。
因此,我是否理解在静态函数的情况下,将调用其变量包含引用的类的方法?
我一直在阅读一些有关静态函数和静态成员函数的内容。根据我的理解,如果一个函数被声明为静态,那么该函数仅对其翻译单元可见,而对其他地方不可见。相反,静态成员函数是无需实例化其类的任何对象即可调用的函数(因此您可以像在名称空间中一样使用它)。
为了用静态函数来澄清,我的意思是
static int foo(int a, int b)
{
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
对于静态成员函数,我的意思是
struct MyClass
{
static int foo(int a, int b)
{
return a + b;
}
}
Run Code Online (Sandbox Code Playgroud)
这是唯一的区别吗?或者同一翻译单元内的可见性仍然是他们两个的共同特征?
我知道有这样的情况是有线的但不知怎的,我有它:
class foo
#static method
@staticmethod
def test():
pass
# class variable
c = {'name' : <i want to reference test method here.>}
Run Code Online (Sandbox Code Playgroud)
它的方法是什么?
仅供记录:
我认为这应该被视为python最差的做法.如果有的话,使用静态方法并不是真正的pythoish方式......
static-methods ×10
c# ×4
python ×3
static ×3
.net ×1
c++ ×1
class ×1
class-method ×1
cookies ×1
inheritance ×1
java ×1
php ×1
polymorphism ×1
pytest ×1
resharper ×1
subclass ×1