static-methods

Using static methods in Android?

冷暖自知 提交于 2019-12-01 19:57:23
I already made some Apps in Android and noticed that I am often using static methods. For example I have an Class which extends PreferenceFragment . In this PreferenceFragment I set an onClick event on a Button . Button btn = new Button(getActivity().getApplicationContext()); btn.setText("Save"); v.addView(btn); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { SettingsActivity.finishActivityWithResultOkey(); } }); Then I'm calling a static method in my SettingsActivity which finishes this Activity . Is this a good way of doing what I want to do? Or is there a

Can a method be used as either a staticmethod or instance method?

柔情痞子 提交于 2019-12-01 18:03:40
I'd like to be able to do this: class A(object): @staticandinstancemethod def B(self=None, x, y): print self is None and "static" or "instance" A.B(1,2) A().B(1,2) This seems like a problem that should have a simple solution, but I can't think of or find one. It is possible, but please don't. I couldn't help but implement it though: class staticandinstancemethod(object): def __init__(self, f): self.f = f def __get__(self, obj, klass=None): def newfunc(*args, **kw): return self.f(obj, *args, **kw) return newfunc ...and its use: >>> class A(object): ... @staticandinstancemethod ... def B(self, x

How can I use a static method as a default parameter for the strategy design pattern?

白昼怎懂夜的黑 提交于 2019-12-01 17:34:56
I want to make a class that uses a strategy design pattern similar to this: class C: @staticmethod def default_concrete_strategy(): print("default") @staticmethod def other_concrete_strategy(): print("other") def __init__(self, strategy=C.default_concrete_strategy): self.strategy = strategy def execute(self): self.strategy() This gives the error: NameError: name 'C' is not defined Replacing strategy=C.default_concrete_strategy with strategy=default_concrete_strategy will work but, left as default, the strategy instance variable will be a static method object rather than a callable method.

Does a static method share its local variables & what happens during concurrent usage from different threads?

筅森魡賤 提交于 2019-12-01 15:26:52
C# Question - I'm trying to determine whether it is OK to use a static method where, within the method it does have some local variables it uses. Are the local variables "shared" across usages of the method? What happens for example if the static method is being called/used at the same time from different threads? Does one thread block until the other is complete etc? Perhaps the generalised question is, in a threaded application, when should one "not" being using a static method? Local variables within the method live on the stack and each thread has its own stack. Therefore it's safe for

Access static variable from static method

♀尐吖头ヾ 提交于 2019-12-01 15:02:21
I want to access a static variable from a static method: #!/usr/bin/env python class Messenger: name = "world" @staticmethod def get_msg(grrrr): return "hello " + grrrr.name print Messenger.get_msg(Messenger) How to do it without passing grrrr to a method? Is this the true OOP?.. Anything like name or self.name seems not working: NameError: global name 'name' is not defined and NameError: global name 'self' is not defined Dmitry Isaev Use @classmethod instead of @staticmethod . Found it just after writing the question. In many languages (C++, Java etc.) "static" and "class" methods are

PHP: call to an instance method via ClassName::method syntax, results in a static call?

左心房为你撑大大i 提交于 2019-12-01 12:07:32
Her is my code: class MyClass { public $prop; public function method () { echo $this->prop; } } Then somewhere in the code, accidently: MyClass::method(); I would expect to have an interpretation error about the above line, because the called method is not static. Instead, the method was called, and I received an exception about $prop not existing. So i understand that the method was called as a static method, even though it's not. Does it work this way? (Why the hell? ) Calling non-static methods statically generates an E_STRICT level warning. http://php.net/manual/en/language.oop5.static.php

How to hide static method

耗尽温柔 提交于 2019-12-01 11:05:21
Let's say I have a classes, like that: class A { public static int Count() } class B : A { } class C : A { } How can I hide this static method for class B but not for C? You can't, basically. Heck, if it's public then anyone can call it. You could make it protected which would allow it to be called from within B or C but not elsewhere... but you still couldn't differentiate between B and C. You could do it by creating another class, let's call it Special, that inherits A. Then you would make C inherit from Special and B inherit from A. Also, you would have the static method protected, that

Pickling a staticmethod in Python

断了今生、忘了曾经 提交于 2019-12-01 10:55:23
I've been trying to pickle an object which contains references to static class methods. Pickle fails (for example on module.MyClass.foo ) stating it cannot be pickled, as module.foo does not exist. I have come up with the following solution, using a wrapper object to locate the function upon invocation, saving the container class and function name: class PicklableStaticMethod(object): """Picklable version of a static method. Typical usage: class MyClass: @staticmethod def doit(): print "done" # This cannot be pickled: non_picklable = MyClass.doit # This can be pickled: picklable =

How to hide static method

假如想象 提交于 2019-12-01 09:18:20
问题 Let's say I have a classes, like that: class A { public static int Count() } class B : A { } class C : A { } How can I hide this static method for class B but not for C? 回答1: You can't, basically. Heck, if it's public then anyone can call it. You could make it protected which would allow it to be called from within B or C but not elsewhere... but you still couldn't differentiate between B and C. 回答2: You could do it by creating another class, let's call it Special, that inherits A. Then you

How to mock class method (+)? [duplicate]

孤街浪徒 提交于 2019-12-01 06:48:04
This question already has an answer here: Objective C & OC Mock - Mocking a class method? 4 answers Need to write unit testing for the following code, I want to do mock for class method canMakePayments, return yes or no, so far no good method found dues to canMakePayments is a class method (+), seems all OCMock methods are all used for instance method (-). You guys any suggestion or discussion will be appreciated. Thanks. // SKPaymentQueue.h // StoreKit if ([SKPaymentQueue canMakePayments]){ .... } else{ ... } Since you can't intercept the method by providing a different instance, what you can