术语“实例变量”之间的差异和“在接口中声明的变量”和 [英] Difference between the terms "Instance variable" and "variables declared in Interfaces"

查看:167
本文介绍了术语“实例变量”之间的差异和“在接口中声明的变量”和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一本书中的界面,我发现这条线让我很困惑。

I was reading about Interfaces in a Book and I came upon this Line that confused me.


接口在语法上类似于类,但它们缺少实例变量。

Interfaces are syntactically similar to classes, but they lack instance variables.

据我所知,接口,我们可以在接口内定义变量,默认情况下是 final

As far as I know about Interfaces, we can define variables inside an Interface which are by default final.

我的问题是,那是什么是什么意思? 实例变量与接口中定义的变量 ??

My question is, What does that Line mean? and What is the Difference between an Instance Variable and the Variable defined in the Interface??

推荐答案


我的问题是,那条线是什么意思?

My question is, What does that Line mean?

除此之外,这意味着该书的术语已经关闭。 实例变量表示实例字段。

Amongst other things, it means the book's terminology is off. By "instance variable," they mean "instance field."

实例字段是特定于单个实例的字段类。例如:

An instance field is a field that is specific to an individual instance of a class. For example:

class Foo {
    // Instance field:
    private int bar;

    // Static field:
    public static final int staticBar;
}

字段是每个实例,而不是全班。字段 staticBar 是类范围的(静态字段,有时称为类字段)。

The field bar is per-instance, not class-wide. The field staticBar is class-wide (a static field, sometimes called a "class field").

接口没有实例字段。他们有静态字段。执行此操作时:

Interfaces don't have instance fields. They do have static fields. When you do this:

interface FooInterface {
    int staticBar;
}

staticBar 是自动的声明 public static final (每个JLS < a href =https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.3\"rel =nofollow>§9.3)。所以 staticBar 大致相当于我们的 Foo 类之前的那个。

staticBar is automatically declared public, static, and final (per JLS §9.3). So that staticBar is roughly equivalent to the one on our Foo class before.

这篇关于术语“实例变量”之间的差异和“在接口中声明的变量”和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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