static-methods

How can i modify variables in static member function?

≡放荡痞女 提交于 2019-12-02 10:13:29
I have a code below, i want to modify class's variables in static function but there is some error. How can i fix it with "this" pointer? There is no access to "this" pointer for static members in class,on the other hand I am trying to make an access to class variables in Static member function, therefore i am looking for a way to use "this" pointer of class "me" to do it. class me { public: void X() { x = 1;} void Y() { y = 2;} static void Z() { x = 5 ; y = 10; } public: int x, y; }; int main() { me M; M.X(); M.Y(); M.Z(); return 0; } I got this error : invalid use of member ‘me::x’ in static

Call subclass static method from inside superclass static method

丶灬走出姿态 提交于 2019-12-02 09:34:43
问题 I have a large set of small, related classes linked together by an interface class. All classes implement a static method, which retrieves and processes data specific to the class. The output of that static method needs to be formatted in at least 2 ways. Since the transformation of one format to the other is always the same and rather trivial (though long), I thought I'd implement it as a concrete, sealed, static method in the superclass. However, then I run into the following problem: % (in

In Java, how can I access static method parameters inside a new listener block?

青春壹個敷衍的年華 提交于 2019-12-02 06:02:42
问题 I have a static method that accepts a couple of parameters. Inside the method I am creating a new object and attaching a new listener to it. The problem is that the listener block needs access to the outer static method variables, but I don't know how to reference them. I know how to make this happen with a non static method, but not with a static one. Here is the code: v.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event

How to pass a class variable as a default value in a static method in Python

吃可爱长大的小学妹 提交于 2019-12-02 04:54:01
问题 I want to pass a class variable as a default value to a static method. But when I import the class I get an error NameError: name 'MyClass' is not defined class MyClass: x = 100 y = 200 @staticmethod def foo(x = MyClass.x, y = MyClass.y): return x*y 回答1: MyClass is not defined yet when Python wants to bind the default arguments, but x and y are already defined in the classes' scope. In other words, you can write: class MyClass: x = 100 y = 200 @staticmethod def foo(x=x, y=y): return x*y Note

What's the correct way of retrieving my custom enumeration classes by their value?

风格不统一 提交于 2019-12-02 03:31:13
I've created my own custom pseudo enumerations within my domain model to allow me to have some more verbose values. For example, my class is as follows: public abstract class Enumeration<X, Y> : IComparable where X : IComparable { public Enumeration(X value, Y displayName) { } public Y DisplayName { get { return _displayName; } } public X Value { get { return _value; } } } And a class that inherits it would be: public class JobType : Enumeration<string, string> { public static JobType ChangeOver = new JobType("XY01", "Changeover"); public static JobType Withdrawal = new JobType("XY02",

Generics in static methods

ぐ巨炮叔叔 提交于 2019-12-02 02:50:15
I need to add a method in a utility class with some static methods that parses things from a JSON string and returns an array of things. Problem is that there are various subtypes of these things, so I created this method: public static <E extends Thing> E[] parseThingsFromJSON(String body) { return parser.fromJson(body, E[].class); } How does the caller tell this method what E is? Or is there a better way to do this? You need to pass it. public static <E extends Thing> E[] parseThingsFromJSON(String body, Class<E[]> eClass) { return parser.fromJson(body, eClass); } Generics is largely a

In Java, how can I access static method parameters inside a new listener block?

穿精又带淫゛_ 提交于 2019-12-02 01:29:51
I have a static method that accepts a couple of parameters. Inside the method I am creating a new object and attaching a new listener to it. The problem is that the listener block needs access to the outer static method variables, but I don't know how to reference them. I know how to make this happen with a non static method, but not with a static one. Here is the code: v.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_UP: ((Activity)*context*).startActivityForResult(*intent*, 0);

Using MySQLi's real_escape_string as a static function

房东的猫 提交于 2019-12-02 00:25:47
问题 I'm wondering if I could escape strings (using real_escape_string) without first creating an object instance to apply the function to? i.e, we can do this: $database = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME); $database->real_escape_string($query); $database->query($query) etc. However, what I'm trying to do for consistency within my application, is to have a mostly static database class which is an extension of the MySQLi class, so that I could call: database::real_escape_string($query) ,

Using MySQLi's real_escape_string as a static function

こ雲淡風輕ζ 提交于 2019-12-01 23:41:41
I'm wondering if I could escape strings (using real_escape_string) without first creating an object instance to apply the function to? i.e, we can do this: $database = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME); $database->real_escape_string($query); $database->query($query) etc. However, what I'm trying to do for consistency within my application, is to have a mostly static database class which is an extension of the MySQLi class, so that I could call: database::real_escape_string($query) , a static method. I do realise that I could build a function which escapes the string manually without

Java interface static method workaround?

人盡茶涼 提交于 2019-12-01 22:56:00
问题 We have a given REST interface: POST /calculation <data>abc</data> This calculation can be implemented by different logical "calculators" depending on the server config. We are now designing the Java interface that each calculator must implement. The interface will have a method for each REST service. Given that all REST (and HTTP) calls are stateless, each method should be static. However you can't define static methods in Java interfaces. Is there a good workaround for this situation? We