Inheritance of constructor

zhaozj2021-02-16  52

Inheritance of constructor (the first edition of Jingshui

This is my reading note, I hope to learn Java to learn Java. All code is tested, the test environment: Java Version "1.4.0-RC" Java (TM) 2 Runtime Environment, Standard Edition (Build 1.4.0-RC-B91) Java Hotspot (TM) Client VM (Build 1.4.0 -RC-B91, MIXED MODE, if you find any mistakes, or if you have any opinions, please don't know.

The problem of default constructor: The base class is a parent class. The DeriveD class is a subclass. The first thing to explain is that there is a subclass after the parent class, so there is a parent class before generating a subclass. Class is generated by the Class constructor constructor, each class has a constructor, if you do not write any constructor when writing your own class, then the compiler automatically generates a default default constructor. This Default constructor is essential, which does not contain any code. But I will involve inheritance, and its problem will appear.

If the parent class base class is only default constructor, that is, the compiler automatically generates you. And only the default constructor is default, then no problems are created, because when you try to generate an instance of a sub-class, first perform subclasses constructor, but due to subclasses inherit the parent class, Subwate The default constructor automatically calls the default constructor of the parent class. First generate an instance of the parent class, then generate an instance of a subclass. as follows:

Class Base {} class derived extends base {public static void main (string [] args) {derived d = new derived ();}}

Below I am explicitly plus the default constructor: class base {base () {system.out.println ("base constructor");}} class deact extends base {derived () {system.out.println "derived constructor");} public static void main (string [] args) {derived d = new derived ();}}

The execution result is as follows: Describes the first base class and then the Derived Class. Base ConstructOrderived Constructor

The problem I want to explain is that if the base class has multiple constructor and Derived Class also has multiple constructor. At this time, the constructor in the subclass defaults to the constructor of that parent class defaults? The answer is to call the default constructor of the parent class. But not the compiler automatically generates the default constructor, but the default constructor you own, which is explicitly written.

Class base {base () {system.out.println ("base constructor");} base (int i) {system.out.println ("base constructor int i");}} class deact extends base {Derived () {System.out.println ("Derived Constructor");} Derived (INT i) {system.out.println ("Derived Constructionor INT I");} public static void main (string [] args) {Derived D = New Derived (); derived t = new derived (9);}} D: / java / thinking / think6> java derivedbase constructOrid ConstructorBase ConstructorDerived Construction Port i

If you comment out the Base class's constructor, an error is wrong.

Class base {// base () {// system.out.println ("base constructor"); //} Base (INT i) {system.out.println ("base constructor int i");}} Extends base {derived () {system.out.println ("Derived Constructor");} Derived (INT i) {system.out.println ("Derived Constructor Int i");} public static void main (String [] args ) {Derived d = new derived (); derived t = new derived (9);}}

D: / java / thinking / think6> javac derived.javaderived.java:10: can not resolve symbolsymbol: constructor base () location: class base derived () {^ derived.java: 13: can not resolve symbolsymbol: constructor base () location : Class Base Derived (INT I) {^ 2 ErrorS

Note The constructor in the subclass cannot find the default constructor in the flexible class, so an error.

So what should I do if you don't want to constructor's constructor calling your explicitly written default constructor? Such examples:

Class base {// base () {// system.out.println ("base constructor"); //} Base (INT i) {system.out.println ("base constructor int i");}} class derived Extends base {derived () {super (8); system.out.println ("Derived Constructor");} Derived (INT i) {Super (i); System.out.Println ("Derived Constructor Int i"); } public static void main (String [] args) {derived d = new derived (); derived t = new derived (9);}} D: / java / thinking / think6> java derivedbase constructor int iderived constructorbase constructor int iderived constructor INT i

Super (i) indicates the constructor of the parent class Base (i) Please pay attention to the Super (i) one is super (8). Do you think about why? ?

Conclusion: If there is a plurality of constructors, the parent class either does not construct a function, allowing the compiler to automatically generate, then execute the default constructor of the parent class generated by the compiler before executing the subclass constructor; Either there is at least one of the explicit default constructors to make the subclass constructor.

Quietwater 2003.2.8

转载请注明原文地址:https://www.9cbs.com/read-23714.html

New Post(0)