1 /*2 Super Keyword:3 4 The Super keyword represents a reference to the parent class space. 5 6 The Super keyword works:7 1. When the child parent class has a member with the same name, the child class is accessed by default as a member of the subclass, and the members of the parent class can be accessed through the Super keyword. 8 2. When you create a subclass object, the parent class is called without a parameter by default, and you can specify the constructor method for calling the parent class with the Super keyword. 9 Ten The Super keyword calls the parent class construction method to note the following: One 1. If the constructor of the parent class is not specified on the constructor of the subclass, then the Java compiler adds the super () statement to the constructor of the child class. A 2. When the Super keyword calls the constructor of a parent class, the statement must be the first statement in the subclass constructor. - 3. The super and the This keyword cannot be present in the same constructor to call other constructors. Because the first statement is required for two statements. - the - The Super keyword differs from the This keyword: - 1. The things represented are inconsistent. - 1. The Super keyword represents a reference to a parent class space. + 2. The This keyword represents the caller object of the owning function. - 2. Inconsistent use of premises. + 1. The Super keyword must have an inheritance relationship before it can be used. A 2. The This keyword does not require an inheritance relationship to be used. at 3. The difference between calling constructors: - 1. The Super keyword is the constructor that invokes the parent class. - 2. The This keyword is a constructor that calls this class. - */ - classfu{ - in intx = 10; - to String name; + - the PublicFu () { *System.out.println ("Fu class no parameter construction method:"); $ }Panax Notoginseng - PublicFu (String name) { the This. Name =name; +System.out.println ("The method of constructing the Fu class with parameters:"); A } the + - Public voideat () { $System.out.println ("Small head father eats sweet potato."); $ } - } - the - classZiextendsfu{Wuyi the intx = 20; - Wu intnum; - About PublicZi (String name,intnum) { $ Super(name);//Specifies the constructor method that called the parent class with parameters ... - This();//call this class without a parameter construction method : - //super ();//specifies that the parent class is called without a parameter construction method ... -System.out.println ("constructor method for Zi class with parameters:"); A } + the PublicZi () { -System.out.println ("Zi class no parameter construction method:"); $ } the the the Public voidprint () { theSystem.out.println ("x =" +Super. x); - } in the Public voideat () { theSystem.out.println ("Big head son eats lobster."); About } the } the the classDemo9 { + - Public Static voidMain (string[] args) the {BayiZi z =NewZi ("Dog Doll")); the the - } -}
Java Road Super