限定名称和字段访问表达式之间有什么区别? [英] What is the difference between qualified name and a field access expression?

查看:99
本文介绍了限定名称和字段访问表达式之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自有关受保护访问的JLS详细信息


设C为声明受保护成员的类。只允许在C的子类S的主体内访问

Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.

此外,如果Id表示实例字段或实例方法,则:

In addition, if Id denotes an instance field or instance method, then:

如果访问是通过限定名称Q.Id,其中Q是
ExpressionName,那么当且仅当类型为
时才允许访问表达式Q是S或S的子类。

If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.

如果访问是通过字段访问表达式E.Id,其中E是
主表达式,或者通过方法调用表达式E.Id(...),
,其中E是主表达式,如果和
仅在E的类型是S或子类时允许访问S。

If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.

限定名称字段访问表达式之间有什么区别?

推荐答案

限定名称在类名方面有意义(想想它的静态方面)。其中,字段访问表达式就像指定类的特定对象(包括方法名称)的引用一样指定完全限定名称

qualified name make sense in terms of class name (think of it in terms of static). where as field access expression is like specifying the Fully Qualified Name in terms of reference of for a particular object of a class including method names.

示例:

public class A
{
    public static void method1()
    {//does something
    }
}

public class B 
{
     public int dummy;
     public void hello()
     {
          System.out.println("Hello!");
     }
}

public class Main 
{
     public static void main(String[] args) 
     {
          B b = new B();
          b.dummy=1;
          b.hello();
     }
}

如果我们说

A.method1() 

它更像是一个合格的名字,其中

it is more of a qualified name, where as

b.hello(); 
b.dummy

更多是字段访问表达式。

is more of a field access expression.

这篇关于限定名称和字段访问表达式之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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