Java Programming Ideology (2nd) Learning Notes (8) -1

zhaozj2021-02-17  34

Chapter 2 Interfaces and Inclusion

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 ();

f.f ();

f.g ();

}

}

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 static void main (String [] args) {

Myinterface x = new first ();

// MyInterface data member i is static, you can call directly

System.out.println ("Myinterface.i =" MyInterface.i ", X.I =" X.i);

// MyInterface's data member i is Final, cannot be modified

//x.i ;

// myinterface.i ;

}

}

4. Multiple inheritance

1) Devriced Class can inherit multiple Interface and an Abstract or Concrete Base Class at the same time. 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 fast () {}

}

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 helroom;

F (h); s (h); A (h); h (h);

}

}

Since 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 I);

Interface I3 {Int f ();

Class C {public int f () {return 1;}}

Class C2 Implements I1, I2 {

Public void f () {}

Public INT F (INT I) {RETURN 1;}

}

Class C3 Extends C IMplements I2 {

Public INT F (INT I) {RETURN 1;}

}

Class C4 Extends C IMplements 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

Java is not allowed to achieve multiple inheritance by keyword Extends, but except for the expansion of the interface by multiple inheritance.

Interface I1 {

Void F1 ();

}

Interface I2 {

Void f2 ();

}

Interface IE1 EXTENDS I2 {Void Fe1 ();

}

Class CE1 Implements IE1 {

Public void f2 () {}

PUBLIC VOID FE1 () {}

}

Interface IE2 Extends IE1, I1 {

Void fe2 ();

}

Class CE2 IMPLEMENTS IE2 {

Public void fe2 () {}

Public void f2 () {}

PUBLIC VOID FE1 () {}

Public void f1 () {}

}

Interface IE2 inherits two interfaces.

5. Nested Interfaces

Nested Interfaces can be used outside of the external class (interface) defined in the internal interface (but in).

1) When the interface is nested in the Class

a) Regardless of the interface as public, Friendly or Private, it can be implemented as public, friendly, private three nested classes.

b) The interface that is declared as private cannot be used outside the Class.

Class a {

Private interface b {

Void f ();

}

Public class BIMP IMPLEMENTS B {

Public void f () {}

}

PRIVATE CLASS BIMP2 IMPLEMENTS B {

Public void f () {}

}

Public B getb () {return new bimp ();

Private b DREF;

Public void reciddd (b d) {

DREF = D;

DREF.F () ;;

}

}

PUBLIC CLASS ExplicitStatic {

Public static void main (String [] args) {

A a = new a (); // (a)

//A.b ab = A.GETB (); (b)

//A.bimp = a.getb (); (c)

A.Recivedd (A.Getb ());

}

}

Although a class contains an interface, 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) The interface nested in the interface is automatically copulic 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.

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

New Post(0)