当前位置:网站首页>Internal class -- just read this article~
Internal class -- just read this article~
2022-07-27 07:16:00 【mmmenxj】
1.try( Statement AutoCloseable Interface object , Automatically called close){
// Write the code that may be abnormal here
// If something really goes wrong , Default by JVM Construct an exception object , Pass it to the program , from catech Catch exception object
}catch(FileNotFoundException e){}
}catch(IOEException e){}// I / O stream
}catch(Exception e){}// Null pointer exception
Assume that the file does not exist at this time ,
When JVM When something unusual happens .JVM Only one exception object will be created ,catch When capturing, you will choose the one closest to the exception object type catch Segment capture .
finally{ Code block that will be executed regardless of whether there is an exception , Release resources , Such as file closing, etc }
Inspection exemption exception ( No checked exceptions ): Exceptions that do not require forced exception handling
Error and RuntimeException And subclasses are exempt from inspection , Null pointer , Type conversion , An array
Must check exception ( By checked exception ):
Except for the exception of exemption from inspection, all exceptions are mandatory ,IOEException
throw: Methods artificially generate abnormal objects , It is equivalent to that there is an exception here , If it is necessary to detect abnormal , Here we will show the processing
throws: Used on method declarations , Clearly indicate the exceptions that this method may produce , But in this method, we do not deal with
Inner class
1. Member inner class : Directly defined in a class , No addition static Defined inner classes , Member method or property
The inner class of a member must depend on the existence of the outer class , You must have an external class object before you can generate member internal classes .
Generation of class objects within members
1> The inner part of the outer class : Grammar is just like ordinary classes
Inner in = new Inner();
2> The outer part of the outer class : The premise is that the inner class is visible to the outer class public
Outter.Inner in = new Outter().new Inner();
ps:new Outter An external class object is generated , Then use this object to construct the inner class object , When generating internal class objects , The external object that constructs this inner class will be passed into the inner class by the compiler .
2. Internal and external classes can easily access each other's private domains
The inner class accesses the private domain of the outer class directly ( An object of an external class is hidden in the inner class )
public class Outter {
private String msg = "outter Class msg attribute ";
// Completely hidden from the outside , Just use it inside the class
class Inner{
private int num = 10;
private String msg = " In the inner class msg attribute ";
public void test(){
// Direct access to external classes msg attribute
// Here you directly access the private member variables of the external class msg, Member variables must be accessed through objects
System.out.println(msg);
System.out.println(Outter.this.msg);// Passed in by external class msg
System.out.println(Outter.this);
}
}
public void fun(){
Inner inner = new Inner();
// Access the private properties of the inner class through the inner class object
inner.test();
}
public static void main(String[] args) {
Outter outter = new Outter();
outter.fun();
}
}
The operation results are as follows :

The external class needs to access the private domain of the internal class , Don't want to access through objects .
Member methods can access static domains , Static variables cannot be defined
For member inner classes , Can you define a static variable ?
You can't , The inner class of a member must depend on the outer class , If a member's inner class has static attributes , No external class objects can be accessed .

For external classes , Can member inner classes be used in static methods of outer classes ?== It is equivalent to calling member variables in static methods Definitely not !

2. Static inner class
Defined in class , Use static The decorated inner class is the static inner class
Static inner classes do not need to rely on external class objects
The inner part of the outer class :

Why can both member methods and static methods create objects of inner classes of members ?
Static variables in analogy classes , Objects without classes can be used . Static methods of classes can call , The member method is more acceptable .
Outside the inner class :
External class . Inner class quote = new External class . Inner class ();
At this time, there is no need to generate external class objects
A static inner class is just an ordinary class , It's just inside a class .
summary :
The member inner class can access the member domain and static domain of the outer class , But you can't have a static domain
Static inner classes can have member domains , But you cannot directly access the member domain of an external class , Can pass new An external class object to access , Static domain random access .
3. Method inner class ( Local inner classes )-- Analogy to local variables
Classes defined directly inside methods , No access modifiers are allowed , Completely hidden from the outside ( Out of this class, it's gone )
1> You cannot use any access modifiers
2> Out-of-service static modification
3> If you want to use the parameters of the method , It is used by default. fianl Modify the , So you can't change the size
4. Anonymous inner class (lambda The predecessor of expression , Functional programming )99% Used in method parameter transfer process
Anonymous inner classes are special versions of method inner classes , Do not write the class name directly .
Anonymous inner classes comply with all requirements of method inner classes , Anonymous inner classes inherit a class by default ( Both ordinary and abstract classes can , Generally, it inherits abstract classes or implements interfaces ) Or implement an interface
The design of inner classes is not our first choice now , Write data structure codes such as linked lists ,Node Is a typical internal class design .
边栏推荐
- C4D云渲染平台选哪家合作?
- 2022-07-25 顾宇佳 学习笔记
- word-wrap:break-word 换行在各浏览器兼容的问题
- Convert Excel to csv/csv UTF-8
- Web configuration software for industrial control is more efficient than configuration software
- 指令集 x 数澜科技丨加速政企数字化转型,打造DT领域独角兽企业联盟
- 2021 interview questions for php+go of Zhongda factory (2)
- TS learning (VIII): classes in TS
- Gbase 8C product introduction
- Drools (5): drools advanced syntax
猜你喜欢

Hospital reservation management system based on SSM

Digital image processing - Chapter 6 color image processing

Watermelon book learning notes - Chapter 4 decision tree

网易云信亮相 GIAC 全球互联网架构大会,解密新一代音视频架构在元宇宙场景的实践...

端口转发小结

Talk about multimodality of fire

Quartus:往别人的工程添加.v文件报错

Student achievement management system based on SSM

Student status management system based on SSM

Qi Yue: thiol modified oligodna | DNA modified cdte/cds core-shell quantum dots | DNA coupled indium arsenide InAs quantum dots InAs DNA QDs
随机推荐
[latex format] there are subtitles side by side on the left and right of double columns and double pictures, and subtitles are side by side up and down
Analysis on the current situation and optimization strategy of customer experience management in banking industry
Gbase 8C product introduction
Es compares the data difference between the two indexes
Golang encapsulates the packages involved in MySQL and the differences between sqlx and Gorm
OpenGL development with QT (I) drawing plane graphics
Book borrowing management system based on SSM
R2LIVE代码学习记录(3):对雷达特征提取
如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
The qualities that a technical manager should have (guess)
DNA modified near infrared two region GaAs quantum dots | GaAs DNA QDs | DNA modified GaAs quantum dots
泛型 -- 学会它,好处多多
Drools (5): drools advanced syntax
C4D动画如何提交云渲染农场快速渲染?
ZnS DNA QDs near infrared zinc sulfide ZnS quantum dots modified deoxyribonucleic acid dna|dna modified ZnS quantum dots
内部类--看这篇就懂啦~
请问有人使用oracle xstream 时出现个别capture延迟很大的吗,该如何解决延迟问题呢
Interpretation of deepsort source code (IV)
36 - 新的 Promise 方法:allSettled & any & race
Codeforces Round #787 (Div. 3)(7/7)