static-methods

python class design (staticmethod vs method)

谁都会走 提交于 2019-12-19 06:18:05
问题 What's the nicer way for methods that don't need any passed information (object instance or class) because for example they just do a simple conversion. @staticmethod or method ? class Foo(object): def __init__(self, trees): self.money = Foo.trees2money(trees) @staticmethod def trees2money(trees): return trees * 1.337 class Quu(object): def __init__(self, trees): self.money = self.trees2money(trees) def trees2money(self, trees): return trees * 1.337 回答1: The choice of the type of method

python class design (staticmethod vs method)

。_饼干妹妹 提交于 2019-12-19 06:17:22
问题 What's the nicer way for methods that don't need any passed information (object instance or class) because for example they just do a simple conversion. @staticmethod or method ? class Foo(object): def __init__(self, trees): self.money = Foo.trees2money(trees) @staticmethod def trees2money(trees): return trees * 1.337 class Quu(object): def __init__(self, trees): self.money = self.trees2money(trees) def trees2money(self, trees): return trees * 1.337 回答1: The choice of the type of method

why doesn't this code throw NullPointerException

本秂侑毒 提交于 2019-12-19 05:19:36
问题 I was just discussing about calling static methods using class name with my friend and tried out this code and expected it to throw NPE at runtime.but as it turn out it dint. i just want to understand the execution order. public class One { public static void method() { System.out.println("in static one"); } } public class Two { static One o; public static void main(String[] args) { o.method(); // expected NPE here, as o is null } } I know that static methods should be invoked with their

Python: check if method is static

落爺英雄遲暮 提交于 2019-12-19 05:17:14
问题 assume following class definition: class A: def f(self): return 'this is f' @staticmethod def g(): return 'this is g' a = A() So f is a normal method and g is a static method. Now, how can I check if the funcion objects a.f and a.g are static or not? Is there a "isstatic" funcion in Python? I have to know this because I have lists containing many different function (method) objects, and to call them I have to know if they are expecting "self" as a parameter or not. 回答1: I happens to have a

How do I call a static method of another class

空扰寡人 提交于 2019-12-18 12:45:19
问题 I have a class, lets say CAppPath which has a static method: public: static CString GetAppPath(); and in CAppPath.cpp it's defined as: CString CAppPath::GetAppPath() { return "C:\..\MypAth"; } Now I have another class CXMLHandler , and I have included CAppPath.h in it. But how do I call the GetAppPath() method? I've tried: #include "CAppPath.h" void CXMLHandler::MyMethod { CNDSClientDlg->GetAppPath(); } but it doesn't work. How should I access this method? Since it is a static method, do I

Why overloaded operators cannot be defined as static members of a class?

廉价感情. 提交于 2019-12-18 12:45:16
问题 C++ syntax allows defining overloaded operators either inside the struct/class like: struct X { void operator+(X); } or outside of the struct/class like: void operator+(X, X); but not as struct X { static void operator+(X, X); } Does any body know reasons for this decision? Why the third form is not allowed? (MSVC gives a syntax error). Maybe there is some story behind this? p.s. Presence of the first and the second definitions at the same time creates ambiguity: 1>CppTest1.cxx 1>c:\ballerup

How can I run a static initializer method in C# before the Main() method?

Deadly 提交于 2019-12-18 07:30:21
问题 Given a static class with an initializer method: public static class Foo { // Class members... internal static init() { // Do some initialization... } } How can I ensure the initializer is run before Main() ? The best I can think of is to add this to Foo : private class Initializer { private static bool isDone = false; public Initializer() { if (!isDone) { init(); isDone = true; } } } private static readonly Initializer initializer = new Initializer(); Will this work or are there some

Using $this inside a static function fails

帅比萌擦擦* 提交于 2019-12-17 17:38:52
问题 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(); } 回答1: 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 :) 回答2: You can't use $this inside a static function, because

Static Method Memory Allocation

社会主义新天地 提交于 2019-12-17 16:26:24
问题 We have two classifications heap and stack . When a object is created, memory for object is stored in heap. What if the class has static methods ,which can be called using class name. If object is not created then how will it allocate memory and if it does where will it allocate memory? 回答1: It depends on the JVM, but static fields are usually stored in a special object on the heap. (You can see it in a heap dump) When the ClassLoader is unloaded, its classes and their static "objects"/fields

cannot make a static reference to a non static method

送分小仙女□ 提交于 2019-12-17 14:26:13
问题 So far I have the following code: import java.util.Scanner; public class HallLanceMemoryCalculator { private double currentValue; public static int displayMenu(){ Scanner input=new Scanner(System.in); int choice=0; while(choice<1||choice>5){ System.out.println("1.Add"); System.out.println("2.Subtract"); System.out.println("3.Multiply"); System.out.println("4.Divide"); System.out.println("5.Clear"); System.out.println("What would you like to do?"); choice=input.nextInt(); } return choice; }