OOP06_Abstract_Classes.py
python_old/OOP06_Abstract_Classes.py
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Rectangle(Shape): # Rectangle is a type of Shape
def __init__(self, w, h):
self.w = w
self.h = h
def area(self): # must implement abstract method
return self.w * self.h
class Circle(Shape):
def __init__(self, r):
self.r = r
def area(self): # must implement abstract method
return 3.14 * self.r ** 2
if __name__ == "__main__":
# s = Shape() # Abstract class cannot be instantiated
r = Rectangle(3, 4)
c = Circle(5)
print(r.area())
print(c.area())
相關文章
01_classical_caesar_cipher.py
01_classical_caesar_cipher.py — python source code from the python old learning materials (python_old/Cryptography/01_classical_caesar_cipher.py).
閱讀文章 →02_classical_monoalphabetic.py
02_classical_monoalphabetic.py — python source code from the python old learning materials (python_old/Cryptography/02_classical_monoalphabetic.py).
閱讀文章 →03_classical_rail_fence.py
03_classical_rail_fence.py — python source code from the python old learning materials (python_old/Cryptography/03_classical_rail_fence.py).
閱讀文章 →04_frequency_analysis.py
04_frequency_analysis.py — python source code from the python old learning materials (python_old/Cryptography/04_frequency_analysis.py).
閱讀文章 →05_cryptographic_hashing.py
05_cryptographic_hashing.py — python source code from the python old learning materials (python_old/Cryptography/05_cryptographic_hashing.py).
閱讀文章 →06_password_hashing.py
06_password_hashing.py — python source code from the python old learning materials (python_old/Cryptography/06_password_hashing.py).
閱讀文章 →