static-methods

Generics in static methods

大憨熊 提交于 2019-12-04 05:01:40
问题 I need to add a method in a utility class with some static methods that parses things from a JSON string and returns an array of things. Problem is that there are various subtypes of these things, so I created this method: public static <E extends Thing> E[] parseThingsFromJSON(String body) { return parser.fromJson(body, E[].class); } How does the caller tell this method what E is? Or is there a better way to do this? 回答1: You need to pass it. public static <E extends Thing> E[]

Using static methods in Android?

纵饮孤独 提交于 2019-12-04 04:05:21
问题 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

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

女生的网名这么多〃 提交于 2019-12-04 03:38:03
问题 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. 回答1: 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,

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

匆匆过客 提交于 2019-12-04 03:24:31
问题 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

Calling a Static method in C#

北慕城南 提交于 2019-12-04 02:59:22
How do I call a static method? I want to call this from a class I have created, I want to get the location from IP. I've declared it but what I need to do is call the method... as static ... To be honest with you, I'm quite confused here, do I need to instantiate address , city , etc.? I have done this so far; LocationTools.cs public static class LocationTools { public static void GetLocationFromIP(string address, out string city, out string region, out string country, out double? latitude, out double? longitude) { Home.cs public string IPAPIKey { get { return WebConfigurationManager

Access static variable from static method

醉酒当歌 提交于 2019-12-04 02:48:43
问题 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 回答1: Use @classmethod instead of @staticmethod . Found it just after

Memory usage when converting methods to static methods

一笑奈何 提交于 2019-12-04 02:07:37
I started using Resharper and it indicated when a method could be made static. Would converting a few hundred methods to static methods increase the memory footprint over a large period of time? No - Changing to static methods has no effect on memory. The first time a type is referenced (whether static or non-statically), any static members are initialized and static constructors are run. However, if you're just considering switching methods from non-static to static, this will have no effect on garbage collection or total memory footprint. You only have to worry about memory footprint

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

随声附和 提交于 2019-12-04 02:01:57
问题 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? ) 回答1: Calling non

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

旧巷老猫 提交于 2019-12-04 01:38:50
问题 This question already has answers here : Objective C & OC Mock - Mocking a class method? (4 answers) Closed 6 years ago . 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 (

Should I never use static methods and classes and singletons when following the Test Driven Development paradigm

烈酒焚心 提交于 2019-12-04 00:29:55
I've been reading that static methods, static classes, and singletons are evil when you try to implement unit testing in your project. When following the TDD paradigm, should I just forget that they ever existed and never use them again or is it ok to use them sometimes? Phil Sandler Never say never--static classes and methods have their place in your toolbox. That said, if the class you are trying to isolate and test (subject under test or SUT) depends on a static class or method, you will be unable to write a test that isolates the SUT from that static dependency--when your test code runs it