美文网首页
#jsl4# Names 声明 章六

#jsl4# Names 声明 章六

作者: LeeMin_Z | 来源:发表于2018-07-30 20:36 被阅读1次

NAMES are used to refer to entities declared in a program.

6.1 Declarationsew

声明将实体引入程序并包含标识符(§3.8)可以在名称中使用,以引用此实体。 标识符被约束为当引入的实体是类,接口或类型时,它是类型标识符参数。

//Example 6.1-1. Unique Package Names
com.nighthacks.scrabble.dictionary
org.openjdk.compiler.source.tree
net.jcip.annotations
edu.cmu.cs.bovik.cheese
gov.whitehouse.socks.mousefinder

6.4 Shadowing and Obscuring

局部变量(第14.4节),形式参数(第8.4.1节,第15.27.1节),异常参数(§14.20),本地类(§14.3)只能使用简单的名称来引用,而不是合格的名称(§6.2)。某些声明不允许在local变量的范围内参数,异常参数或本地类声明,因为它会不可能仅使用简单名称来区分声明的实体。

jshell> class Test1 {
   ...>  public static void main(String[] args) {
   ...>  int i;
   ...>  for (int i = 0; i < 10; i++)
   ...>  System.out.println(i);
   ...>  }
   ...> }
|  错误:
|  已在方法 main(java.lang.String[])中定义了变量 i
|   for (int i = 0; i < 10; i++)
|        ^-------^

jshell> /edit Test1
|  已创建 类 Test1

6.4.1 Shadowing

一些声明可能会在另一部分声明的影响范围内被影响相同的名称,在这种情况下,简单的名称不能用于引用声明实体。阴影与隐藏不同(§8.3,§8.4.8.2,§8.5,§9.3,§9.5),适用只对那些本来会被继承但不是因为a的成员子类中的声明。 阴影也不同于模糊(第6.4.2节)。

(有点像调用class里的接口...)

class Test {
    static int x = 1;
    public static void main(String[] args) {
        int x = 0;
        System.out.print("x=" + x);
        System.out.println(", Test.x=" + Test.x);
    }
}


//output 
x=0, Test.x=1
  1. the class variable x would normally be available throughout the entire body of the method main
  2. the class variable x is shadowed within the body of the method main by the declaration of the local variable x.

===============

// Example 6.4.1-2. Shadowing of a Type Declaration by Another Type Declaration

class Vector {
    int val[] = {1,2};
}

class Test {
    public static void main(String[] args) {
        Vector v = new Vector();
        System.out.println(v.val[0]);
    }
}

//output 
1

6.4.2 Obscuring

一个声明可用于多种显示,所以它可能是模糊的。

A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package.

6.5 Determining the Meaning of a Name

如何决定声明的意义。

6.5.2 Reclassification of Contextually Ambiguous Names

上下文不明确名称的重新分类

最好不要重名,不然可能会重新分类路径。

6.5.5 Meaning of Type Names

//Example 6.5.5.2-1. Qualified Type Names

jshell> java.util.Date date =
   ...> new java.util.Date(System.currentTimeMillis())
date ==> Mon Jul 09 16:59:10 CST 2018

jshell> date.toLocaleString()
$54 ==> "2018年7月9日 下午4:59:10"

6.5.6 Meaning of Expression Names

//Example 6.5.6.2-2. Qualifying an Expression with a Type Name

jshell> class Foo<T>{
   ...> public static int classVar = 42;
   ...> }
|  已创建 类 Foo

jshell> Foo<String>.classVar()
|  错误:
|  非法的类型开始
|  Foo<String>.classVar()
|                      ^

jshell> Foo<String>.classVar
|  错误:
|  非法的类型开始
|  Foo<String>.classVar
|                      ^

jshell> Foo.class
class      classVar

jshell> Foo.classVar
$10 ==> 42

jshell> Foo.classVar = 92
$11 ==> 92

6.6 Access Control

通过private,protected,public等关键字控制作用范围


2018.7.9
进度 absolute page 149 -- 183

相关文章

网友评论

      本文标题:#jsl4# Names 声明 章六

      本文链接:https://www.haomeiwen.com/subject/nobzuftx.html