-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathclass.py
More file actions
34 lines (25 loc) · 686 Bytes
/
class.py
File metadata and controls
34 lines (25 loc) · 686 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from abc import ABC, abstractmethod, abstractproperty
# Kelas abstrak hewan, tidak dapat di-inisialisasi dan hanya berupa instance
class Hewan(ABC):
# Properti abstrak
@abstractproperty
def suara(self):
pass
# Metode abstrak
@abstractmethod
def bersuara(self):
pass
# Menggunakan pewarisan dari kelas Hewan
class Kucing(Hewan):
# Mengimplementasikan properti suara
@property
def suara(self):
return "MIAWW MIAWW"
# Mengimplementasikan atribut bersuara
def bersuara(self):
print(self.suara)
# contoh membuat objek
tomcat = Kucing()
# mengakses metode bersuara
tomcat.bersuara()
# output: "MIAWW MIAWW"