In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the internal classes of Java". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Internal class, computer object-oriented programming concept. In object programming, you can define another class inside one class, which is called nested classes, which has two types, static nested class and non-static nested class. Static nested classes are rarely used, and the most important are non-static nested classes, which are called inner classes (inner). Inner classes are the main add-on to the JAVA language. An inner class can be almost anywhere within a class, at the same level as an instance variable, within a method, or even part of an expression.
1. Ordinary inner class
The simplest example of a normal inner class:
/ / external class class OutterClass {/ / inner class public class InnerClass {private int i = 0; public int getInt () {return I;}} public void proc () {InnerClass inClass = new InnerClass (); System.out.println (inClass.getInt ()) }} public class Main {public static void main (String [] args) {OutterClass outClass = new OutterClass (); outClass.proc ();}}
The outer class can access the private members of the inner class, and the inner class can access the private members of the outer class:
/ / external class class OutterClass {private int mOut = 10; / inner class public class InnerClass {private int mIn = 0; public void printOutPrivate () {/ / directly print the member System.out.println (mOut) of the external class }} public void printInPrivate () {InnerClass inClass = new InnerClass (); / / directly print the private member of the inner class System.out.println (inClass.mIn);} public void printOutPrivate () {InnerClass inClass = new InnerClass (); inClass.printOutPrivate () }} public class Main {public static void main (String [] args) {OutterClass outClass = new OutterClass (); outClass.printInPrivate (); outClass.printOutPrivate ();}}
If the member variable of the outer class has the same name as the member variable of the inner class, you can use "OutClass.this.mem" to distinguish when the inner class wants to access that member of the outer class:
/ / external class class OutterClass {private int mMem = 10; / inner class public class InnerClass {private int mMem = 0; public void printOutPrivate () {/ / directly print the member System.out.println (OutterClass.this.mMem) of the external class;}
To create a normal inner class, you must first create a corresponding external class:
/ / external class class OutterClass {private int mMem = 10; / inner class public class InnerClass {private int mMem = 0; public void printOutPrivate () {/ / directly print the member System.out.println (OutterClass.this.mMem) of the external class } public class Main {public static void main (String [] args) {OutterClass outClass = new OutterClass (); OutterClass.InnerClass inClass = outClass.new InnerClass (); inClass.printOutPrivate ();}}
You can also use the following ways:
/ / external class class OutterClass {private int mMem = 10; / inner class public class InnerClass {private int mMem = 0; public void printOutPrivate () {/ / directly print the member System.out.println (OutterClass.this.mMem) of the external class }} public InnerClass newInnerClass () {return newInnerClass ();}} public class Main {public static void main (String [] args) {OutterClass outClass = new OutterClass (); OutterClass.InnerClass inClass = outClass.newInnerClass (); inClass.printOutPrivate ();}}
two。 Static inner class
The ordinary inner class is preceded by the static modifier to become the static inner class. The static inner class is similar to C++ 's nested class, with the following differences from the ordinary inner class:
The static inner class has no reference to the external class, which is more like a namespace for it.
A normal inner class cannot have static members, static methods, or another static inner class; static inner classes can have all of this.
Static inner classes can be created directly, without having to create external classes first:
/ / external class class OutterClass {private int mMem = 0; / / static inner class static public class InnerClass {private int mMem = 0; public void printOutPrivate () {/ / this is wrong / / System.out.println (OutterClass.this.mMem) }} public void printInPrivate () {InnerClass inClass = new InnerClass (); / / you can directly access the member System.out.println (inClass.mMem) of the static inner class }} public class Main {public static void main (String [] args) {/ / directly create static inner class OutterClass.InnerClass inClass = new OutterClass.InnerClass (); inClass.printOutPrivate ();}}
As can be seen from the above description, the static inner class is not much different from the general class, except that it is placed in a class, which is equivalent to its namespace and can prevent naming conflicts.
3. Local inner class
Java can define a class in a method, or even a {} block, whose scope is in this block:
/ / external class class OutterClass {public void testLocalInner () {if (true) {class LocalInner {public void proc () {System.out.println ("hello") }} / / can be created using LocalInner localInner = new LocalInner (); localInner.proc ();} / / error: beyond the scope of the class definition LocalInner localInner = new LocalInner (); localInner.proc () }}
The general purpose of a local inner class is to implement an interface and to be used as an outgoing method of that interface:
/ / Interface interface Talker {public void Talk ();} / / external class class OutterClass {public Talker getTalker () {/ / the local inner class class SomeTalker implements Talker {public void Talk () {System.out.println ("hello") of the interface }} / / create a class instance and return SomeTalker talker = new SomeTalker () as Talker; return talker;}} public class Main {public static void main (String [] args) {OutterClass outClass = new OutterClass (); Talker talker = outClass.getTalker () Talker.Talk ();}}
4. Anonymous inner class
The syntax for anonymous inner classes is as follows:
New InterfaceName () {.}; or new SuperclassName () {.}
It is considered to be the implementer of InterfaceName or the inherited class of SuperclassName. Anonymous inner classes do not build functions. If SuperclassName has a constructor with parameters, you must take these parameters with them when creating anonymous inner classes. Here are the most common uses of anonymous inner classes:
/ / Interface interface Talker {public void Talk ();} / external class class OutterClass {public void Talk (Talker talker) {talker.Talk ();}} public class Main {public static void main (String [] args) {OutterClass outClass = new OutterClass () / / directly generate an anonymous inner class outClass.Talk (new Talker () {public void Talk () {System.out.println ("hello");}});}} "what are the inner classes of Java"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.