maoruibin / maoruibin/maoruibin.github.com

Java 内部类

Open
#83 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
22
Forks
4
PR merge metrics
No merged PRs in 30d

Description

关于内部类更详细的解释 https://www.jianshu.com/p/f897100a09fc

Java 类中,每一个类都会有一个默认构造方法叫 init 。

Java 中可以在类中继续定义一个类,这个类就叫做内部类。如下,在外部类 Outer 中创建了一个名为 Inner 的 Class,它就叫做 Outer 的内部类。

在 Java 的内部类中,内部类是可以访问外部类属性的,这是一开始学 Java 时大家都知道的,但是这里要问的是,这个机制是如何做到的?

在 非静态内部类中,这个内部类会默认持有一个外部类的实例,所以当我们访问外部类对象时,其实是通过这个默认实例来访问的。

public class Outer {
    private String name;
    class Inner{
        void test(){
            System.out.print("name is "+name);
        }
    }
}

那这个外部类的实例叫什么?

this$0

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the linked Jianshu article and the Java example in the issue, then verify how a non-static inner class accesses its enclosing instance. Document what the synthetic this$0 reference represents and how the mechanism works; the issue is complete when the explanation answers that question clearly.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.