Knowledge point - (Private Interface)

xiaoxiao2021-03-06  154

When I mentioned Private Interface in the forum, many people were confused: Interface is not let others be implemented, why have private interfac? It should be said that this is not full, indeed, like Class, the general interface can only be public or friendly (of course, the class can also be final, if you don't want others to override your class, but for Nesting Inteface is You can be declared as private: first look at "Tij" is a simple example: ------------------------------ -------------------------------- //: c08: NestingInterfaces.java// from 'thinking in java, 2nd Ed. 'by Brouce Eckel // www.bruceeckel.com. See Copyright Notice In Copyright.txt.

Class a {interface b {void f ();} public class bimp imports b {public void f () {} b {public void f () {}} public interface c {void f (); Class CimP IMPLEMENTS C {PUBLIC VOID F () {}} private void f ()}} private interface d {}} private interface d {}} private void f ();} private class dimp imports D {/ / WAS mailmented by private class public void f () {}} public class dimp2 imports d {// NOTURE: WAS IMPLEMENTS by public class public void f () {}} public void f () {}} public void fetd ()}} PRIVATE D DREF; Public Void Received (D) {DREF = D; DREF.F ();}}

Interface e {interface g {void f ();} // redundant "public": public interface h {void f ();} void g (); // cannot be private within an interface: //! private interface i { }

public class NestingInterfaces {public class BImp implements AB {public void f () {}} class CImp implements AC {public void f () {}} // Can not implement a private interface except // within that interface's defining class: //! Class DIMP IMPLEMENTS AD {} //!} Class Eimp IMPLEMENTS E {PUBLIC VOID G () {}} Class EGIMP IMPLEMENTS EG {public void f () {}} Class Eimp2 Implements E { Public void g () {} class eg imports eg {public void f () {}}} public static void main (String [] args) {a a = new a (); // can't access ad: // Because D is private ///! ad ad = a.getd (); // doesn't return anything but ad: // !a.dimp2 di2 = A.GETD (); / ** error message ----- ---------- ---------- Compile ---------- NestingInterfaces.java: 83: Incompatible Types Found: Ad Required: A.dimp2 A. DIMP2 DI2 = a.getd (); ^ 1 Error --------------------------------- * / // canNot Access a member of the interface: //! A.Getd (). F (); / ** - Error Messae ------------------- --------- compile ---------- NestingInterfaces.java: 85: f () in ad IS not defined in a public class or interface; cannot be access from outside package a.getd () .f (); ^ 1 Error ------------------------------------- * / / / Only Another a can do anything with getd (): a a2 = new a (); a2.Received ());}} ///: ~ ============ ========================================

=============== Just start I think Privte Interface can only be implemented by Private, but it is obvious that public class Dimp2 also implements Private Interface, but although DIMP2 is public, but he It is impossible to say that he has implemented a private interface, which means that the DIMP2 generated outside the Class D in Inteface D cannot be transformed upwards, - Hand experiment --------------- -------- class PrivateInterface {private interface InterfaceTest {// private void f ();} public class ImplementsTest implements InterfaceTest {public void f () {// override System.out.println ( "Test_1") ()} Public interface,} public void test (interfacetest ab) {ab.f (); system.out.println (ab);} public static void main (String arg []) {PrivateInterface A = new privateinterface (); //privateinterface.InterfaceTest Pi = a.getme (); // Upcasting: You can transform up, in the same class PI.f (); // no problem a.test (a .Getme ()); // no problem}} class mytest {public static void main (string args []) {privateinterface m = new privateInterface (); // m.test (m.getMe ()); // Call f () / ** ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----- ----- because getme () and test () are public, so the objects can be called, and the objects in the Test () method have the right to use the value of the Getme () return value (x), so You can call (x) by TEST (), and invoke f () in the ImplementsTest, [actually: X first transition to InterfaceTest, then call the corresponding override method, here it should be used Later binding in the polymorphology (late-binding) ----------------------------------- ----------------------------- * / / Since InterfaceTest is private, it is not visible.

// More people talk about the upward transformation (see below): privateInterface.InterfaceTest Pi = m.getme (); // errior pi.f (); // error}} -------- Compile ----------PrivateInterface.java:30: PrivateInterface.InterfaceTest has private access in PrivateInterface PrivateInterface.InterfaceTest pi = m.Getme (); ^ PrivateInterface.java: 31: f () in PrivateInterface.InterfaceTest is NOT Defined in a public class or interface; cannot be accessed from outside package pi.f (); ^ 2 Errors output (time consumption 1 second) - Normal termination -------------- ------------------------------ Premier understanding, there may be inaccurate or missing places, continue to know ... ......

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

New Post(0)