Instance methods, static methods, class methods: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{#addbodyclass:tag_tech}} | |||
{{#addbodyclass:tag_prog}} | |||
{{stub}} | |||
(In most languages, the way you will define all of these is considered part of a class) | (In most languages, the way you will define all of these is considered part of a class) | ||
==Instance method== | ==Instance method== | ||
<!-- | |||
For context, many functions defined on a class will be considered '''instance methods''': | For context, many functions defined on a class will be considered '''instance methods''': | ||
Line 37: | Line 39: | ||
That is not the only thing you ever want to do with a function. | That is not the only thing you ever want to do with a function. | ||
--> | |||
==Static method== | ==Static method== | ||
<!-- | |||
you can call them on a class | you can call them on a class | ||
they don't necessarily working on an instance of that class, | they don't necessarily working on an instance of that class, | ||
Line 60: | Line 63: | ||
* MyString.from_regular_string( string_instance ) | * MyString.from_regular_string( string_instance ) | ||
--> | |||
==Class method== | ==Class method== | ||
<!-- | |||
[[class methods]] are defined on a class, and do not take an instance, but ''do'' get a reference to that class, meaning they can fetch class variables (note: not instance variables) | |||
: ...and could potentially alter that class | |||
: ...but probably more usually used for things like [[factories]]?{{verify}} | |||
: from the context of instqance variables, self.__class__ often lets you cheat you way out of needing these (but it's not as clean) | |||
: Not used much unless you do some meta-modelling, or need a reference to the class but not an instance. | |||
--> | --> |
Latest revision as of 14:25, 23 April 2024
✎ This article/section is a stub — some half-sorted notes, not necessarily checked, not necessarily correct. Feel free to ignore, or tell me about it.
(In most languages, the way you will define all of these is considered part of a class)