Extra-detailed summary of Java internal classes (with code samples)
What is an internal class
What is an internal class?
As the name implies, the definition of one class is placed inside another class.
The concept is clear, it feels simple, but the key lies in where this internal class is placed, which can be the scope of a class, a method, or a code block.
Understanding the concepts is only the first step, and understanding the details will lead to a thorough understanding of Java's internal class characteristics.
For example, this is the most common internal class:
public class Product1 {
class Design{ private String name = "P30 pro"; public String showName() { return name; } } class Content{ private int i; Content(int value){ i = value; } int value() {return i;} } public void show(int value) { Content c = new Content(value); Design d = new Design(); System.out.println(d.showName()); System.out.println(c.value()); } public static void main(String[] args) { Product1 p = new Product1(); p.show(6000); }
}
Explain:
The example above shows the most basic use of an internal class, which places the definition of one or more classes inside the periphery.You can see that the use of the show() method is the same as that of a normal class.
Additionally, internal class objects cannot be created anywhere other than the nonstatic methods of the perimeter class by directly creating new internal classes.Compilation errors will be reported.
Like this:
Insert a picture description here
And so on:
Insert a picture description here
If you want to create an internal class object outside of a non-static method of a peripheral class.What shall I do?
The correct position is like this:
Add two common methods to the outer class to return a reference to the inner class.
public Design design() { return new Design(); } public Content content(int value) { return new Content(value); }
The internal class object is then obtained by calling the method from the external class object.
public static void main(String[] args) { Product2 p = new Product2(); p.show(6000); Product2.Content c1 = p.content(100); Product2.Design d1 = p.design(); }
It is worth noting that classes other than external classes do not have direct access to the internal class objects of the external class.
Compilation errors will be reported.This also conforms to the definition of internal class, which serves the periphery class.
Attributes of internal classes
1. Can link to external classes
When an internal class object is generated, there is a connection between the object and the peripheral object that makes it so that it can access all the members of the peripheral object without any special conditions.
The following is an example from Java Programming Ideas
Define a Selector interface first
public interface Selector {
boolean end(); Object current(); void next();
}
Then define a Sequence class, which defines an internal class with a private.
public class Sequence {
private Object[] items; private int next = 0; public Sequence(int size) {items = new Object[size];} public void add(Object x) { if(next < items.length) { items[next++] = x; } } private class SequenceSelector implements Selector{ private int i = 0; public boolean end() { return i == items.length; } public Object current() { return items[i]; } public void next() { if(i < items.length) {i++;} } } public Selector selector() { return new SequenceSelector(); } public static void main(String[] args) { Sequence sequence = new Sequence(10); for(int i = 0; i < 10; i++) { sequence.add(Integer.toString(i)); } Selector selector = sequence.selector(); while(!selector.end()) { System.out.print(selector.current() + " "); selector.next(); } }
}
Explain:
You can see that SequenceSelector is an internal class whose end(), current(), and next() all use the private items field in the peripheral class.
2. Use.this and.new
Usage of.this
If you need to generate a reference to an external class object, you can use the name of the external class followed by a dot and this.
As follows:
public class DotThis {
void name() {System.out.println("name");} public class Inner{ public DotThis outer() { return DotThis.this; } } public Inner inner() {return new Inner();} public static void main(String[] args) { DotThis dt = new DotThis(); DotThis.Inner inner = dt.inner(); inner.outer().name(); }
}
It is important to note that DotThis.this only produces the correct external class reference.External class object was not created.
Usage of.new
Internal class objects can be created through this syntax, but it is important to note that external class objects are used to create them.
public class DotNew {
public class Inner{} public static void main(String[] args) { DotNew dn = new DotNew(); DotNew.Inner dnInner = dn.new Inner(); }
}
Internal Class Classification
1. Local Internal Classes
Define a class within a method or within a scope of a method.It is called a local internal class.
public class Product3 {
public Section section(String inputName) { class ASection implements Section{ private String name; private ASection(String name) { this.name = name; } @Override public String hello() { return name + " say hello"; } } return new ASection(inputName); } public static void main(String[] args) { Product3 p = new Product3(); Section section = p.section("aaaaa"); System.out.println(section.hello()); }
}
ASection is an implementation of the Section interface, which has the following code:
public interface Section {
String hello();
}
Explain:
The internal class cannot access the ASection class outside of the section() method;
Access modifiers (public, private, protected) are not allowed in method internal classes;
Note that the method returns a reference to Section, that is, an upward transition.
You can also place the definition of an internal class within a block of statements in a method, such as if.
public class Product4 {
public String check(boolean flag) { String checkId = null; if(flag) { class DetailCheck{ private String id; private DetailCheck(String id) { this.id = id; } String getId() { return id; } } DetailCheck dc = new DetailCheck("1111"); checkId = dc.getId(); } return checkId; } public static void main(String[] args) { Product4 p = new Product4(); System.out.println(p.check(true)); }
}
Explain:
The DetailCheck internal class is in the if statement block, so its scope is also within the if statement block.Beyond this range is not available.For example, errors will be compiled:
Insert a picture description here
2. Anonymous Internal Class
Anonymous inner classes are actually special method inner classes (local inner classes).It is unique in that it combines the definition of an internal class with the creation of its objects.The declared internal class name is not displayed by the class keyword, so it is called "anonymous".
Look at the code example:
public class Product5 {
public Section section() { return new Section() { private String name = "hayli"; @Override public String hello() { // TODO Auto-generated method stub return name + " haha"; } }; } public static void main(String[] args) { Product5 p = new Product5(); p.section(); }
}
Explain:
Section can be an interface or a base class here.
3. Nested Classes (Static Internal Classes)
Internal classes decorated with the static keyword are called static internal classes and are also called nested classes.
The biggest difference between nested and regular inner classes is:
A common internal class object implicitly saves a reference to the external class object that created it.Nested classes create objects without external class objects.
Non-static external class objects cannot be accessed from objects of nested classes.
Normal internal classes cannot have static data and static fields, nor can they contain nested classes, but nested classes can contain all these things.
public class Product6 {
private static int id = 100; private static class BSection implements Section{ private String name = "bbbb"; @Override public String hello() { return name + " hello"; } // Only static data or fields of external classes can be accessed public int getId() {return id;} // Can contain static data or methods static int x = 200; public static void test1() {} // You can nest one more layer static class BInner{ private String name; static void test1() {System.out.println("inner ===");} } } public static void main(String[] args) { Section section = new BSection(); section.hello(); }
summary
This article describes what internal classes are, how they are most commonly defined, and several specific types of Java internal classes.Although there are no opportunities to use internal classes in your work, knowing the most basic knowledge, really encountering internal class writing in your project, can also understand what is going on.
Original Address https://www.cnblogs.com/happyone/p/11306419.html