static-methods

Java: How to refer to subclass's static variable in abstract class?

给你一囗甜甜゛ 提交于 2019-12-08 05:04:33
问题 I understand, thanks to this question, that the value of a static field declared in an abstract class will be the same among all subclasses. The solution in the aforementioned question is to declare a static field in each subclass, and an abstract "getter" instance method in the abstract class that must be implemented by each subclass. But I have a static method in my abstract class, and I need to refer to the static field of the subclass. I can't do this because the getter is an instance

static and non static methods in java [duplicate]

天大地大妈咪最大 提交于 2019-12-08 04:44:49
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: When should a method be static? i am trying to make an interface class for tube/subway ticket machine. well not for real, but for a coursework in a computer science module. i dont understand when to use static methods. i dont know much about computer sciences, but main methods seem to use static. class UNInterfaceTest { public static final int NOTTING_HILL = 1; public static final int HIGH_KEN = 2; public static

Avoiding “Access to a static member of a type via a derived type”

点点圈 提交于 2019-12-07 19:39:39
问题 I believe this is purely a Resharper warning, but the reasoning behind it (explained here) makes good sense. What Greg Beech is saying is that you can call a base-class static method from a sibling class... in his example he uses: var request = (FtpWebRequest)HttpWebRequest.Create(...) ... which is misleading. So is there a design that would allow me to avoid this warning in the following classes? public abstract class BaseLog { // I omitted several other properties for clarity protected

Deciding to make a C# class static

筅森魡賤 提交于 2019-12-07 17:10:54
问题 If I have a Utilities.cs class in C#, which contains only static methods e.g. public static string DoSomething() { return "something"; } Should I make the class itself static? public static Utilities() { ... Does making the class static have any advantages? 回答1: Yes you should. It stops people from instantiating the class when they shouldn't, and makes the purpose of the class clearer. Have a read of the MSDN page for more information. You also need to mark the class as static if you want to

Wrapping an API to support dependency injection

老子叫甜甜 提交于 2019-12-07 16:05:37
问题 I am interacting with an API that just has static functions, and cannot be opened up and changed. public class WindowsNativeGraphAPI { public static IEnumerable<IGraphData> GetGraphData(); public static bool DeleteGraphData(IGraphData data); } I would like to be able to pass the API into a function or constructor and comply with dependency injection (just in case we were to swap out the API later). public void GatherGraphData(IGraphAPI api) {...} To allow this API to be passed in as a

Python: Assigning staticmethod to class variable gives error

℡╲_俬逩灬. 提交于 2019-12-07 14:41:12
问题 I want to assign a static method to a class variable in Python, and below is what my code looks like. class Klass: classVariable = None @staticmethod def method(): print "method called" Klass.classVariable = Klass.method Klass.method() Klass.classVariable() This gave me an error at the last line, TypeError: unbound method method() must be called with Klass instance as first argument (got nothing instead). But when I change the static method to class method it works. Can anyone give me any

How do I infer the class to which a @staticmethod belongs?

落爺英雄遲暮 提交于 2019-12-07 12:06:37
问题 I am trying to implement infer_class function that, given a method, figures out the class to which the method belongs. So far I have something like this: import inspect def infer_class(f): if inspect.ismethod(f): return f.im_self if f.im_class == type else f.im_class # elif ... what about staticmethod-s? else: raise TypeError("Can't infer the class of %r" % f) It does not work for @staticmethod-s because I was not able to come up with a way to achieve this. Any suggestions? Here's infer_class

PowerMockito mock single static method and return object inside another static method

ⅰ亾dé卋堺 提交于 2019-12-07 08:46:09
问题 I have written test cases to mock static classes and methods using PowerMockito's mockStatic feature. But I am strugling to mock one static method inside another static method. I did see few examples including this but none of them actually helping me or I am not understanding the actual functionality? (I'm clueless) Eg. I have a class as below and complete code is here. public static byte[] encrypt(File file, byte[] publicKey, boolean verify) throws Exception { //some logic here PGPPublicKey

Difference between Static function declaration and the normal function declaration in Javascript?

懵懂的女人 提交于 2019-12-07 04:49:45
问题 There are many ways one can declare a function in javascript. One of the ways is declaring a class and a static function inside is as showed below. class className { static fucntionName() { } } another way of is declaring is through the tradition javascript style as showed below. export function functionName() { } I would like to know the advantages/disadvantages of using either of the cases. Is there any specific use cases for the static methods, why declare a class(we know that in

How to monkeypatch a static method? [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-07 01:31:02
问题 This question already has answers here : Pointers to static methods in Python (3 answers) Closed 6 years ago . While it's fairly simple to monkeypatch instance methods to classes, e.g. class A(object): pass def a(self): print "a" A.a = a doing this with another class's @staticmethod à la class B(object): @staticmethod def b(): print "static b" A.b = B.b results in A.b() yielding a TypeError : unbound method b() must be called with A instance as first argument (got nothing instead) 回答1: Make A