penso in questo modo
+----------------+
| super |
+----------------+ <-----------------+
| +------------+ | |
| | this | | <-+ |
| +------------+ | | |
| | @method1() | | | |
| | @method2() | | | |
| +------------+ | | |
| method4() | | |
| method5() | | |
+----------------+ | |
We instantiate that class, not that one!
Vorrei spostare che sottoclasse un po 'a sinistra per rivelare ciò che è sotto ... (Uomo, faccio grafica amore ASCII)
We are here
|
/+----------------+
| | super |
v +----------------+
+------------+ |
| this | |
+------------+ |
| @method1() | method1() |
| @method2() | method2() |
+------------+ method3() |
| method4() |
| method5() |
+----------------+
Then we call the method
over here...
| +----------------+
_____/ | super |
/ +----------------+
| +------------+ | bar() |
| | this | | foo() |
| +------------+ | method0() |
+-> | @method1() |--->| method1() | <------------------------------+
| @method2() |^| method2() | |
+------------+ | | method3() | |
| | method4() | |
| | method5() | |
| +----------------+ |
\______________________________________ |
\ |
| |
...which calls super, thus calling the super's method1() here, so that that
method (the overidden one) is executed instead[of the overriding one].
Keep in mind that, in the inheritance hierarchy, since the instantiated
class is the sub one, for methods called via super.something() everything
is the same except for one thing (two, actually): "this" means "the only
this we have" (a pointer to the class we have instantiated, the
subclass), even when java syntax allows us to omit "this" (most of the
time); "super", though, is polymorphism-aware and always refers to the
superclass of the class (instantiated or not) that we're actually
executing code from ("this" is about objects [and can't be used in a
static context], super is about classes).
In altre parole, citando dal Java Language Specification:
La forma super.Identifier
si riferisce al campo denominato Identifier
dell'oggetto corrente , ma con la oggetto corrente visualizzato come istanza di la superclasse della classe corrente.
La forma T.super.Identifier
si riferisce al campo denominato Identifier
della dell'istanza lessicale racchiude corrispondente a T
, ma con quella esempio visto come un'istanza della superclasse di T
.
In parole povere, this
è fondamentalmente un oggetto (* ** l'oggetto, lo stesso oggetto è possibile muoversi nelle variabili), l'istanza della classe istanziata, una variabile pianura nel dominio dei dati; super
è come un puntatore a un blocco di codice preso in prestito che si desidera eseguire, più come una semplice chiamata di funzione, ed è relativo alla classe in cui è chiamato.
Pertanto se si utilizza super
dalla superclasse si ottiene il codice dalla classe superduper [il nonno] eseguito), mentre se si utilizza this
(o se è usato implicitamente) da una superclasse si continua indicando la sottoclasse (perché nessuno l'ha cambiato, e nessuno poteva farlo).
Sospetto che potresti voler "preferire la composizione sull'ereditarietà". –