# Modul Praktikum Pemrograman Berorientasi Objek: Metode ## 1. Pendahuluan Metode adalah fungsi yang didefinisikan di dalam sebuah kelas dan digunakan untuk memanipulasi data dari objek kelas tersebut. Metode biasanya memerlukan setidaknya satu parameter, yaitu `self`, yang merujuk pada instance (objek) dari kelas tersebut. Dalam Python, metode berperan penting untuk menjaga enkapsulasi dan menyediakan cara interaksi yang lebih baik dengan objek. ### Tujuan Praktikum 1. Memahami konsep metode dalam OOP. 2. Mengimplementasikan berbagai jenis metode dalam program Python. 3. Memahami perbedaan antara metode instance, class method, dan static method. ## 2. Dasar Teori ### Metode (Methods) Metode adalah fungsi yang terikat pada objek kelas. Terdapat beberapa jenis metode dalam Python: 1. **Metode Instance**: Metode yang bekerja pada objek instance dari sebuah kelas. 2. **Metode Kelas (Class Method)**: Metode yang terikat pada kelas itu sendiri, bukan objek individual. Didefinisikan dengan `@classmethod` dan parameter `cls`. 3. **Metode Statis (Static Method)**: Metode yang tidak terikat pada objek maupun kelas, dan tidak membutuhkan parameter `self` atau `cls`. Didefinisikan dengan `@staticmethod`. ### Struktur Dasar ```python class MyClass: def instance_method(self): # instance method pass @classmethod def class_method(cls): # class method pass @staticmethod def static_method(): # static method pass ``` ## 3. Contoh Use Case dan Analisis ### Contoh 1: Metode Instance Sederhana ```python class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): print(f"Hello, my name is {self.name} and I am {self.age} years old.") # Membuat objek dari kelas Person p1 = Person("Alice", 30) p1.greet() ``` **Analisis:** - `__init__`: Konstruktor yang digunakan untuk menginisialisasi atribut objek. - `greet`: Metode instance yang menggunakan parameter `self` untuk mengakses data objek (`name` dan `age`). --- ### Contoh 2: Metode Kelas ```python class Circle: pi = 3.14 def __init__(self, radius): self.radius = radius @classmethod def change_pi(cls, new_pi): cls.pi = new_pi # Membuat objek dari kelas Circle c1 = Circle(5) Circle.change_pi(3.14159) print(Circle.pi) ``` **Analisis:** - `change_pi`: Metode kelas yang mengubah nilai atribut kelas `pi` menggunakan parameter `cls`. - `Circle.change_pi(3.14159)`: Memanggil metode kelas untuk mengubah nilai konstanta pi di seluruh instance Circle. --- ### Contoh 3: Metode Statis ```python class MathOperations: @staticmethod def add(a, b): return a + b @staticmethod def multiply(a, b): return a * b # Memanggil metode statis tanpa membuat objek print(MathOperations.add(5, 3)) print(MathOperations.multiply(4, 2)) ``` **Analisis:** - `add` dan `multiply`: Metode statis yang tidak membutuhkan instance atau atribut kelas. - Metode ini dapat dipanggil langsung melalui kelas tanpa perlu membuat instance. --- ### Contoh 4: Menggunakan Metode Instance dan Atribut ```python class BankAccount: def __init__(self, owner, balance=0): self.owner = owner self.balance = balance def deposit(self, amount): self.balance += amount def withdraw(self, amount): if amount > self.balance: print("Insufficient funds") else: self.balance -= amount # Membuat objek BankAccount account = BankAccount("John") account.deposit(1000) account.withdraw(500) print(f"Remaining Balance: {account.balance}") ``` **Analisis:** - `deposit` dan `withdraw`: Metode instance yang memodifikasi atribut objek (`balance`). - `account.deposit(1000)` dan `account.withdraw(500)`: Metode untuk menyetor dan menarik uang dari akun. --- ### Contoh 5: Metode Kelas dan Atribut Kelas ```python class Employee: raise_amount = 1.05 def __init__(self, name, salary): self.name = name self.salary = salary def apply_raise(self): self.salary = self.salary * Employee.raise_amount @classmethod def set_raise_amount(cls, amount): cls.raise_amount = amount # Mengubah nilai raise_amount melalui metode kelas Employee.set_raise_amount(1.1) # Membuat objek Employee e1 = Employee("John", 5000) e1.apply_raise() print(f"New Salary: {e1.salary}") ``` **Analisis:** - `apply_raise`: Metode instance yang menggunakan atribut kelas `raise_amount`. - `set_raise_amount`: Metode kelas yang mengubah nilai `raise_amount` untuk seluruh instance Employee. --- ### Contoh 6: Metode Statis dalam Pengolahan Data ```python class TemperatureConverter: @staticmethod def celsius_to_fahrenheit(celsius): return (celsius * 9/5) + 32 @staticmethod def fahrenheit_to_celsius(fahrenheit): return (fahrenheit - 32) * 5/9 # Mengonversi suhu tanpa membuat instance temp_c = 100 temp_f = TemperatureConverter.celsius_to_fahrenheit(temp_c) print(f"{temp_c}°C is {temp_f}°F") ``` **Analisis:** - `celsius_to_fahrenheit` dan `fahrenheit_to_celsius`: Metode statis yang melakukan konversi suhu tanpa memerlukan data dari objek. - Pemanggilan dilakukan langsung melalui kelas tanpa perlu membuat instance. ## 4. Kesimpulan Metode dalam OOP Python memainkan peran penting dalam interaksi dengan objek. Metode instance digunakan untuk memanipulasi data objek, sedangkan metode kelas dan metode statis digunakan untuk tugas yang tidak bergantung pada instance tertentu. Dalam praktik, penggunaan metode membantu menciptakan struktur kode yang lebih bersih dan modular.