当前位置:网站首页>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 .
边栏推荐
- 内部类--看这篇就懂啦~
- Neural network parameter initialization
- sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)
- 基于SSM音乐网站管理系统
- Knowledge points and answers of PHP intermediate interview in 2020
- C4D动画如何提交云渲染农场快速渲染?
- MySQL quickly compares database table data
- Web configuration software for industrial control is more efficient than configuration software
- Codeforces Round #804 (Div. 2)(5/5)
- Student achievement management system based on SSM
猜你喜欢

VIVO应用市场APP上架总结

The vscode run command reported an error: the mark "&" is not a valid statement separator in this version.

pytorch笔记:TD3

(posted) comparison of Eureka, consumer and Nacos 2

Dajiang livox customized format custommsg format conversion pointcloud2

李沐动手学深度学习V2-transformer和代码实现

Student achievement management system based on SSM

泛型 -- 学会它,好处多多

Talk about multimodality of fire

Vscode connection remote server development
随机推荐
Livox Slam (with lio+ closed loop detection optimization)
A Competitive Swarm Optimizer for Large Scale Optimization
仿真模型简单介绍
OpenGL development with QT (I) drawing plane graphics
LogCat工具
内部类--看这篇就懂啦~
Golang controls the number of goroutines and obtains processing results
(posted) comparison of Eureka, consumer and Nacos 1
Quartus: an error is reported when adding a.V file to someone else's project
ZnS DNA QDs near infrared zinc sulfide ZnS quantum dots modified deoxyribonucleic acid dna|dna modified ZnS quantum dots
[Vani有约会]雨天的尾巴
Interpretation of deepsort source code (V)
C4D动画如何提交云渲染农场快速渲染?
从技术原理看元宇宙的可能性:Omniverse如何“造”火星
(posted) comparison of Eureka, consumer and Nacos 2
Dajiang livox customized format custommsg format conversion pointcloud2
The vscode run command reported an error: the mark "&" is not a valid statement separator in this version.
Linear table -- stack and queue
Book borrowing management system based on SSM
MySQL quickly compares database table data