super() e a sobreposição de membros - Python Orientado a Objetos
This commit is contained in:
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"window.zoomLevel": 1,
|
"window.zoomLevel": 2,
|
||||||
"editor.fontSize": 26,
|
"editor.fontSize": 26,
|
||||||
"editor.hover.enabled": true,
|
"editor.hover.enabled": true,
|
||||||
"workbench.startupEditor": "none",
|
"workbench.startupEditor": "none",
|
||||||
|
|||||||
64
aula139.py
Normal file
64
aula139.py
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
# super() e a sobreposição de membros - Python Orientado a Objetos
|
||||||
|
# Classe principal (Pessoa)
|
||||||
|
# -> super class, base class, parent class
|
||||||
|
# Classes filhas (Cliente)
|
||||||
|
# -> sub class, child class, derived class
|
||||||
|
# class MinhaString(str):
|
||||||
|
# def upper(self):
|
||||||
|
# print('CHAMOU UPPER')
|
||||||
|
# retorno = super(MinhaString, self).upper()
|
||||||
|
# print('DEPOIS DO UPPER')
|
||||||
|
# return retorno
|
||||||
|
|
||||||
|
|
||||||
|
# string = MinhaString('Luiz')
|
||||||
|
# print(string.upper())
|
||||||
|
|
||||||
|
class A(object):
|
||||||
|
atributo_a = 'valor a'
|
||||||
|
|
||||||
|
def __init__(self, atributo):
|
||||||
|
self.atributo = atributo
|
||||||
|
|
||||||
|
def metodo(self):
|
||||||
|
print('A')
|
||||||
|
|
||||||
|
|
||||||
|
class B(A):
|
||||||
|
atributo_b = 'valor b'
|
||||||
|
|
||||||
|
def __init__(self, atributo, outra_coisa):
|
||||||
|
super().__init__(atributo)
|
||||||
|
self.outra_coisa = outra_coisa
|
||||||
|
|
||||||
|
def metodo(self):
|
||||||
|
print('B')
|
||||||
|
|
||||||
|
|
||||||
|
class C(B):
|
||||||
|
atributo_c = 'valor c'
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
# print('EI, burlei o sistema.')
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def metodo(self):
|
||||||
|
# super().metodo() # B
|
||||||
|
# super(B, self).metodo() # A
|
||||||
|
# super(A, self).metodo() # object
|
||||||
|
A.metodo(self)
|
||||||
|
B.metodo(self)
|
||||||
|
print('C')
|
||||||
|
|
||||||
|
|
||||||
|
# print(C.mro())
|
||||||
|
# print(B.mro())
|
||||||
|
# print(A.mro())
|
||||||
|
c = C('Atributo', 'Qualquer')
|
||||||
|
# print(c.atributo)
|
||||||
|
# print(c.outra_coisa)
|
||||||
|
c.metodo()
|
||||||
|
# print(c.atributo_a)
|
||||||
|
# print(c.atributo_b)
|
||||||
|
# print(c.atributo_c)
|
||||||
|
# c.metodo()
|
||||||
Reference in New Issue
Block a user