Java internal class learning notes, Java class learning notes

Source: Internet
Author: User

Java internal class learning notes, Java class learning notes
20160923

  • Definition: Place the definition of one class inside another class;
  • Create an internal Class Object outside the non-static method of the external class: OutClassName. InnerClassName;
  • The internal class has access to all its external class members;
  • The internal class of the Member cannot contain static modified variables and methods, because the internal class of the member must first create an external class to create its own
  • Generate reference of external Class Object in internal class. You can use OutClassName. this;
 1 public class DoThis { 2     void f(){System.out.println("DoThis.f()");} 3     public class Inner{ 4         public DoThis getOuter(){ 5             return DoThis.this; 6         } 7     } 8     public Inner getInner(){ 9         return new Inner();10     }11     public static void main(String[] args) {12         DoThis dThis = new DoThis();13         DoThis.Inner dThisInner = dThis.getInner();14         dThisInner.getOuter().f();15     }16 }
  • To create an internal class object, you must use the reference of the external class object;
1 public class DoNew {2     public class Inner{};3     public static void main(String[] args) {4         DoNew doNew = new DoNew();5         DoNew.Inner dInner = doNew.new Inner();6     }7 }
  • Create a nested class (static internal class) without reference from external class objects;
  • Private-modified internal classes can only be accessed within their external classes; protected-modified internal classes, only its external class, its external class subclass, and other classes in the same package of its external class can be accessed;
1 class Parcel4 {2 private class PContents implements Contents {3 private int I = 11; 4 @ Override 5 public int value () {return I ;} 6} 7 protected class PDestination implements Destination {8 private String label; 9 private PDestination (String whereTo) {label = whereTo;} 10 @ Override11 public String readLabel () {return label ;} 12} 13 public Destination destination (String s) {return new PDestination (s);} 14 public Contents contents () {return new PContents ();} 15 Parcel4.PContents t; // PContents is private and can only be accessed within Parcel4 16} 17 public class TestParcel {18 public static void main (String [] args) {19 Parcel4 p = new Parcel4 (); 20 Contents contents = p. contents (); 21 Destination destination = p. destination ("Tasmania"); 22 // Parcel4.PContents pc = p. new PContents (); // PContents is private and can only be accessed within Parcel4. Here error 23} 24}
  • Complex internal classes: Define internal classes in methods or scopes for the following reasons:
  • Example

1. A class defined in a method

 1 public class Parcel5 { 2     public Destination destination(String s){ 3         class PDestination implements Destination{ 4             private String label; 5             private PDestination(String whereTo){ 6                 label = whereTo; 7             } 8             @Override 9             public String readLabel() { return label; }10         }11         return new PDestination(s);12     }13     public static void main(String[] args) {14         Parcel5 p = new Parcel5();15         Destination destination = p.destination("Tasmania");16     }17 }

2. A class defined in the scope is inside the method.

 1 public class Parcel6 { 2     private void internalTracking(boolean b) { 3         if (b){ 4             class TrackingSlip { 5                 private String id; 6                 public TrackingSlip(String s) { 7                     id = s; 8                 } 9                 String getSlip(){ return id;}10             }11             TrackingSlip ts = new TrackingSlip("slip");12             String s = ts.getSlip();13         }14         //Can't use it here!Out of scope:15         //TrackingSlip ts = new TrackingSlip("x");16     }17     public void track(){internalTracking(true);}18     public static void main(String[] args) {19         Parcel6 p = new Parcel6();20         p.track();21     }22 }

3. An anonymous class that implements the interface

4. An anonymous class extends the class with non-default constructor

5. perform field initialization for an anonymous class.

6. Construct an anonymous class through instance initialization (the Anonymous class has no constructor)

  • The parameter passed to the anonymous internal class and used in the anonymous internal class, which must be defined as final;
  • Anonymous internal class extensible class can also implement interfaces, but cannot at the same time; If an interface is implemented, only one interface can be implemented;
  • On page 7 of Java programming ideology, 10.6.1 re-visited the factory method and used anonymous internal class examples;
  • Nested class: static modified internal class, unable to access non-static external class objects
  • Nested classes can be part of interfaces and even implement external interfaces.
  •  1 public interface ClassInInterface { 2     void howdy(); 3     class Test implements ClassInInterface{ 4         @Override 5         public void howdy() { System.out.println("Howdy"); } 6         public static void main(String[] args){ 7             new Test().howdy(); 8         } 9     }10 }
  • The internal class can be nested at multiple layers, and can transparently access all the members of its embedded external class.
  •  1 class A{ 2     private void f(){} 3     class B { 4         private void g(){} 5         public class C { 6             void h() { 7                 g(); 8                 f(); 9             }10         }11     }12 }13 public class MultiNestingAccess {14     public static void main(String[] args) {15         A a = new A();16         A.B ab = a.new B();17         A.B.C abc =ab.new C();18         abc.h();19     }20 }
  • Why Internal classes?
    • The 204 page of Java programming ideology explains that each internal class can inherit one (Interface) implementation independently, so no matter whether the peripheral class has inherited an (Interface) implementation, no impact on internal classes;
    • In short, internal classes implement interfaces and inherit a class, which is much less concerned than external class implementation interfaces. External class implementation interfaces need to be fully considered and whether there is any impact elsewhere;
    • Internal classes can inherit multiple concrete classes or abstract classes, and work with interfaces to make the solution of "Multi-inheritance" perfect;
    • Internal classes can have multiple instances, each of which has its own status information and is independent of external class objects;
    • In a single external class, multiple internal classes can implement the same interface in different ways, or inherit the same class;
    • The creation of internal class objects does not depend on the creation of external class objects.
    • The internal class has no "is-a" relationship, and the internal class is an independent entity.
  • The closure records some information about the scope of the closure, so that the closure can call its external scope data; the internal class is the object-oriented closure;
  • Callback,
  • Examples of closure functions provided by internal classes:
  •  1 package com.helei.innerclasses; 2 interface Incrementable { 3     void increment(); 4 } 5 class Callee1 implements Incrementable { 6     private int i = 0; 7     @Override 8     public void increment() { 9         i++;10         System.out.println(i);;11     }12 }13 class MyIncrement {14     public void increment() { System.out.println("Other operation");}15     static void f(MyIncrement mi) {mi.increment();}16 }17 class Callee2 extends MyIncrement {18     private int i = 0;19     public void increment() {20         super.increment();21         i++;22         System.out.println(i);23     }24     private class Closure implements Incrementable {25         public void increment() {26             Callee2.this.increment();27         }28     }29     Incrementable getCallbackReference() {30         return new Closure();31     }32 }33 class Caller {34     private Incrementable callbackReference;35     Caller(Incrementable cbh){callbackReference = cbh;}36     void go() {callbackReference.increment();}37 }38 public class Callbacks {39     public static void main(String[] args) {40         Callee1 c1 = new Callee1();41         Callee2 c2 = new Callee2();42         MyIncrement.f(c2);43         Caller caller1 = new Caller(c1);44         Caller caller2 = new Caller(c2.getCallbackReference());45         caller1.go();46         caller1.go();47         caller2.go();48         caller2.go();49     }50 }
  • After reading the above process several times, it is best to repeat it and debug it again.
  • Internal class and control framework
  • Internal class inheritance
  • Internal class overwrite, invalid; can inherit an internal class explicitly;
  • The difference between a local internal class and an anonymous internal class

 

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.