static-methods

How the static class's static method will know the caller?

隐身守侯 提交于 2019-12-10 18:34:49
问题 Imagine I have a static class and a static method inside that. And it has to be accessed by 10 different classes. But how the static class will know who has called it :( It was an interview question....please rephrase it properly and answer me, I am new :( 回答1: I would try the following: public class ParentClass { } public class ChildClass : ParentClass { } public static class StaticClass { public static void SomeMethod(ParentClass d) { var t = d.GetType(); } } public class StaticChildren {

Get instance of subclass with superclass static method

穿精又带淫゛_ 提交于 2019-12-10 17:45:25
问题 I have a superclass that I would like to forward a static method called getInstance() to all subclasses. When creating an instance of a subclass, I then register the instance in the superclass (perhaps using a hashtable, where the key is based on getClass() ). Then, I wish to use the aforementioned static method ( getInstance ) where the superclass method will return the instance of the correct type. For example, I have a superclass A, and a subclass B extends A. I want to write a static

Can't Access Super Globals Inside __callStatic?

我的梦境 提交于 2019-12-10 16:18:31
问题 The following code fails on my install of PHP 5.3.6-13ubuntu3.2 which makes me wonder why I can't access the $_SERVER Super Global inside this method. <?php header('Content-Type: text/plain'); $method = '_SERVER'; var_dump($$method); // Works fine class i { public static function __callStatic($method, $args) { $method = '_SERVER'; var_dump($$method); // Notice: Undefined variable: _SERVER } } i::method(); Anyone know what is wrong here? 回答1: As indicated in the manual: Note: Variable

C++ linker problems with static method

南楼画角 提交于 2019-12-10 15:47:14
问题 I'm writing a Vector3D class that calls a static method on a VectorMath class to perform a calculation. When I compile, I get this: bash-3.1$ g++ VectorMath.cpp Vector3D.cpp /tmp/cc5cAPia.o: In function `main': Vector3D.cpp:(.text+0x4f7): undefined reference to 'VectorMath::norm(Vector3D*)' collect2: ld returned 1 exit status The code: VectorMath.h: #ifndef VECTOR3D_H #include "Vector3D.h" #endif class VectorMath { public: static Vector3D* calculatePerpendicularVector(Vector3D*, Vector3D*);

Avoiding static methods and variables in a Java Class file that handles config.properties

自作多情 提交于 2019-12-10 15:37:47
问题 I am working on a simple Java Application and I've created a class called Config.java in order to handle the Application properties, thus, avoiding hard-coding. The Config.java class is not a static class, and I am creating an instance of the Config.java class in another class called Serial.java. The main method is located in another Class called App.java. So I have 3 Classes in total: App.java Serial.java (Instance of Config class is located here as a private variable) Config.java At this

Are static methods in ASP.NET code-behind classes non-thread-safe?

左心房为你撑大大i 提交于 2019-12-10 13:32:22
问题 Can I use static methods in my ASP.NET Pages and UserControls classes if they don't use any instance members? I.e.: protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e) { gridStatement.DataSource = CreateDataSource(); gridStatement.PageIndex = e.NewPageIndex; gridStatement.DataBind(); } private static DataTable CreateDataSource() { using (var command = new SqlCommand("SELECT foobar")) { var table = new DataTable(); new SqlDataAdapter(command).Fill(table); return

Static and instance methods in Python [duplicate]

岁酱吖の 提交于 2019-12-10 12:40:27
问题 This question already has answers here : Can a method be used as either a staticmethod or instance method? (4 answers) Closed 6 years ago . Can I define a Python method to be both static and instance at the same time? Something like: class C(object): @staticmethod def a(self, arg1): if self: blah blah So that I can call it with both: C.a(arg1) C().a(arg1) The intent is to be able to run two sets of logics. If accessed as an instance method, it would make use of instance variables and do stuff

Why are static methods untestable?

╄→гoц情女王★ 提交于 2019-12-10 12:32:40
问题 Why are static methods untestable? Kindly exemplify (in PHP if possible). 回答1: Static methods themselves aren't untestable, but if the object being tested calls a static method then a test cannot "get in between" and make it call a stub method instead. If the object being tested instead calls a regular method, the test can give it an alternative object with a stub implementation of that method. In general, rigid dependencies are less testable while dependency injection (google it) makes code

why this weird order of constructor/static initializer/static member function in java?

吃可爱长大的小学妹 提交于 2019-12-10 10:43:22
问题 public class DataFactory { private static DataFactory ourInstance = new DataFactory(); static { System.out.println("static initialize"); } private DataFactory() { System.out.println("constructor"); } public static void doNothing() { System.out.println("inside doNothing"); } } public class App { public static void main(String[] args) { System.out.println("main start"); DataFactory.doNothing(); } And After I run it, here is the printed sequence: main start constructor static initialize inside

error :should not be called statically, assuming $this from incompatible context. only on my machine

谁都会走 提交于 2019-12-10 09:22:12
问题 My team members wrote the model function calls in the controller statically such as: $data = ModelName::functionName($param); while it should be called dynamically such as: $model = new Model(); $data = $model->functionName($param); mostly all the calls are made statically. the code is working on the server and on their local machines except for mine. And the static calls are too many to fix without rewriting huge code base. I always update my project via composer. My php version is 5.4.