1

我最近遇到了这句话:

"Class A has class member int a"

可能很明显,但这句话只是意味着a定义intclass A,对吗?

另一件事,例如aclass A. 它仍然是班级成员吗?
我还没有找到类成员的明确定义,我看了这里:但这不是很有帮助。

在此先感谢您的帮助

4

5 回答 5

8

类成员是调用静态成员的另一种方式。

class A {
    int a; //instance variable
    static int b; //class variable
    public void c() {
        int d; //local variable
    }
}
于 2013-07-15T15:02:48.227 回答
1

类成员不仅仅是类的变量。可以使用类名访问它们。这意味着它们是该类的静态变量。

文件清楚地提到了这一点。

public class Bicycle {

private int cadence;
private int gear;
private int speed;

// add an instance variable for the object ID
private int id;

// add a class variable for the
// number of Bicycle objects instantiated
private static int numberOfBicycles = 0;

 ...
}

在上面的代码中 numberOfBicycles 是一个类成员。它可以使用

Bicycle.numberOfBicycles

并且方法内部的变量不能这样访问。所以他们不能成为班级成员。在方法中声明的变量是局部变量,属于该方法。所以你可以称它们为最终的,但不是静态的、公共的、受保护的或私有的。

于 2013-07-15T15:10:40.627 回答
1

在相同的文档中

声明中带有 static 修饰符的字段称为静态字段或类变量

类变量由类名本身引用,如

Bicycle.numberOfBicycles

这清楚地表明它们是类变量。

于 2013-07-15T15:03:17.960 回答
0

在您提到的文档链接中,第一行(标题后)清楚地表明

In this section, we discuss the use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class.

所以这意味着static关键字用于创建类字段和方法(即类成员)。所以在你的情况下,

class A{
    int a;
    public void methodA(){
        int a;//inner a
    }

}

您问的是int a在 methodA() 内部仍然是类成员吗?

答案是否定的:因为它前面没有静态关键字。如果您尝试使用静态关键字:

class A{
    int a;
    public void methodA(){
        static int a;//inner a will cause compile time error
    }

}

你会得到编译时错误。希望有帮助!!:)

于 2013-07-15T15:12:49.210 回答
0

Java中的变量是一个数据容器(内存),用于存储Java程序执行期间的数据值。Java中有3种类型的变量。它们是局部变量、实例变量、静态变量。局部变量 - 在方法体内声明.. 实例变量 - 在类内部但不在方法内部声明,要访问这些变量,您需要创建一个对象静态 - 内存仅分配一次.. 可直接访问且不是特定于对象的在类的全局范围内定义的静态变量,因此它们也被称为类成员。例如

 public class TypesofVar {
   int a = 10; // instance variables
   static int c = 30; // static variables
   public static void main(String[] args) {
     int b = 20; // local variable
     System.out.println(c);
     System.out.println(b);
     TypesofVar obj = new TypesofVar();
     System.out.println(obj.a);
   }
 }

您问的是 int a inside methodA() 仍然是类成员吗?NO,因为它前面没有 static 关键字

于 2021-11-21T12:51:33.233 回答