Java Note 8__ Inner class/list implementation/wrapper class, enjoy meta design pattern/package, access modifier

Source: Internet
Author: User
Tags true true

/*** Inner class: Class defined within a class * 1. Class outer{* Class inner{} *} * 2. Class outer{//Method inner classes * public void Dosomethin      G () {* Class Inner () {} *} *} * 2*:1. The inner class of the method can only be instantiated in methods that define the inner class * 2. Method Inner class object cannot use the non-final local variable of the method in which the inner class is located * (Cause: A local variable disappears as the end of a method call, and the scope of a new inner class object is the one that can be outside the method) * 3. Class outer{//Static inner class: You can access it without an external class object, and only access static members and methods of the outer class * static Class inner{} *} * outer.inner n = new Ou ter.  Inner (); */ Public classMain { Public Static voidMain (string[] args) {Dog G1=NewDog ();//generating an external class instanceDog.childdog cg1 = G1.NewChilddog ();//instantiate an inner class object, which is typically not usedG1.say (); G1.        Childsay ();    Cg1.say (); }}classdog{//will generate two class files    PrivateString name = "KWS";  Public voidsay () {System.out.println ("I ' m a Dog, My name is" +name); }    classchilddog{//Inner Class (member inner Class)         Public voidsay () {System.out.println ("I ' m a childdog, My mom's name is" +name); }    }     Public voidChildsay () {Childdog cg1=NewChilddog ();    Cg1.say (); }}

/*** Anonymous Inner class: There are three kinds of * 1. The inner class without a name. * 2. Interface * 3. Parametric * * Use anonymous inner class several principles: * 1. There can be no construction method, only one instance * 2. You cannot define any static member, static method * 3. Cannot be public, Protected, Private, static * 4. Must be in the back of new, with its implicit implementation of an interface or implementation of a class * 5. The anonymous inner class is local, so all restrictions on the local class take effect on it * * Inner class: * Each inner class can inherit from one independently (interface ) is implemented, so no matter whether an external class has inherited an implementation of (an interface), the * has no effect on the inner class. Some design and programming problems * are difficult to solve without the ability of an inner class to inherit multiple concrete or abstract classes. From this point of view, the inner class makes the solution of multiple inheritance complete. The interface solves some of the problems, while the inner classes effectively * implement multiple inheritance. "Java can only be single-inheritance"*/ Public classMain { Public Static voidMain (string[] args) {Dog D1=NewDog () {//1. Inherited anonymous inner class             Public voidsay () {System.out.println ("I'm an anonymous dog.");        }        };                D1.say (); Child D2=NewChild () {//2. Interface anonymous Inner class             Public voidTalk () {System.out.println ("I'm an inherited dog.");        }        };                D2.talk (); Childtalk (NewChild () {//3. Parametric anonymous inner class             Public voidTalk () {System.out.println ("I'm a parametric dog.");    }        }); }     Public Static voidChildtalk (child c) {C.talk (); }}classdog{protectedString name;    Dog () {}; Dog (String name) { This. Name =name; }     Public voidsay () {System.out.println ("I ' m a Dog, My name is" +name); }}classMaledogextendsdog{ PublicMaledog (String name) {Super(name); }     Public voidsay () {System.out.println ("I ' m a Maledog, My name is" +name); }}Interfacechild{ Public voidTalk ();}

/*** The implementation of the linked list*/ Public classMain { Public Static voidMain (string[] args) {NodeManager nm=NewNodeManager ();        Nm.printnode (); Nm.addnode ("Node1"); Nm.addnode ("Node2"); Nm.addnode ("Node3"); Nm.addnode ("Node4"); Nm.addnode ("Node5");        Nm.printnode (); Nm.delnode ("Node3");        Nm.printnode (); Nm.delnode ("Node1");    Nm.printnode (); }}classnodemanager{PrivateNode Root;//root node     Public voidAddNode (String name) {if(root==NULL) {root=NewNode (name); }Else{root.add (name); }    }     Public voidDelnode (String name) {if(root!=NULL){            if(root.name.equals (name)) {root=Root.next; }Else{Root.del (name); }        }    }     Public voidPrintnode () {if(root!=NULL) {System.out.print (Root.name+ "--");        Root.print (); }Else{System.out.println ("NULL"); }    }    classnode{PrivateString name; PrivateNode Next;  PublicNode (String name) { This. Name =name;}  Public voidAdd (String name) {if( This. next==NULL){                 This. Next =NewNode (name); }Else{                 This. Next.add (name); }        }         Public voiddel (String name) {if( This. next!=NULL){                if( This. Next.name.equals (name)) {                     This. Next = This. Next.next; }Else{                     This. Next.del (name); }            }        }         Public voidprint () {if( This. next!=NULL) {System.out.print ( This. next.name+ "--");  This. Next.print (); }Else{System.out.println ("NULL"); }        }    }}

/*** Eight types of basic data type wrapper class * Basic type wrapper class parent class * 1.int Integer (number) * 2.char Character (Object) *       3.float Float (number) * 4.double double (number) * 5.boolean Boolean (Object) * 6.byte    Byte (number) * 7.short short (number) * 8.long long (number) * Basic data Type--wrapper class : Boxing Operation * Wrapper class--basic data type: Unpacking Operation*/ Public classMain { Public Static voidMain (string[] args) {Long x1=NewLong (100);//Manual BoxingLong x2 = 100L;//Automatic Boxing        Longx3 = x2;//Automatic Unpacking        Longx4 = X1.longvalue ();//Manual UnpackingString S1= "123456"; intD1 =Integer.parseint (S1); Integer D2=integer.valueof (S1); System.out.println (D1+" "+D2); Integer W1=NewInteger (100); Integer W2=NewInteger (100); SYSTEM.OUT.PRINTLN (W1==W2);//falseInteger W3 =-128; Integer W4=-128; System.out.println (W3==W4);//true//when an integer is directly assigned, the same object ( -128~127) is used when the value is within 1 bytes//Byte Short Long Integer element design mode    }        /*** Enjoy meta mode: Use shared objects to minimize memory usage and share information to as many similar objects as possible; * It applies to a large number of objects that are simply duplicated and thus result in an unacceptable use of large amounts of memory. * Some of the state of the object is usually shared.     It is common practice to put them in an external data structure and pass them on to the element when it is needed. * #运用共享技术有效的支持大量细粒度的对象 "like bullets in the game"*/}

/** * Package: For the management of multiple Java source files, just like our file directory * Defines a package: The package com.vince  "can only appear in the first sentence of the code" * Runtime must specify the packet name   example: Java com.vince.main< c5/> or   java com/vince/main *  * Access modifier * Scope: Same       class     same package     different Bun class     different package non-subclass * Public          true       True True True           * protected       true       true           false   * Default            true       True False False          * Private         true          false */

Java Note 8__ Inner class/list implementation/wrapper class, enjoy meta design pattern/package, access modifier

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.