Chapter 8 Interfaces and Internal Hypersology
I. Interface
1. If all functions in the interface are implemented, this Class must be declared as the Abstract Class, and the function that is not implemented in the interface is Abstract Class in this Class.
interface Interface {public void f (); public void g ();} abstract class First implements Interface {public void f () {}} class Second extends First {public void g () {}} public class ExplicitStatic {public static void Main (String [] args) {interface f = new second (); ff (); fg ();}}
2. All functions in the interface automatically have public access, so when an interface is implemented, it must be assigned to all functions from the interface to public.
Interface MyInterface {public void f (); void g ();} class first imports myinterface {public void f () {} //! Void g () {} error should be defined as public}
3. Data members in the interface automatically become Static and Final
Interface MyInterface {INT I = 5; Void f (); Void g ();} Class first imports myinterface {public void f () {}} public void g () {}} public class explicitstatic}} public ] Args) {MyInterface x = new first (); // myinterface data member i is static, you can call SYSTEM.OUT.Println directly ("MyInterface.i =" MyInterface.i ", xi =" xi) ////Terface data member i is Final, cannot modify //x.i ; // myinterface.i ;}}
4. Multiple Inheritance 1) Devriced Class can simultaneously inherit multiple Interface and an Abstract or Concrete Base Class. If the base class and interface are inherited, then write the name of the iconic class first, and then the name of the Interfaces. 2) If the DERIVED CLASS is inherited with the same function as the interfaces, then the function can not be implemented at Derived Class.
interface CanFight {void fight ();} interface CanSwim {void swim ();} class ActionCharacter {public void fight () {}} class Hero extends ActionCharacter implements CanFight, CanSwim {public void swim () {};} public class ExplicitStatic {Static void f (canfight x) {x.fight ();} static void s {x.swim ();} static void A (an actioncharacter x) {x.fight ();} static void h Hero x) {x.fight (); x.swim ();} public static void main (string [] args) {HERO H = new helo (); f (h); s (h); A (h) H (h);}} Because there is a function Fight () that is exactly the same as the interface CANFight in the ActionCharacter Class, the front () method can be implemented in Hero Class. When you want to call X.fight (), the front () function in the ActionCharacter Class is called. 3) Name conflict problems when the interface is combined
Interface I1 {void f ();} interface i2 {int F (int 1);} interface i3 {int f ();} class c {public int f () {return 1;}} Class C2 Implements I1, I2 { Public void f () {} public int f (int 1) {return 1;}} Class C3 Extens C imports I2 {public int f (int 1) {return 1;}} Class C4 Extends c imports i3 {public int F () {RETURN 1;}} // Class C5 Extends C Implements I1 {} (a) // Class C6 Extends C Implements I1 {Public Void F () {}} (b) Interface i4 Extends I1, I3 {} / / (C) Class C7 IMPLEMENTS I4 {public void f () {} public int f () {return 1;}}
(A) The code will produce the following error: method f () in class c cannot import method f () in Interface I1 with Different Return Type, WAS Void. (B) The code is also wrong: Method F () in class c6 cannot Override Method F () in class c with diffreent return Type, WAS INT. It can also be seen from the (b) code, although you try to implement the function in the interface I1, but because the extends C is in front, the compiler will regard the function in the C6 as a function over Class C, not Like functions of functions in the interface you imagined. (C) The coda is in the original book (P253), it will be wrong, but I didn't have an error when I was tested. But when you try to implement the interface i4 by C7, it is impossible to compile. 4) The only place in Java can use multiple inheritance places that do not allow multiple inherits by keyword Extends, but except for the expansion of the interface by multiple inheritance. Interface I1 {void f1 (); interface I2 {void f2 (); interface ie1 () {void fe1 ();} copUBlic void f2 () {} public void f2 () {}} interface IE2 Extends IE1, I1 {Void Fe2 ();} CE2 Implements IE2 {public void f2 () {} public void f2 () {} public void f1 () {} public void f1 () {}
Interface IE2 inherits two interfaces. 5. Nested Interfaces Nested Interfaces can be used outside of the external class (interface) defining the internal interface (but in). 1) When the interface is nested in the Class a), regardless of the interface as public, Friendly or Private, three nested classes can be implemented as public, friendly, private. b) The interface that is declared as private cannot be used outside the Class.
Class a {private interface b {void f ();} public class bimp imports b {public void f () {} b {public void f () {} public void f () {} public b getb ()} public b getb () {RETURN NEW BIMP (); PRIVATE B DREF; Public Void Recivedd (B D) {DREF = D; DREF.F () ;;}} Public class explicitstatic}} PUBLIC CLASS ExplicitStatic} {a a = new A ( ); // (a) // ab ab = A.GETB (); (b) //a.bimp = a.getb (); (c) a. gene.Recidd (A.Getb ());}} A class contains an interface, but it can still be instantiated, such as (a). Since the interface B is Private, the interface B is invoked at (b). However, when the interface B is declared as public, (b) will be compiled. But (c) will still be wrong, because the inherently invisible scope is within the external category defined in the invisible class. 2) When the interface is nestled in the interface 1) Nested the interface in the interface to automatically use the public, and can only be PUBLIC. 2) When an interface is implemented, there is no need to implement the nesting interface. 3) The Private interface cannot be implemented outside of the Class it defined.
II. Inner Classes
1. Basic Basic Usage 1) If you want to generate an Inner Class object outside of the peripheral Class's Non-Static function, you can specify the type of the object in the form of OuterClassName.innerClassName. And in the Non-Static function is not available.
public class ExplicitStatic {class Contents {private int i = 11; public int value () {return i;}} class Destination {private String label; Destination (String whereTo) {label = whereTo;} String readLabel () {return label; }}} Public Destination to (String S) {// Innner Class object Return New Destination (s) in the Non-Static function in Outer Class; // (1)} public contents last () {Return New Contents ); // (1)} Public void ship (String DEST) {// Inn, in the Non-Static function of Outer Class, you can specify the object type for CONTENTS C = Cont (); Destination D = TO ( DEST); system.out.println (D.Readlabel ());} public static void main (string [] args) {explicitStatic P = new explicitstatic (); p.ship ("tanzania"); explicitStatic Q = New ExplicitStatic (); // Generate Inner Class object explicitStatic.Content.Contents c = within the non-Non-Static function of Outer Class Q.cont (); explicitStatic.destination D = Q.to ("Borneo"); // You cannot generate Inner Class objects directly // new contents ();}} 2) for non-static inner classes The Non-Static function of the peripheral Class can generate an Inner Class object, as above (1) above. But to create an Inner Class object in a non-Non-Static function, you must be associated with an object of its Enclosing Class. 3) The upward transformation of Inner Class When the Inner Class object is turned upward into an interface, we get just a reference.
interface Destination {String readLabel ();} interface Contents {int value ();} class Parcel3 {private class PContents implements Contents {private int i = 11; public int value () {return i;}} protected class PDestination implements Destination { private String label; PDestination (String whereTo) {label = whereTo;} public String readLabel () {return label;}} public Destination to (String s) {return new PDestination (s);} public Contents cont () {return new PCONTENTS ();}} public class explicitstatic {public static void main (string [] args) {PARCEL3 P = new paradel3 (); // Put the Inner Class object upward converts C = P.cont (); destination d = P .to ("borneo");}} Although we can't call PContents Class in the ExplicitStatic Class, we can call a PContents Class object to Contents. 4) The role of Inner Class is within the scope of the Inner Class. But Inner Class can be inherited outside of its scope (see 4).
interface Contents {int value ();} class Parcel3 {// PContents1 class within a scope Parcel3 class private class PContents1 implements Contents {private int i = 11; public int value () {return i;}} public Contents cont1 ( ) {RETURN New PCONTENTS1 (); PUBLIC Contents Cont2 () {// PCONTENTS2 CLASS Scheme for Function Cont2 Class PCONTENTS2 IMPLEments Contents {Private Int i = 11; Public Int Value () {Return I;}} Return New PCONTENTS2 ();} // cannot use PCONTENTS2 CLASS / * PUBLIC Contents Cont2 () {RETURN NEW PCONTENTS2 () {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{// PContents3 Class The scope is the current IF Class PContents3 Implements Contents {Private Int i = 11; public int value () {return i;}} Return new PContents3 ();} // Cannot use PContent outside IF S3 Class // Return New PContents3 (); return null;}} public class explicitstatic}} public class explicitstatic}} public class explicitstatic}} public class explicitstatic}}} {PARCEL3 P = New para 3 (); contents c1 = p.cont1 (); contents c2 = P.cont2 (); contents c3 = p.cont3;}}
2. Connection relationship with peripheral ENCLOSISING CLASS 2.1 Non-Static Inner Class1) Inner Class Access all members of Enclosing Class (including Private members), just like Inner Class owns themselves. That is, INNER CLASS is natural with access to all members of Enclosing Class. 2) When the Inner Class object is generated, it is necessary to associate an object to its Enclosing Class (this Enclosing Class object is the manufacturer of the Inner Class object). While constructing Inner Class objects, there is a Reference that has its enclosing class object. Cause: Because INNER CLASS can access all members of Enclosing Class, then when an Inner Class is generated, the compiler will automatically add a Reference to the Enclosing Class object to the Inner Class object (this Reference is hidden). So INNER CLASS is generated, must be associated with an object of its Enclosing Class. 3) The INCLOSING Class object is available in the INCLOSING CLASS object, which is the same enclosing class object. interface Destination {String readLabel ();} interface Contents {int value ();} class Parcel3 {int i1 = 10; private String s1 = "Parcel3_"; Parcel3 (String s) {s1 = s;} private class PContents implements Contents {// Members (1) Private I2 = I1; Private String S2 = S1; PCONTENTS ("" Num ": i2 =" i2 ": Num ": i2 = " i2 ", S2 =" S2);} public int value () {RETURN 1;}} public contents cont (INT i) {return new pcontents (i);}} public class explicitstatic}} public class explicitstatic}} ARGS) {PARCEL3 P1 = New Parcel3 ("1"); Contents C1 = P1.cont (1); Contents C2 = P1.Cont (2); PARCEL3 P2 = New Parcel3 ("2"); c2 = p2.cont (3); C2 = p1.cont (4);}}
The result is: 1: I2 = 10, S2 = PARCEL3_12: I2 = 10, S2 = PARCEL3_13: I2 = 10, S2 = PARCEL3_24: I2 = 10, S2 = PARCEL3_1 In Inner Class Calling Enclosing Class member. The results show that the INCLOSING CLASS object P1 generated inner Class object call is a member in the same Enclosing Class object, such as 1, 2, 4 in the results. 2.2 Static Inner Classes 1) When the Static Inner Classes object is generated, there is no need to simultaneously exist in the STATIC INNER CLASS object to access static members in the Enclosing Class in the Static Inner Classes object. interface Contents {int value ();} class Parcel1 {private static String s1 = "Parcel3_"; private String s11 = "Parcel3_"; Parcel1 (String s) {s1 = s;} protected static class PContents implements Contents {// Only the S1 STRING S2 = S1 in the Enclosing Class can only be accessed, and // S11 is not a Static member, and // string 22 = S11; PContents (INT NUM) {system.out.println (" Num ": S2 = " S2);} public int value () {Return 1;}} public static contents last (int i) {return new pcontents (i);}} public class explicitstatic}} public class explicitstatic}}}}}} PARCEL1 P1 = New Parcel1 ("1"); Contents C1 = P1.cont (1); C1 = PARCEL1.CONT (2); // (1) PARCEL1 P2 = New Parcel1 ("2"); C1 = P2. Cont (3); c1 = pacel1.cont (4); // (1)}}
Because the internal hidden PCONTENTS CLASS is static, it is directly generated by the Enclosing Class object through the Enclosing Class object in (1). 2.3 None of the INNER CLASS is nestled, and all of all Outer Class members can be accessed.
Class MNA {Private Void F () {} class a {private void g () {} class b {void h () {g (); f ();}}}} 3. How to generate an Inner Class object 3.1 Non-Static Internal Inclusion 1) In the Non-Static function of Enclosing Class, 2) can be generated directly through New. 2) In the STATIC function or other Class of Enclosing Class, there must be an enclosing Class objects (whose remembol has been described above).
interface Contents {int value ();} class Parcel1 {protected class PContents implements Contents {public int value () {return 1;}} public Contents cont () {// PContents class generated directly by the new non-static function Object return new PContents (); public static void test (string [] args) {PARCEL1 P1 = New paracel1 (); // generates CONTENTS C1 = p1.cont () by external class PARCEL1 objects in the Static function. / / Call function c1 = p1.new pContents (); // via new}} public class explicitstatic {public static void main (string [] args) {// generates PARCEL1 P1 = New paracel1 (); CONTENTS C1 = p1.cont (); // Call function C1 = p1.new pContents (); // through new}}
3.2 STATIC Nike Class 1) In addition to the method of generating Non-Static hidden objects, it is also possible to be generated by existing an Enclosing Class object.
interface Contents {int value ();} class Parcel1 {protected static class PContents implements Contents {public int value () {return 1;}} public Contents cont () {// PContents generated directly by the new non-static function Class object return new pContents (); public static contents cont1 () {// Directly generate PCONTENTS CLASS object Return New PCONTENTS () () in the static function, directly, PCONTENTS (); // (1)} public static void test (String [] Args) {PARCEL1 P1 = New PARCEL1 (); // In the Static function via an external class PARCEL1 object to generate Contents C1 = p1.cont (); // Call function C1 = p1.new pContents (); // through New // Directly generate PContents Class objects directly in the Static function C1 = new pContents (); // (1)}} public class explicitstatic} public static void main (string [] args) {// through external class PARCEL1 object To generate PARCEL1 P1 = New paracel1 (); Contents C1 = p1.cont (); // Call function c1 = p1.new pContents (); // Directly generate C1 = parcel1.cont1 (); // (2)} The code in (1) and 9 (2) above can only pass when the PContents Class is STATIC. (1) See 2.1.4 from the cause of the pass. Inner Class inheritance 1) Inner Class can be inherited. The DRFAULT constructor of the Inner Class's Drived Class must be incompatible to the Outer Object and call the build function of the Outer Class in the constructor.
class WithInner {class Inner {}} class InheritInner extends WithInner.Inner {// InheritInner () {} compile error InheritInner (WithInner wi) {wi.super ();}} public class ExplicitStatic {public static void main (String [] Args) {WITHINNER WI = New With (); Inheritinner II = New Inheritinner (Wi);} 2) Overriding Inner Class does not have polymorphism.
Class egg {class yolk {public yolk () {system.out.println ("egg.yolk ()");}} private yolk y;}} private yolk y; public egg () {system.out.println ("new Egg ()") Y = new yolk (); // (1)}} Class Bigegg Extends Egg {// (2) Try overwriting Inner Class Class Yolk {public yolk () {system.out.println ("Bigegg.Yolk () ");}}} Public class explicitstatic {public static void main (string [] args) {new bigegg (); // (3)}}
The result is: New Egg () Egg.Yolk () We tried to override Inner Class in (2). When a BiGeGG is generated by (3), the constructor of the EGG will be called. The Egg.Yolk Class object is generated at the (1) of the EGG constructor, not a subclass BIGEGG.YOLK CLASS object. **: As shown above, the two INNER CLASs described above are completely independent individuals, each with its own namespace.