Junior — Middle
What types of methods does a class have in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming, a class typically contains the following types of methods:
-
Instance methods — operate on a specific object of the class, have access to its attributes through
self. -
Class methods — operate on the class itself, not on an instance, receive the class as the first argument (
cls), declared with the@classmethoddecorator. -
Static methods — do not have access to the instance or the class, perform auxiliary functions, declared with
@staticmethod.
Example in Python:
class MyClass:
def instance_method(self):
print(f'Instance method called on {self}')
@classmethod
def class_method(cls):
print(f'Class method called on {cls}')
@staticmethod
def static_method():
print('Static method called')
Each method type serves its purpose and is used depending on the task.