当前位置:网站首页>Inner class
Inner class
2022-07-18 02:00:00 【_ ady】
Inner class
Inner class ( The relationship between the heart and the body )
Definition : stay Java in , You can define a class in another class or a method , Such a class is called an inner class . With the corresponding , Classes that contain inner classes are called outer classes .
characteristic :
1 Internal classes can directly access members of external classes
2 The outer class accesses the inner class , Objects of inner classes must be created
classification : Member inner class 、 Method inner class 、 Anonymous inner class 
Access rules :
- Internal classes can directly access members of external classes
- If an external class wants to access an internal class , Only objects of internal classes can be created to access
- external $ Inner class ;
Member inner class ( Common internal class )
class Outer
{
class Inner // Inner class .
{
void show()
{
System.out.println("inner is show");
}
}
static class Inner2 // Equivalent to an external class
{
void show2()
{
System.out.println("inner2 is show2");
}
static void show3()
{
System.out.println("inner2 is show3");
}
}
// error : If a static member is defined in an inner class , Then the inner class is also static
class Inner3
{
static void show4()
{
System.out.println("inner2 is show4");
}
}
public void method()
{
Inner in = new Inner();
in.show();
}
}
class InnerClassDemo
{
public static void main(String[] args)
{
Outer o = new Outer();
o.method();
//============== The static , Non private internal class access =============
// External programs access internal classes
Outer.Inner in = new Outer().new Inner();
in.show();
//============== static state , Non private internal class access ===============
Outer.Inner2 in2 = new Outer.Inner2();
in2.show2();// Access non static methods
Outer.Inner2.show3();// Access static methods
}
}
| Non static methods in static inner classes | Non static inner class | Static methods in static inner classes |
|---|---|---|
| Outer.Inner in = new Outer.Innner(); | Outer.Inner in = new Outer().new Inner() | Outer.Inner.method() |
| in.method(); | in.method(); | nothing |
Interview questions ( polymorphic )
problem : Why can internal classes directly access members of external classes ?
Because the inner class holds the reference of the outer class
External class name .this
class Outer
{
int num = 2;
class Inner
{
int num = 3;
void show()
{
int num = 4;
System.out.println("num="+num);
System.out.println("num="+Inner.this.num);
System.out.println("num="+Outer.this.num);
}
}
public void method()
{
// Anonymous object
new Inner().show();
}
}
| Outer.this.num | Variables in external classes |
|---|---|
| this.num=Inner.this.num | Variables in inner classes |
| num | local variable |
Local inner classes
Be careful : Local inner classes , Can only be accessed by final Modified local variables
class Outer
{
int num = 3;
void method()
{
// If we do not set up final: If method After leaving the stack , But the object created by the local inner class points to the new object , Then this local variable cannot be accessed .
final int x = 5;
class Inner {
void show()
{
System.out.println("x="+x);
System.out.println ("num="+num);
}
}
// Create inner class objects
Inner in = new Inner();
in.show ();
}
}
class OuterDemo
{
public static void main(String[] args)
{
Outer outer = new Outer ( );
outer.method ( );
}
}
| Local inner classes | Be careful |
|---|---|
| Local inner classes access local variables | Local variables must be set to final |
Anonymous inner class : Is the short form of the inner class
Anonymous inner class inherits abstract methods
/** * Anonymous inner class : Is the abbreviation of anonymous class * Premise : * The inner class must inherit or implement the interface of an outer class * Anonymous inner class : In fact, it is an anonymous subclass object * new Parent class or Interface (){ Subclass content } */
abstract class Demo{
abstract void show();
}
class Outer{
/* The premise of having a name int num = 4; class Inner extends Demo { void show(){ System.out.println ("show..."+num); } } public void method(){ new Inner ().show (); } */
int num =4;
public void method()
{
// Anonymous inner class
new Demo ()
{
void show() {
System.out.println ("show......."+num);
}
}.show ();
}
}
public class InnerClassDemo {
public static void main(String[] args) {
System.out.println ("hello world");
}
}
Anonymous inner classes implement interfaces
interface Inter
{
void show1();
void show2();
}
class Outer
{
/*1 Normal writing class Innner implements Inter { void show1() { } void show2() { } } public void method() { Innner in = new Innner () in.show1 (); in.show2 (); } */
/* How to write anonymous inner class public void method() { new Inter (){ void show1() { } void show2() { } }.show1 (); new Inter (){ void show1() { } void show2() { } }.show2(); } */
/* One of the commonly used scenarios : * When the function parameter is an interface type , And the methods in the interface do not exceed 3 individual * You can pass anonymous inner classes as actual parameters * */
public void method() {
Inter in = new Inter ( ) {
void show1() {
}
void show2() {
}
};
in.show1 ();
in.show2 ();
}
}
// The actual use
public class InnerClassDemo1 {
public static void main(String[] args) {
show (new Inter ( ) {
public void show1() {
}
public void show2() {
}
});
}
public static void show(Inter inter){
inter.show2 ();
inter.show1 ();
}
}
Interview questions
public class InnerClassDemo1 {
static class Inner
{
}
public static void main(String[] args) {
new Inner (); // The main function is static , Cannot access non static members .
}
}
public class InnerClassDemo1 {
public static void main(String[] args) {
new Outer ().method ();
}
}
// Compare the following 2 Anonymous inner class
class Outer
{
void method()
{
new Object (){
public void show()
{
System.out.println ("show run");
}
}.show ();
}
}
class Outer
{
void method()
{
Object obj = new Object (){
public void show()
{
System.out.println ("show run");
}
};
obj.show ();// Because the subclass object of anonymous inner class is transformed upward into a parent class Object 了
// In this way, subclass specific methods cannot be used
}
}
summary
| Member inner class | Mainly understand the static problems in the inner classes of members . |
|---|---|
| Local inner classes | Mainly find out final The problem of |
| Anonymous inner class | Find out the writing of anonymous inner classes , And the combination of anonymous inner classes and polymorphism |
Member inner class :
- For method access in non static inner classes :Outer.Inner in = new Outer().new Inner()
- Access to non static methods in static inner classes :Outer.Inner in = new Outer.Inner()
- Access to static methods in static inner classes :Outer.Inner.method()
- Access to static methods in non static inner classes , non-existent , Because static methods are stored in the method area , Take precedence over class loading
- At the same time, understand why internal classes can point to external classes , Because there is an external class pointing .num,this.num,Outer.this,num
Local inner classes
- Understand that you need to use final Embellished , Because the inner class you create may point to other references . After method stack , The member variable does not exist , But the object still exists , Because the object exists in the heap , At this time, you need to access the member variable just now . Only set it to final Can only be , because final The decorated content will be stored in the runtime constant pool of the method area , It will not disappear with the exit of the stack .
Anonymous inner class
- Anonymous inner classes actually simplify the writing process of an inner class .
- Format :new Interface or parent class (){ The content of the subclass }
边栏推荐
- Idea setting / modifying shortcut keys
- Issue 36: recent document plan
- 树莓派串口通信
- Analysis of problems related to C language pointer
- 【C】 Practical debugging skills
- 企业站,有排名,没有流量,怎么办?
- Compare the high-quality [test report] template with your own?
- eth入门之节点与客户端
- Introduction to redis
- 2022-07-14 study notes of group 5 self-cultivation class (every day)
猜你喜欢
随机推荐
Compare the high-quality [test report] template with your own?
Send your code into space and develop "the greatest work" with Huawei cloud
apt-get 无法使用 语法报错
Matlab drawing examples
fast Fourier transform
redis持久化——rdb
redis持久化之aof
第三十七期:mapState浅析
使用scroll-view实现内容列表的竖向滚动
Pat brush questions
Issue 53: thoroughly understand MVC, MVP and MVVM
【C】数组的地址
[HCIA] OSI model
百度搜索基础信息设置规范,有哪些我们需要注意的问题呢?
300000 prize pool is waiting for you to fight! Natural language processing (NLP) competition collection is coming
What is the difference between VDD, VCC, VSS, GND and ground?
For collecting lost rights and interests, is it still worth digging into the content of enterprise stations?
【C 练习】变种水仙花数
STM32 and Internet of things 02 network data sending and receiving
软件测试-基础篇



![[C exercise] print 'x' graphics](/img/4a/d9b69b787bf585b4786b2074063370.png)



![[exercise C] number of varieties of daffodils](/img/b3/2ae90f4cb0d30d3fef6106c58dacc0.png)
