static-methods

PHP methods that work in both instantiated and static contexts?

此生再无相见时 提交于 2019-12-11 14:37:45
问题 I'm trying to setup some PHP methods that are callable in instantiated and static contexts. What are some good ways to do this? For example I want to be able to do: Foo::bar($item); foo($item)->bar(); I could setup two separate classes and have each function modify the thisArg and delegate to the other, but it seems like there's got to be a better way. The only way I could think of to do it with only one class would be something like this: function foo($item = null) { return $item instanceof

A class that only has static data and methods to access these data. How to implement that properly?

你离开我真会死。 提交于 2019-12-11 12:17:59
问题 I know that is a beginner's question. I'm new to java and and also to programming in general. Say I got a class that has only static data, example: class Foo { private static int x; } I want to use the class without instantiating any object. So I want to be able to do: Foo.setX(5); Foo.getX(); What is the best way to implement this class, I'm a little bit confused about interfaces and other stuff for now. Thanks. 回答1: Why don't you just define two static methods that modify/return the static

MonoTouch - how to override static UIView.layerClass

随声附和 提交于 2019-12-11 12:12:59
问题 I'm trying to port AUISelectiveBordersView to MonoTouch. It's basically a subclass of CALayer and integration in UIView via Categories. "Translation" of AUISelectiveBordersLayer is easy, but integration point is a bit tricky. In obj-c it's done like: @implementation UIView (AUISelectiveBorder) +(Class) layerClass { return [AUISelectiveBordersLayer class]; } Is there any way to translate this to MonoTouch? It looks like overriding a static method, but I don't see anything like layerClass or

Circular references in iPhone Cocos2D game programming textbook

◇◆丶佛笑我妖孽 提交于 2019-12-11 11:05:09
问题 an abused topic but I couldn't find an answer to this. I am following the "Learn iPhone and iPad cocos2d Game Development" book and cannot really understand if the approach in the ShootEmUp example (available at 1) is the best one. The author uses a GameScene to which adds as child various objects (e.g. Ship, InputLayer etc..). The controversial aspect is that within those object there are calls to the GameScene via using a static method that returns a static instance of the GameScene class

No enclosing instance of type ContactsXmpp is accessible?

走远了吗. 提交于 2019-12-11 10:16:58
问题 this is the class i am using public class ContactsXmpp extends SherlockFragmentActivity { private static Context ctx; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contacts_xmpp_sip); ctx = this; } i am getting error when i call asynctask from this method. this is the error No enclosing instance of type ContactsXmpp is accessible. Must qualify the allocation with an enclosing instance of type ContactsXmpp (e.g. x.new A(

Static ArrayList accessed from another class is always empty

女生的网名这么多〃 提交于 2019-12-11 09:49:20
问题 I have the following classes import java.util.ArrayList; import java.util.List; public class TestStaticArrayList { public static List<String> numberList = new ArrayList<String>(); public static List<String> getArrayValues(){ return numberList; } public static void populateArray() { numberList.add("1"); numberList.add("2"); numberList.add("3"); } } The static ArrayList in the above class is populated dynamically from a call from below class. import java.lang.reflect.Field; import java.util

Performance and static method versus public method

北城以北 提交于 2019-12-11 09:28:31
问题 I have a helper method that takes a begin date and an end date and through certain business logic yields an integer result. This helper method is sometimes called in excess of 10,000 times for a given set of data (though this doesn't occur often). Question: Considering performance only, is it more efficient to make this helper method as a static method to some helper class, or would it be more gainful to have the helper method as a public method to a class? Static method example: // an

Can only return, not assign, Self?

大城市里の小女人 提交于 2019-12-11 09:22:12
问题 Consider this pattern extension UIViewController { class func make(sb: String, id: String) -> Self { return helper(sb:sb, id:id) } private class func helper<T>(sb: String,id: String) -> T { let s = UIStoryboard(name: storyboardName, bundle: nil) let c = s.instantiateViewControllerWithIdentifier(id) as! T return c } } that works fine, so let s = SomeViewControllerClass.make( ... ) does in fact return the subclass "SomeViewControllerClass". (Not just a UIViewController.) That's all fine BUT,

Why am I seeing this weird output in Java threads in Socket connections?

蓝咒 提交于 2019-12-11 07:57:44
问题 So I have this little code: public class MyCounterClass { private static int counter = 0; synchronized public static int getCounter(){ return counter++; } } This is the server: public class MyServer implements Runnable{ public static void go() throws IOException { System.out.println("MyServer: Go called..."); ServerSocket serverSocket = new ServerSocket(5000); while(true){ Socket socket = serverSocket.accept(); System.out.println(time() + "MyServer: Connection accepted!"); OutputStream

C++ private static member variables

馋奶兔 提交于 2019-12-11 05:29:42
问题 This C++ code is producing linker errors at compile time: // A.h class A { public: static void f(); private: static std::vector<int> v; }; // A.cpp void A::f() { // this line is causing trouble int i = v.size(); } Moving the vector declaration into the cpp file works. However I want to understand the linker error "Undefined symbols" cause in the above code. What is causing the linker error in the above code? 回答1: Static members have to be defined in a compilation unit: // A.cpp vector<int> A: