当前位置:网站首页>Internal class usage scenarios, syntax and principle explanations
Internal class usage scenarios, syntax and principle explanations
2022-06-30 01:18:00 【Shocking mud】
Inner class
1 . The role of inner classes
- encapsulation ------- Access control : Common class access control characters can only be controlled up to the... Under the same package , But internal classes can be private and only external classes are allowed to access .
- Multiple inheritance -------- One of the solutions : The second is the bridging mode
- Anonymous inner classes make the code simple : lambda expression , Monitor mode , etc.
2 . Classification of internal classes
- Member inner class
- Static inner class
- Local inner classes
- Anonymous inner class ( Special local inner classes )
3 . Syntax of inner class
- Member inner class
- Member inner classes are not allowed to have static properties and methods ;
- The inner class level of a member holds a reference to an outer class object , So the initialization of the inner class must show the initialization of the outer class (
new OuterClass().new InnerClass()) ; - A member inner class can access any member property of an outer class, including private properties . ( Static properties cannot be accessed , There is no need to say more );
- The internal class and external class variables have the same name , need OuterClass.this.name Refers to an external class ,this.name Refers to an internal class ;
ps: As for why you can access private properties of external classes , Because the compiler did something .javac The compiler generates... For external classes access Channel method . amount to get set Method to access private properties .
public class OuterClass {
private static int source = 100;
private String name = "OuterClass";
private long birthday = 1844548484148L;
// Member inner class
public static void main(String[] args){
// Initializing a local inner class must show initializing an outer class , Only in this way will there be no problem for the inner class to access the properties of the outer class .
InnerClass innerClass = new OuterClass().new InnerClass();
innerClass.fun();
}
public class InnerClass{
private String name = "InnerClass";
private long birthday = 000000L;
// Member inner classes cannot have static properties and methods
public void fun(){
System.out.println(" External class name"+ OuterClass.this.name);
System.out.println(" Inner class name"+ this.name);
}
}
}
- Static inner class
- Static inner classes can have static state / The static attribute / Method ; Is a complete class ;
- Static inner classes do not hold references to outer class instances , Instantiation does not need to instantiate an external class first ; As for the loading of external classes when accessing the static properties of external classes, it is jvm Accomplished ;
- Static inner classes only allow access to any static domain of the outer class ( Including private properties ), Access to non static domains is not allowed , Because there is no reference to hold access to ;
ps: in summary The static inner class is a complete class , Just borrowed the shell of the external class , Other classes are no different from ordinary classes .
public class OuterClass {
private static int source = 100;
private String name = "OuterClass";
private long birthday = 1844548484148L;
// Static inner class
public static class StaticInnerClass{
private String name;
private long birthday;
private static int source = 20;
// The static inner class can only access the static domain of the outer class .
public void fun(){
System.out.println(" External class static properties "+ OuterClass.source);
System.out.println(" Internal class static properties "+ source);
}
}
}
- Local inner classes
- The inner class defined in the method , Scope is only in methods ;
- A local inner class can access only the non static domain of the outer class just like a member inner class . But you can access local variables ;
- If the local inner class wants to access local variables , Then the local variable must be represented by final modification ; Because local variables are allocated on the stack , The internal class objects are allocated on the heap , The life cycle of an inner class object will be longer than that of a local variable ; When the method ends, the stack frame pops up , Local variables will be destroyed . At this point, if the internal class object accesses the local variable again, there will be a problem ; JVM The implementation of Within an internal class object Save a copy of the local variable ; But the extra , But it brings the performance overhead of maintaining the consistency of local variables and their replica data . Therefore, changing the value of a local variable in a local inner class is simply not allowed . There is a Local variables accessed in local inner classes must be declared as final This rule .
Here is an example
public class OuterClass {
private static int source = 100;
private String name = "OuterClass";
private long birthday = 1844548484148L;
public void fun(){
final int a = 0;
// Local inner classes
class Runnable implements java.lang.Runnable {
@Override
public void run() {
birthday ++; // A local inner class can also access the member domain of an outer class
int b = a;
b ++;
}
}
new Thread (new Runnable()).start();
}
}
- Anonymous inner class
- A local inner class without a name , Can directly new An abstract class or interface , Just in new To implement its abstract methods ;
- Anonymous inner class The local variables to be accessed must also be final modification ( Because its essence is a local inner class )
- lambda Expressions are syntax sugar for anonymous inner classes , So the local variables to be accessed must also be final modification
public class OuterClass {
private static int source = 100;
private String name = "OuterClass";
private long birthday = 1844548484148L;
public void fun(){
final int a = 0;
new Thread (()-> {
birthday ++; // A local inner class can also access the member domain of an outer class
int b = a;
b ++;
}).start();
}
}
At the end of the day , Inner classes are a compiler phenomenon ,JVM The virtual machine doesn't know the difference between internal classes and regular classes . As for this point, friends who do not understand can
Reference resources Blog
边栏推荐
- RubyMine开发工具,重构和意图操作
- 2022-06-29:x = { a, b, c, d }, y = { e, f, g, h }, x、y两个小数组长度都是4。 如果有: a + e = b + f = c + g = d + h
- Twitter launches the test of anti abuse tool "safe mode" and adds enabling prompt
- Too voluminous ~ eight part essay, the strongest king of interview!
- Resizekit2.net size and resolution independent
- HC32M0+ GPIO
- Online text digit recognition list summation tool
- Seata and the three platforms are working together in the summer of programming. Millions of bonuses are waiting for you
- 【three.js】WEB3D初次体验
- 快手伸手“供给侧”,找到直播电商的“源头活水”?
猜你喜欢

Preliminary understanding of NVIDIA Jetson nano

Statsmodels notes STL
![[mrctf2020]ezpop-1 | PHP serialization](/img/f8/6164b4123e0d1f3b90980ebb7b4097.png)
[mrctf2020]ezpop-1 | PHP serialization

Application of pointer in STC89C52 single chip microcomputer and demonstration of practical cases

Analysis of natural frequency and buckling load of cylinder by finite element method

2022-06-29:x = { a, b, c, d }, y = { e, f, g, h }, x、y两个小数组长度都是4。 如果有: a + e = b + f = c + g = d + h

如何统一项目中包管理器的使用?

Solving plane stress problem with MATLAB

R语言线性回归模型拟合诊断异常值分析家庭燃气消耗量和卡路里实例带自测题

Seata and the three platforms are working together in the summer of programming. Millions of bonuses are waiting for you
随机推荐
Cantilever beam calculation [matlab code]
快手伸手“供给侧”,找到直播电商的“源头活水”?
In depth analysis of a large number of clos on the server_ The root of wait
How does webapi relate to the database of MS SQL?
Exercise "product": self made colorful Prompt string display tool (for loop and if condition judgment)
[spark] basic Scala operations (continuous update)
Stimulus reports reporting tool, stimulus creates and builds reports
Quality management of functional modules of MES management system
[535. encryption and decryption of tinyurl]
Is the numpy index the same as the image index?
How to create a module in the idea and how to delete a module in the idea?
postman 之接口关联
MySQL function
How did the data center change from "Britney Spears" to "Mrs. bull"?
Resizekit2.net size and resolution independent
存储引擎分析
HC32M0+ GPIO
VIM编辑器常用指令
Transaction summary on June 25, 2022
How to view the size of all files in a folder?