static-methods

Calling static method non-statically

北城以北 提交于 2019-11-28 07:55:15
问题 I have a child class that extends a class with only static methods. I would like to make this child class a singleton rather than static because the original developer really wanted a singleton but used static instead (obvious because every method in the static class calls the Init() function (basically a constructor)). Most of the methods in the parent don't need to be overwritten in the child, but I would like to avoid having to write methods like this: public function Load($id) { return

How to call method in main activity from other activity?

北城余情 提交于 2019-11-28 06:46:29
问题 I want to call public method in main activity from other activity. How can I do that? class MainActivity extends Activity { public void myMethod() {} } class MyActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // How can I call myMethod() in MainActivity? } } 回答1: It depends. In the case if you want just to use some shared functionality (as example the code which does some calculation). I would recommend to move this shared

Struts 2 refactoring code to avoid OGNL static method access

末鹿安然 提交于 2019-11-28 05:34:59
问题 Struts 2, 2.3.20 mentioned that Support for accessing static methods from expression will be disabled soon, please consider re-factoring your application to avoid further problems! We have used OGNL static calls in validators: @ExpressionValidator( expression = "@foo.bar@isValidAmount(amount)", key = "validate.amount.is.not.valid"), Also we used it in tags <s:set var="test" value="@foo.bar@sampleMethod(#attr.sampleObject.property1)" /> Well, what is the best way to refactor above two usages ?

ES6: this within static method

╄→尐↘猪︶ㄣ 提交于 2019-11-28 05:13:47
问题 Let's say I have two ES6 classes like this: class Base { static something() { console.log(this); } } class Derived extends Base { } And then I make a call like this: Derived.something(); Note that I am making a call to a static method defined on the super class via sub class. This does not give me errors. It prints [Function: Derived] So accessing this within a static method seems to work here. I need a common static method for all sub-classes of a super class and I need to be able to know

Using private static methods [duplicate]

落爺英雄遲暮 提交于 2019-11-28 04:40:21
This question already has an answer here: Should private helper methods be static if they can be static 21 answers What do you think about using private static methods ? Personally, I prefer using a static private method to non-static as long as it does not require access to any instance fields. But I heard that this practice violates OOP principles. Edit: I am wondering from style prospective of view, not performance. A private static method by itself does not violate OOP per se, but when you have a lot of these methods on a class that don't need (and cannot*) access instance fields, you are

Python: Make class iterable

倾然丶 夕夏残阳落幕 提交于 2019-11-28 04:40:19
I have inherited a project with many large classes constituent of nothing but class objects (integers, strings, etc). I'd like to be able to check if an attribute is present without needed to define a list of attributes manually. Is it possible to make a python class iterable itself using the standard syntax? That is, I'd like to be able to iterate over all of a class's attributes using for attr in Foo: (or even if attr in Foo ) without needing to create an instance of the class first. I think I can do this by defining __iter__ , but so far I haven't quite managed what I'm looking for. I've

Using this inside a static function fails

我们两清 提交于 2019-11-28 04:37:10
I have this method that I want to use $this in but all I get is: Fatal error: Using $this when not in object context. How can I get this to work? public static function userNameAvailibility() { $result = $this->getsomthin(); } This is the correct way public static function userNameAvailibility() { $result = self::getsomthin(); } Use self:: instead of $this-> for static methods . See: PHP Static Methods Tutorial for more info :) You can't use $this inside a static function, because static functions are independent of any instantiated object. Try making the function not static. Edit : By

Pointers to static methods in Python

痞子三分冷 提交于 2019-11-28 04:25:46
问题 Why is it that in the following code, using a class variable as a method pointer results in unbound method error, while using an ordinary variable works fine: class Cmd: cmd = None @staticmethod def cmdOne(): print 'cmd one' @staticmethod def cmdTwo(): print 'cmd two' def main(): cmd = Cmd.cmdOne cmd() # works fine Cmd.cmd = Cmd.cmdOne Cmd.cmd() # unbound error !! if __name__=="__main__": main() The full error: TypeError: unbound method cmdOne() must be called with Cmd instance as first

How do I know if this C# method is thread safe?

心不动则不痛 提交于 2019-11-28 03:23:09
I'm working on creating a call back function for an ASP.NET cache item removal event. The documentation says I should call a method on an object or calls I know will exist (will be in scope), such as a static method, but it said I need to ensure the static is thread safe. Part 1: What are some examples of things I could do to make it un-thread safe? Part 2: Does this mean that if I have static int addOne(int someNumber){ int foo = someNumber; return foo +1; } and I call Class.addOne(5); and Class.addOne(6); simutaneously, Might I get 6 or 7 returned depending on who which invocation sets foo

PHP static method call with variable class name and namespaces

不羁岁月 提交于 2019-11-28 03:18:40
问题 I'm trying to call a static method for a namespaced class from another class with the same namespace. But the other class' name is contained in a variable : <?php namespace MyApp\Api; use \Eloquent; class Product extends Eloquent { public static function find($id) { //.... } public static function details($id) { $product = self::find($id); if($product) { $type = $product->type; // 'Book' $product = $type::find($product->id); return $product; } } } Here is the Book class : <?php namespace