从内部类访问外部类:为什么这样做? [英] Access outer class from inner class: Why is it done this way?

查看:116
本文介绍了从内部类访问外部类:为什么这样做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我们大多数人都知道如何从内部类访问外部类。使用这些单词进行搜索可以提供有关该主题的大量回答问题。但我想知道的是为什么语法就是这样。

So most of us know how to access an outer class from an inner class. Searches with those words give oodles of answered questions on that topic. But what I want to know is why the syntax is the way it is.

示例:

public class A
{
    private class B
    {
        public void c()
            {A.this.d();}

        public void d()
            {System.out.println("You called the d() in the B class! Oh noes!");}
    }

    public void d()
        {System.out.println("You've called d()! Go, you!");}
}

为什么 A.this.d()?它看起来像这个是A类的静态字段,但是...... *很困惑*

Why is it A.this.d()? It looks like this is a static field of class A, but... * am confused *

原谅我如果这是重复;就像我说的那样,用这些词搜索会给出答案。

Forgive me if this is a repeat; like I said, searches with those words give how-answers.

推荐答案

为什么这样做?真的,这只是因为它的方式。它的工作原理很有意义,并且没有必要提出花哨的语法来完成这项工作。

Why is it done that way? Really, it's just because of the way it is. It works, it sort of makes sense, and there's no need to come up with fancy syntax to do the job.

当你看到这样的东西时:

When you see something like this:

x.y.z

可能意味着很多事情:

The . can mean a lot of things:


  • 子包装分隔符

  • 会员字段访问

  • 嵌套类型分隔符

换句话说, 重载,以便在Java编程语言中提供许多语法功能。它可能会导致混淆,但它可能会有问题,但这就是它的方式。

In other words, the . is overloaded to serve many grammatical functions within Java programming language. It may lead to confusion, and it can be problematic, but that's just the way it is.

它有助于遵循命名约定,但某些事情看起来仍然模糊不清/或令人困惑。

It helps to follow naming convention, but certain things can still look ambiguous and/or confusing.


  • Sun命名约定

  • JLS 6.5确定名称的含义


    • 此部分有许多示例说明如何解析名称

    这篇关于从内部类访问外部类:为什么这样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆