zoukankan      html  css  js  c++  java
  • Java 中 field 和 variable 区别及相关术语解释(转)

    https://www.jianshu.com/p/08e2d85d3ce9

    这是一个以前从没仔细想过的问题——最近在阅读Java Puzzlers,发现其大量使用了“域”这个词,这个词个人很少见到,在这本书中倒是时常出现,所以在好奇心的驱使下搜索了一下相关的内容,顺便复习了一下基础,最后整理如下。

    先说一下 field 和 variable 之间的区别:

    class variables and instance variables are fields while local variables and parameter variables are not. All fields are variables.

    成员变量(field)是指类的数据成员,而方法内部的局部变量(local variable)、参数变量(parameter variable)不能称作 field。field 属于 variable,也就是说 variable 的范围更大

    术语解释:

    1. 域或字段、实例变量、成员变量(field, instance variable, member variable, non-static field)

      field: A data member of a class. Unless specified otherwise, a field is not static.

      • 非 static 修饰的变量。
      • 虽然有如上定义,但是一般在使用时,成员变量(field)包括 instance variable 和 class variable。为了区分,个人认为,用实例变量/非静态变量(instance variable / non-static field)描述上面的定义更佳。
      • 成员变量与特定的对象相关联,只能通过对象(new 出)访问。
      • 声明在类中,但不在方法或构造方法中。
      • 如果有多个对象的实例,则每一个实例都会持有一份成员变量,实例之间不共享成员变量的数据。
      • 作用域比静态变量小,可以在类中或者非静态方法中使用以及通过生成实例对象使用。(访问限制则不可用)
      • JVM 在初始化类的时候会给成员变量赋初始值。

      Example:

      public class FieldTest  {
          private int xValue; // xValue is a field
      
          public void showX() {
              System.out.println("X is: " + xValue);
          }
      }
      
    2. 类字段、静态字段、静态变量(class variable, static field, staic variable)

      • 使用 static 修饰的字段,一般叫做静态变量。
      • 声明在类中,但不在方法或构造方法中。
      • 多个实例对象共享一份静态变量
      • JVM在准备类的时候会给静态变量赋初始值。
      • 作用域最大,类中都可以访问,或通过 类名.变量名 的方式调用(访问限制则不可用)。

      Example:

      System.out.println(Integer.MAX_VALUE);
      
    3. 局部变量(local variable)
      定义在一个区块内(通常会用大括号包裹),区块外部无法使用的变量。

      • 定义在一个区块内(通常会用大括号包裹),没有访问修饰符,区块外部无法使用的变量。
      • 没有默认值,所以必须赋初始值
      • 生命周期即为方法的生命周期

      Example:

      if(x > 10) {
          String local = "Local value";
      }
      
    4. 参数(input parameter, parameter (variable), argument)

      这个就不多说了,要注意的是 argument 和 parameter 的区别(下文)。
      另外,Oracle 官方文档中将参数分为了构造参数、方法参数和异常参数三部分。

      Example:

      public class Point {
      private int xValue;
          public Point(int x) {
              xValue = x;
         }
      
          public void setX(int x) {
              xValue = x;
          }
      }
      

      Strictly speaking, a parameter is a variable within the definition of a method. An argument would be the data or actual value which is passed to the method. An example of parameter usage: int numberAdder(first, second) An example of argument usage: numberAdder(4,2)

    5. 不可变量、常量(final variable, constant)
      即为使用 final 关键词修饰的变量。不可变量属于成员变量。

    6. 成员(member)

      A field or method of a class. Unless specified otherwise, a member is not static.

      指的是类中非静态的成员变量或方法。(用法同field)

    7. 属性(property)

      Characteristics of an object that users can set, such as the color of a window.

      可以被用户设置或获取的对象特征即为属性。
      POJO 或 JavaBean 中的成员变量也称作属性(具有set、getter方法)。

    最后,总结一下国内目前的惯用法(英文取其一,序号对应上文):

    1. field -> 成员变量, instance variable / non-static field -> 实例变量/非静态变量
    2. class variable -> 静态变量
    3. local variable -> 本地变量
    4. input parameter -> 参数
    5. final variable -> 常量
    6. member -> 成员(用法同field)
    7. property -> 属性

    参考资料:

    fields-vs-variables-in-java
    http://docs.oracle.com/javase/tutorial/information/glossary.html
    https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12



    作者:scruel
    链接:https://www.jianshu.com/p/08e2d85d3ce9
    來源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    WebDriver Api
    Web系统基础
    python自动化测试相关资料
    持续集成Jenkins
    Git和Github库详细使用教程
    TCP/UDP协议
    CentOS连接网络设置
    Web测试基本思路:UI测试、功能测试和兼容性测试
    初识Jenkins
    selenium +python 对table的操作
  • 原文地址:https://www.cnblogs.com/wxdlut/p/8464757.html
Copyright © 2011-2022 走看看