static-methods

cannot make a static reference to a non static method

我与影子孤独终老i 提交于 2019-12-17 14:26:11
问题 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; }

How can I dynamically create class methods for a class in python [duplicate]

孤人 提交于 2019-12-17 10:15:38
问题 This question already has answers here : Adding a Method to an Existing Object Instance (16 answers) Closed last year . If I define a little python program as class a(): def _func(self): return "asdf" # Not sure what to resplace __init__ with so that a.func will return asdf def __init__(self, *args, **kwargs): setattr(self, 'func', classmethod(self._func)) if __name__ == "__main__": a.func I receive the traceback error Traceback (most recent call last): File "setattr_static.py", line 9, in

Getting the name of a child class in the parent class (static context)

帅比萌擦擦* 提交于 2019-12-17 08:17:33
问题 I'm building an ORM library with reuse and simplicity in mind; everything goes fine except that I got stuck by a stupid inheritance limitation. Please consider the code below: class BaseModel { /* * Return an instance of a Model from the database. */ static public function get (/* varargs */) { // 1. Notice we want an instance of User $class = get_class(parent); // value: bool(false) $class = get_class(self); // value: bool(false) $class = get_class(); // value: string(9) "BaseModel" $class =

Static properties in Swift

自古美人都是妖i 提交于 2019-12-17 06:27:43
问题 I'm trying to convert the following Objective-C code to Swift. In my Objective-C code, there's a static variable and its accessed from a class method. @implementation SomeClass static NSMutableArray *_items; + (void)someMethod { [_items removeAll]; } @end Since you can't access types declared like this private var items = [AnyObject]() from class functions in Swift, I created a stored property for it like this. class var items: [AnyObject] { return [AnyObject]() } And I'm trying to call a

Is using a lot of static methods a bad thing?

强颜欢笑 提交于 2019-12-17 04:43:07
问题 I tend to declare as static all the methods in a class when that class doesn't require to keep track of internal states. For example, if I need to transform A into B and don't rely on some internal state C that may vary, I create a static transform. If there is an internal state C that I want to be able to adjust, then I add a constructor to set C and don't use a static transform. I read various recommendations (including on StackOverflow) NOT to overuse static methods but I still fail to

C# Static variables - scope and persistence

风流意气都作罢 提交于 2019-12-17 04:29:22
问题 I just did a little experiment: public abstract class MyClass { private static int myInt = 0; public static int Foo() { return myInt; } public static int Foo(int n) { myInt = n; return bar(); } private static int bar() { return myInt; } } and then I ran: MessageBox.Show(MyClass.Foo().ToString()); MessageBox.Show(MyClass.Foo(3).ToString()); MessageBox.Show(MyClass.Foo().ToString()); MessageBox.Show(MyClass.Foo(10).ToString()); MessageBox.Show(MyClass.Foo().ToString()); The results I expected

Class with single method — best approach?

拜拜、爱过 提交于 2019-12-17 03:44:35
问题 Say I have a class that's meant to perform a single function. After performing the function, it can be destroyed. Is there any reason to prefer one of these approaches? // Initialize arguments in constructor MyClass myObject = new MyClass(arg1, arg2, arg3); myObject.myMethod(); // Pass arguments to method MyClass myObject = new MyClass(); myObject.myMethod(arg1, arg2, arg3); // Pass arguments to static method MyClass.myMethod(arg1, arg2, arg3); I was being intentionally vague about the

Should private helper methods be static if they can be static

自作多情 提交于 2019-12-17 02:33:20
问题 Let's say I have a class designed to be instantiated. I have several private "helper" methods inside the class that do not require access to any of the class members, and operate solely on their arguments, returning a result. public class Example { private Something member; public double compute() { double total = 0; total += computeOne(member); total += computeMore(member); return total; } private double computeOne(Something arg) { ... } private double computeMore(Something arg) {... } } Is

Can the staticmethod and classmethod decoraters be implemented in pure Python?

对着背影说爱祢 提交于 2019-12-14 03:43:31
问题 This is an academic question, more than a practical one. I'm trying to delve into the foundations of Python, and I wonder: could I implement the staticmethod and classmethod decorators in pure Python if I wanted to? For staticmethod maybe I could keep a reference to the original function and call it omitting the first argument? For classmethod maybe I could keep a reference to the original function and call it passing the type of the first argument, rather than the argument itself? (How would

C++ undefined reference (static member) [duplicate]

时间秒杀一切 提交于 2019-12-14 02:43:24
问题 This question already has answers here : static variable link error (2 answers) Closed 4 years ago . Possible Duplicate: C++: undefined reference to static class member Logger.h: class Logger { private: Logger(); static void log(const string& tag, const string& msg, int level); static Mutex mutex; public: static void fatal(const string&, const string&); static void error(const string&, const string&); static void warn(const string&, const string&); static void debug(const string&, const