static-methods

C++ type of enclosing class in static member function

南楼画角 提交于 2019-11-27 09:13:38
I assume this is outright impossible, but what if. Is it possible to somehow get type of enclosing class in a static member function, in any version of C++? class Impossible { public: static void Fun() { typedef Impossible EnclosingClass; // now do something with EnclosingClass ... } } Is there a way to get the type of the enclosing class ( Impossible in this case) without writing the name of the class in the function? The reason why I'd like to do that is to avoid repeating the class name in the function. It could easily lead to a hard to find copy-paste bug, if something like this happened:

Inheritance in Static Methods

时光总嘲笑我的痴心妄想 提交于 2019-11-27 09:05:55
Why does the below code print "Main"? public class Main { public static void method() { System.out.println("Main"); } public static void main(String[] args) { Main m = new SubMain(); m.method(); } } class SubMain extends Main { public static void method() { System.out.println("SubMain"); } } At runtime, m is pointing to an instance of Submain , so it should conceptually print "SubMain". Static methods are resolved on the compile-time type of the variable. m is of type Main , so the method in Main is called. If you change it to SubMain m ... , then the method on SubMain will be called. It is

Why aren't static methods considered good OO practice? [closed]

心不动则不痛 提交于 2019-11-27 07:34:29
I'm reading Programming Scala . At the beginning of chapter 4, the author comments that Java supports static methods, which are "not-so-pure OO concepts." Why is this so? One reason that static methods aren't very OO that hasn't been mentioned so far is that interfaces and abstract classes only define non-static methods. Static methods thus don't fit very well into inheritance. Note also that static methods do not have access to " super ", which means that static methods cannot be overridden in any real sense. Actually, they can't be overridden at all, only hidden. Try this: public class Test

How to get (sub)class name from a static method in Python?

孤人 提交于 2019-11-27 06:34:20
问题 If I define: class Bar(object): @staticmethod def bar(): # code pass class Foo(Bar): # code pass Is it possible for a function call Foo.bar() to determine the class name Foo? 回答1: Replace the staticmethod with a classmethod. This will be passed the class when it is called, so you can get the class name from that. class Bar(object): @classmethod def bar(cls): # code print cls.__name__ class Foo(Bar): # code pass >>> Bar.bar() Bar >>> Foo.bar() Foo 回答2: If you need to find the class information

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

若如初见. 提交于 2019-11-27 06:31:21
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 = __CLASS__; // value: string(9) "BaseModel" // 2. Query the database with id $row = get_row_from_db_as

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

試著忘記壹切 提交于 2019-11-27 06:26:42
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 translation of a Java static method is usually a module-level function, not a classmethod or staticmethod. (And

PHP Can static:: replace self::?

狂风中的少年 提交于 2019-11-27 06:24:50
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? DarkThrone You have to ask yourself: "Am I targeting the problem with the adequated approach?" self:: and static:: do two different things. For instance self:: or __CLASS__ are references to the current class, so defined in certain scope it will NOT

Python: Make class iterable

谁都会走 提交于 2019-11-27 05:27:20
问题 I have inherited a project with many large classes constituent of nothing but class objects (integers, strings, etc). I'd like to be able to check if an attribute is present without needed to define a list of attributes manually. Is it possible to make a python class iterable itself using the standard syntax? That is, I'd like to be able to iterate over all of a class's attributes using for attr in Foo: (or even if attr in Foo ) without needing to create an instance of the class first. I

Using private static methods [duplicate]

醉酒当歌 提交于 2019-11-27 05:27:05
问题 This question already has an answer here: Should private helper methods be static if they can be static 21 answers What do you think about using private static methods ? Personally, I prefer using a static private method to non-static as long as it does not require access to any instance fields. But I heard that this practice violates OOP principles. Edit: I am wondering from style prospective of view, not performance. 回答1: A private static method by itself does not violate OOP per se, but

How do I know if this C# method is thread safe?

本小妞迷上赌 提交于 2019-11-27 05:07:19
问题 I'm working on creating a call back function for an ASP.NET cache item removal event. The documentation says I should call a method on an object or calls I know will exist (will be in scope), such as a static method, but it said I need to ensure the static is thread safe. Part 1: What are some examples of things I could do to make it un-thread safe? Part 2: Does this mean that if I have static int addOne(int someNumber){ int foo = someNumber; return foo +1; } and I call Class.addOne(5); and