Thoroughly understand the abnormal treatment of Java -3

zhaozj2021-02-17  46

four. Abnormal inheritance

1. About the abnormality in the constructor

1.1 Abnormal rules in the constructor

A "exceptional size interface" of a Derived class constructor can be wide than the exceptional specifications of the constructor of the parent class they call, but must not narrow.

1) The constructor of Derived Class must declare the exception declared in the exception specification of all Base Class constructor in its own exception specifications.

2) The new exception can also be declared in the exception specifications of the constructor of the Derived Class, ie the abnormality that is not declared in the exception specification of the base class constructor.

1.2 reasons

When you generate a DeriveD Class's object, the Base Class constructor is called in the DeriveD Class constructor (see Chapter 6), so the Base Class constructor may be thrown in the Derived Class constructor. The exception declared in an exception specification, so the exception declared in the exception declaration of the Base Class constructor is declared in the exception specification of the Derive Class.

**: If the exception is declared in the exception specification of the called function, the exception declared in its exception specification is called when calling the function. However, in the DeriveD class constructor, it cannot capture the exception thrown from its base class constructor.

2. About the unusual rules of non-configuration functions

2.1 "Abnormal Specifications Interface" of a function can be narrowed in inheritance and overload, but never be widened

To overwrite the Base Class's function, if an exception is declared in the exception of the overwriting function (function in the base class), the overwriting function (the function of the Derived Class is written over the Base Class) exception Specifications can be declared (1) Exactly the same abnormality as the overwritten function; (2) Partially exception in the abnormal composition of the overwritten function or its sub-class abnormality; (3) does not declares an exception specification.

2.2 reasons

This is to meet the principle of "code that can handle the code to overwrite the function, do not do any modification can handle the code" code to handle the code ".

If the abnormal specifications of the overwriting function declares that there is no exception in the abnormal specification of the overwritten function, the code that can handle the overwritten function cannot handle the overwritten function, because there is no capture overwriting function to be overwritten Abnormal declaration in the function.

Import java.sql.sqlexception;

Class baseclass {

Public void f () {}

}

Class DerivedClass1 Extends Baseclass {

// public void f () throws sqlexception {} (1)

Public void f () {} // (2)

}

Public class test {

Public Static Void F (BaseClass BC) {bc.f ();

/ * (3)

Public Static Void F (BaseClass BC) {

Try {

Bc.f ();

}

Catch (SQLException EX) {}

}

* /

Public static void main (String [] args) {

Baseclass bc = new baseclass ();

f (bc);

DerivedClass1 DC = new derivedclass1 ();

f (bc);

}

}

If the "exception interface" is allowed to be widen, let's see what results will appear above. First, we can remove the annotations of the code (1) and comment out of the code (2). Since the overwritten F () function in the BaseClass Class does not declares an exception specification, the code (1) overridden writing F () function declares, then f (BaseClass BC) in the Test Class can handle the overwritten F () function. Call, but cannot handle the call to the F () function, because the code overwriting F () function declares an exception specification, and F (BaseClass BC) does not capture. So in order to handle overwritten F () functions, we also have to write code (3) to process functions. 2.3 Anomalus rules for generating objects

When an object is generated, the captured constructed exception declared in the constructor called when the object is generated.

2.4 Experience rules when the function is called

1) When an object is turned upward into its base class, and when the function is called by the transformation, we have to capture the exception declaration of its base class.

2) When the function is used to call the function, simply capture the abnormality of the called overwriting function.

2.5 An example of an abnormal rule in inheritance

Import java.lang.exception;

Class BaseException Extends Exception {}

Class Derived1exception Extends BaseException {}

Class Derived2Exception Extends BaseException {}

Class deived11exception extens deived1exception {}

Class baseclass {

Baseclass () throws deived1exception {}

Baseclass (INT i) throws baseexception {}

BaseClass (Int i, int J) {}

/ / Do not declare anomalous specification when overwriting f ()

Public void f () {}

/ / Can not declare an exception or declaration of BaseException during overwriting G ()

// baseException exception subclass

Public void g () throws baseexception {}

// You can not declare anomalies when overwriting u ()

Public void u () throws deived1exception, derived2exception {}

}

Class DerivedClass1 Extends Baseclass {

// Base Class constructor declares an abnormal processing method

// Declare the exact same abnormality as an exception in the Base Class constructor

DerivedClass1 (INT I) throws deived1exception {}

// Declare an exception in the base class constructor in the Base Class constructor

DerivedClass1 () throws baseException {}

// Declare an exception and new exception in the base class constructor

DerivedClass1 (String S) throws deived1exception, derived2exception {}

// Declare an abnormal parent class exception and new exception in the base class constructor

DerivedClass1 (String S, INT I) throws baseexception, derived2exception {}

DeriveDClass1 (INT I, INT J) {Super (i, j);}

// Note the following two sentences

DerivedClass1 (Int I, String S) throws baseException {super (i);} //! DerivedClass1 (INT I, STRING S) THROWS DERIVED1EXCEPTION {Super (i);

Public void f () {}

// Cover the g () method below

// does not declare

// public void g () {}

// public void g () throws baseException {} declaration is exactly the same

// The statement is abnormal

Public void g () throws derived1exception {}

// The statement is abnormal

// public void g () throws derived1exception, derived11exception {}

// Remove the U () of U () below

// public void u () {} does not declare

// public void u () throws deived11exception {} declared an exception

// public void u () throws deived1exception {} declaration part exception

// The statement is exactly the same

// public void u () throws derived1exception, derived2exception {}

// The statement is abnormal

// public void u () throws derived1exception, derived11exception {}

}

Public class test {

Public static void main (String [] args) {

// Captured is the abnormality of the corresponding constructor

Try {

Baseclass bc1 = new derivedclass1 ();

}

Catch (BaseException BE) {}

Try {

BaseClass BC2 = New DeriveDClass1 ("BC2");

}

Catch (Derived1exception BE1) {}

Catch (Derived2Exception BE2) {}

/ / Call the function by rotating upward

Baseclass BC3 = New DeriveDClass1 (1, 1);

/ * Captured the exception of the function overwritten in the parent class, and the subclass is captured

* Abnormality of the overwritten function, so compiling errors

Try {

BC3.G ();

}

Catch (Derived1exception BE) {}

* /

// Capture the abnormality of the overwritten function, is correct

Try {

BC3.G ();

}

Catch (BaseException BE) {}

// Call the function with the original type of object

DerivedClass1 DC1 = New DeriveDClass1 (1, 1);

// Just capture the abnormality of the called overwriting function

Try {

DC1.G ();

}

Catch (Derived1exception BE) {}

}

}

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

New Post(0)