static-methods

Should private helper methods be static if they can be static

纵饮孤独 提交于 2019-11-26 12:40:49
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 there any particular reason to specify computeOne and computeMore as static methods - or any particular

Difference between static function and singleton class in swift [closed]

谁都会走 提交于 2019-11-26 12:38:32
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . I want to create a class where all utility methods will be kept and these methods will be used throughout the app. Problem:1 Is it good to create a singleton class and keep all necessary methods there or should I create a class where all function will be static. Problem:2

Why doesn't the compiler complain when I try to override a static method?

对着背影说爱祢 提交于 2019-11-26 12:25:48
问题 I know that we cannot override static methods in Java, but can someone explain the following code? class A { public static void a() { System.out.println(\"A.a()\"); } } class B extends A { public static void a() { System.out.println(\"B.a()\"); } } How was I able to override method a() in class B ? 回答1: You didn't override anything here. To see for yourself, Try putting @Override annotation before public static void a() in class B and Java will throw an error. You just defined a function in

Why are class static methods inherited but not interface static methods?

谁都会走 提交于 2019-11-26 12:23:46
问题 I understand that in Java static methods are inherited just like instance methods, with the difference that when they are redeclared, the parent implementations are hidden rather than overridden. Fine, this makes sense. However, the Java tutorial notes that Static methods in interfaces are never inherited. Why? What\'s the difference between regular and interface static methods? Let me clarify what I mean when I say static methods can be inherited: class Animal { public static void identify()

Module function vs staticmethod vs classmethod vs no decorators: Which idiom is more pythonic?

烈酒焚心 提交于 2019-11-26 11:59:43
问题 I\'m a Java developer who\'s toyed around with Python on and off. I recently stumbled upon this article which mentions common mistakes Java programmers make when they pick up Python. The first one caught my eye: A static method in Java does not translate to a Python classmethod. Oh sure, it results in more or less the same effect, but the goal of a classmethod is actually to do something that\'s usually not even possible in Java (like inheriting a non-default constructor). The idiomatic

PHP Can static:: replace self::?

我的未来我决定 提交于 2019-11-26 11:58:15
问题 I am a little confused with this matter. I am designing an ORM class that tries to behave very similarly to ActiveRecord in ruby on rails, but that\'s beside the point. What I\'m trying to say is that my class makes extensive use of static attribute inheritance, specially for database and table handling. My question is, should I use self:: at all? 回答1: You have to ask yourself: "Am I targeting the problem with the adequated approach?" self:: and static:: do two different things. For instance

What's the difference between “class method” and “static method”?

橙三吉。 提交于 2019-11-26 11:45:49
I've worked with a few different languages such as Java, C#, and Objective-C. In most languages, methods that don't require an instance of an object are called static methods. However, when it comes to Objective-C, some people get defensive when you call them static methods, and they expect you to call them class methods. Why are they called class methods instead of static methods? What is the difference between a static method and a class method? So my question is why are they called class methods instead of a static method? What is the difference between a static method and a class method?

Is there a way to force a C# class to implement certain static functions?

痞子三分冷 提交于 2019-11-26 11:20:04
问题 I am developing a set of classes that implement a common interface . A consumer of my library shall expect each of these classes to implement a certain set of static functions. Is there anyway that I can decorate these class so that the compiler will catch the case where one of the functions is not implemented. I know it will eventually be caught when building the consuming code. And I also know how to get around this problem using a kind of factory class. Just curious to know if there is any

c++ automatic factory registration of derived types

邮差的信 提交于 2019-11-26 11:02:53
问题 Like many before me, I\'m trying so get my derived types to automatically register with my factory. I read through many question and tried to focus on what I didn\'t find there. I\'ve got everything running nicely except the automatic registration. My Goals: automatically register any derived class of my base class Base only classes I mark as registrable not only direct sub-classes of Base ex: Base -> Device -> Camera -> Webcam this would make using the CRTP like described in this question

Can a static method be overridden in C#?

大兔子大兔子 提交于 2019-11-26 09:40:58
问题 I was told that static methods are implicitly final and therefore can\'t be overridden. Is that true? Can someone give a better example of overriding a static method? If static methods are just class methods, what is the real use of having them? 回答1: (1) Static methods cannot be overridden, they can however be hidden using the 'new' keyword. Mostly overriding methods means you reference a base type and want to call a derived method. Since static's are part of the type and aren't subject to