static-methods

Access to static properties via this.constructor in typescript

ぃ、小莉子 提交于 2019-12-03 22:31:48
I want to write es6 class: class SomeClass { static prop = 123 method() { } } How to get access to static prop from method() without use SomeClass explicitly? In es6 it can be done with this.constructor , but in typescript this.constructor.prop causes error " TS2339: Property 'prop' does not exist on type 'Function' ". but in typescript this.constructor.prop causes error "TS2339: Property 'prop' does not exist on type 'Function'". Typescript does not infer the type of constructor to be anything beyond Function (after all ... the constructor might be a sub class). So use an assertion: class

Call static method from instance in PHP, future deprecation?

情到浓时终转凉″ 提交于 2019-12-03 22:21:45
While I understand the $this variable is not available when a method is called in a static context, to assist in decoupling my application components from one-another I figured it would make sense to call static methods from an instance. For example: class MyExample{ private static $_data = array(); public static function setData($key, $value){ self::$_data[$key] = $value; } // other non-static methods, using self::$_data } // to decouple, another class or something has been passed an instance of MyExample // rather than calling MyExample::setData() explicitly // however, this data is now

.Net Static Methods and it's effects on Concurrency?

梦想与她 提交于 2019-12-03 22:09:04
I am currently building an API which will be used by a webservice. I was wondering what performance issues I could meet if I built my API using a large amount of static methods . The original idea was to build expert objects which act as services. In a single user environment this approach was great! But I will soon need to port this to a multi/concurrent user environment. What kind of performance issues might i encounter with this kind of architecture? Best regards, Edit: The static methods hold no static variables and have no side effects. They simply execute a normal routine where

Static function declared but not defined in C++

流过昼夜 提交于 2019-12-03 18:40:41
问题 I'm getting an error from the following code using C++. Main.cpp #include "file.h" int main() { int k = GetInteger(); return 0; } File.h static int GetInteger(); File.cpp #include "file.h" static int GetInteger() { return 1; } The error I get: Error C2129: static function 'int GetInteger(void)' declared but not defined. I've read the famous article "Organizing Code File in C and C++", but don't understand what is wrong with this code. 回答1: In C++, static at global/namespace scope means the

Python static method is not always callable

…衆ロ難τιáo~ 提交于 2019-12-03 17:44:28
问题 While parsing attributes using __dict__, my @staticmethod is not callable. Python 2.7.5 (default, Aug 29 2016, 10:12:21) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from __future__ import (absolute_import, division, print_function) >>> class C(object): ... @staticmethod ... def foo(): ... for name, val in C.__dict__.items(): ... if name[:2] != '__': ... print(name, callable(val), type(val)) ... >>> C.foo() foo

How to call a static method from a private base class?

半世苍凉 提交于 2019-12-03 14:44:45
问题 Due to the layout of a third-party library, I have something like the following code: struct Base { static void SomeStaticMethod(){} }; struct Derived1: private Base {}; struct Derived2: public Derived1 { void SomeInstanceMethod(){ Base::SomeStaticMethod(); } }; int main() { Derived2 d2; d2.SomeInstanceMethod(); return 0; } I'm getting compiler error C2247 with MSVC: Base::SomeStaticMethod not accessible because Derived1 uses private to inherit from Base. I know I can't access Base members

What is the difference between all-static-methods and applying a singleton pattern?

徘徊边缘 提交于 2019-12-03 13:29:39
I am making a database to store information about the users of my website (I am using stuts2 and hence Java EE technology). For the database I'll be making a DBManager. Should I apply singleton pattern here or rather make all it's methods static? I will be using this DBManager for basic things like adding, deleting and updating User profiles. Along with it, I'll use for all other querying purposes, for instance to find out whether a username already exists and to get all users for administrative purposes and stuff like that. My questions What is the benefit of singleton pattern? Which thing is

Performance of Singleton Class Instance Method vs. Static Class Method in PHP?

笑着哭i 提交于 2019-12-03 13:25:24
问题 I'm interested in objective analysis of which is more performant; calling instance methods of a singleton class or methods of a static class. I've already seen this so I'm not looking for a discussion about the difference between the two or a discussion of which is "better." I'm only interested in relative performance between the two. Thanks in advance. -Mike 回答1: Check this chart :) grabbed from this article 回答2: Unless you're calling them in a tight loop (meaning no other significant code,

Static block vs static method - initializing static fields

[亡魂溺海] 提交于 2019-12-03 12:57:44
问题 Out of curiosity, I measured the performance between static block and static method initializer. First, I implemented the above mentioned methods in two separate java classes, like so: First: class Dummy { static java.util.List<Integer> lista = new java.util.ArrayList<Integer>(); static { for(int i=0; i < 1000000; ++i) { lista.add(new Integer(i)); } } } public class First { public static void main(String[] args) { long st = System.currentTimeMillis(); Dummy d = new Dummy(); long end = System

What is the right way to use an injected bean in a static method?

白昼怎懂夜的黑 提交于 2019-12-03 12:45:34
问题 This question might seem a little odd. Suppose I have a Service which I want to use in a Utility class that has some static methods. The Service is a Spring bean, so naturally I will for example use a setter and (@Autowired) to inject it into my utility class. As it is mentioned in Spring's documentation, all beans are static in the bean context. So when you want to inject a bean in a class you don't have to use "static" modifier. See below: public class JustAClass{ private Service service;