Java Internal classes

Source: Internet
Author: User

1. Static Nesting Classes

 PackageCom.wangfeng;Importjava.util.ArrayList; Public classOuter { Public Static classinner{PrivateString name;  PublicInner (String name) { This. Name =name; }         PublicString GetName () {returnname; }         Public voidsetName (String name) { This. Name =name; }            }         Public BooleanAddMembers (Inner member) { This. Members.add (member); return true; }         Public voidprintmembers () { for(Inner Member: This. Members)        {System.out.println (Member.getname ()); }    }        PrivateArraylist<inner> Members =NewArraylist<>();  PublicArraylist<inner>getMembers () {returnMembers ; }     Public voidSetmembers (arraylist<inner>Members ) {         This. Members =Members ; }}

The main thing is that the inner class has static in front

If such a class is externally visible, it can be initialized in the following way:

Outer.Inner in = new Outer.Inner ("wangfeng2");

2. Inner class

Import Java.util.arraylist;public class Outer {public class inner{private string Name;public Inner (string name) { THIS.name = name;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}} public Boolean addmembers (Inner member) {This.members.add (member); return true;} public void Printmembers () {for (Inner member:this.members) {System.out.println (Member.getname ());}} Private arraylist<inner> members = new arraylist<> ();p ublic arraylist<inner> getMembers () {return Members;} public void Setmembers (Arraylist<inner> members) {this.members = members;}}

 

The main thing is that the inner class has no static

If such a class is externally visible, it can be initialized in the following way:

Outer.Inner in = out . New Inner ("wangfeng2");

3. Local inner class (that is, the class defined in the method)

Public interface People {string getName (); void SetName (string name);}

  

public class Hello {public static void main (string[] args) {people p = peoplefactory ();p. SetName ("ADFASD"); System.out.println (P.getname ());} public static People Peoplefactory () {class man implements people{string Name;public String GetName () {return name;} public void SetName (String name) {this.name = name;}} return new Man ();}}

 

4. Anonymous inner class

public class Hello {public static void main (string[] args) {people p = peoplefactoryanonymous ();p. SetName ("ADFASD"); System.out.println (P.getname ());} public static People Peoplefactory () {class man implements people{string Name;public String GetName () {return name;} public void SetName (String name) {this.name = name;}} return new Man (); public static People peoplefactoryanonymous () {return new people () {string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;}};}}

  

 

Java Internal classes

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.