2018-05-01
Super keyword
What is super?
This: Represents the current object itself, or an instance of the current class, through which all methods and properties of this object can be called.
Super: Represents the parent class object of the current object. ( is a reference to the parent class object in the Subclass object )
use super in the Java class to refer to the composition of the parent class, use this to refer to the current object , and if a class inherits from another class, when we new the instance object of the subclass, there will be a parent class object inside the subclass object.
How do you refer to the parent object inside? Using Super to refer to, this refers to a reference to the current object, and super is a reference to the parent object inside the current object.
code example:
1 //Birds2 classBird3 {4 Public voidFly () {5SYSTEM.OUT.PRINTLN ("Free Flight");6 }7 }8 //Penguin9 Ten classPenguinextendsBird One { A - Public voidFly () { -System.out.println ("The angel of the Broken Wing"); the } - Public voidsay () { -SYSTEM.OUT.PRINTLN ("I Want to sing hahaha"); - Super. Fly ();//use Super as the reference object of the parent class object to invoke the Fly () method inside the parent class object + } - } + A //Super keyword at classSuperdemo - { - Public Static voidMain (string[] args) - { - //Create a Penguin object -Penguin p =NewPenguin (); in //call the Fly method - P.say (); to } +}
Output Result:
Memory Analysis:
Reference blog:
Https://www.cnblogs.com/danbing/p/5034108.html
Https://www.cnblogs.com/ahfz/p/5890631.html
Second, Java object-oriented (8) _ Inheritance thought--super keyword