Last active
March 31, 2019 20:53
-
-
Save m5wdev/46d51dbf61d1a1e5eb6d0efb861744dd to your computer and use it in GitHub Desktop.
Python 3: Encapsulation, Inheritance, Polymorphism
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Encapsulation | |
class Human(): | |
# __private - переменная | |
__privateVar = "this is __private variable" | |
def __init__(self): | |
self.className = "Human class constructor" | |
self.__privateVar = "this is redefined __private variable" | |
# public - доступен везде | |
def showName(self, name): | |
self.name = name | |
return self.__privateVar + " " + name | |
# __private - Доступен только в базовом классе | |
def __privateMethod(self): | |
return "Private method" | |
# _protected - Доступен в классах - наследниках | |
def _protectedMethod(self): | |
return "Protected method" | |
# __private - Доступен ТОЛЬКО из базового класса | |
def showPrivate(self): | |
return self.__privateMethod() | |
def showProtecded(self): | |
return self._protectedMethod() | |
class Male(Human): | |
def showClassName(self): | |
return "Male" | |
def showPrivate(self): | |
return self.__privateMethod() | |
def showProtected(self): | |
return self._protectedMethod() | |
class Female(Human): | |
def showClassName(self): | |
return "Female" | |
def showPrivate(self): | |
return self.__privateMethod() | |
def showProtected(self): | |
return self._protectedMethod() | |
h = Human() | |
print(h.className) | |
print(h.showName("Vasya")) | |
print(h.showPrivate()) | |
print(h.showProtecded()) | |
# print(h.privateMethod()) | |
# print(h.protectedMethod()) | |
print("\n") | |
m = Male() | |
print(m.className) | |
print(m.showClassName()) | |
# print(m.showPrivate()) | |
print(m.showProtected()) | |
print("\n") | |
f = Female() | |
print(f.className) | |
print(f.showClassName()) | |
print(f.showProtected()) | |
print("\n") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inheritance | |
class Vehicles(): | |
def engine(self): | |
return "Engine" | |
def attribute(self): | |
return "Vehicle" | |
class Helicopter(Vehicles): | |
pass | |
class Submarine(Vehicles): | |
pass | |
class Car(Vehicles): | |
pass | |
v = Vehicles() | |
print(v.engine()) | |
print(v.attribute()) | |
print("\n") | |
h = Helicopter() | |
print(h.engine()) | |
print(h.attribute()) | |
print("\n") | |
s = Submarine() | |
print(s.engine()) | |
print(s.attribute()) | |
print("\n") | |
c = Car() | |
print(c.engine()) | |
print(c.attribute()) | |
print("\n") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Polymorphism | |
class Artillery(): | |
def __init__(self): | |
self.className = "Artillery Class constructor" | |
def showName(self): | |
shootingRange = "aprox. 1-30 km" | |
return "Artillery" + " " + shootingRange | |
class Mortar(Artillery): | |
def showName(self): | |
shootingRange = "1 km" | |
return "Mortar" + " " + shootingRange | |
class Howitzer(Artillery): | |
def __init__(self): | |
self.className = "Howitzer Class constructor" | |
self.rndMath = 2 * 2 | |
def showName(self, color="Green"): | |
shootingRange = "35 km" | |
return "Howitzer " + color + " " + shootingRange | |
class Cannon(Artillery): | |
def showName(self): | |
shootingRange = "10 km" | |
return "Cannon" + " " + shootingRange | |
a = Artillery(); | |
print(a.className) | |
print(a.showName()) | |
print("\n") | |
m = Mortar(); | |
print(m.className) | |
print(m.showName()) | |
print("\n") | |
h = Howitzer(); | |
print(h.className) | |
print(h.rndMath) | |
print(h.showName()) | |
print("\n") | |
x = Howitzer(); | |
print(x.className) | |
print(x.rndMath) | |
print(x.showName("Blue")) | |
print("\n") | |
c = Cannon(); | |
print(c.className) | |
print(c.showName()) | |
print("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment