C 的“静态"的等价物是什么?Java中的关键字? [英] What's the equivalent of C's "static" keyword in Java?

查看:15
本文介绍了C 的“静态"的等价物是什么?Java中的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 java 中的等效关键字是什么,它可以执行与C 中的静态关键字"相同的功能..我想在 Java 中进行递归,执行与 C 中的静态关键字相同的功能......

I want to know what could be the equivalent keyword in java which could perform same function as "Static keyword in C".. I want to do recursion in java, performing same function that a static keyword in C does...

请帮忙..

推荐答案

C 有两种完全不同的 static 关键字用法,C++ 增加了第三种用法:

C has two entirely different uses of the static keyword, and C++ adds a third use:

// Use 1: declare a variable or function to be local to a given module
// At global scope:
static int global_var;
static void func();

在这种情况下,全局变量global_var 和函数void func() 只能在声明它们的文件内访问;任何其他文件都无法访问它们.

In this case, the global variable global_var and the function void func() can only be accessed inside the file in which they are declared; they cannot be accessed by any other file.

// Use 2: declare a variable inside a function with global scope
void func(void)
{
    static int x;
}

在这种情况下,变量 x 实际上是一个全局变量,因为它只有一个实例——多次调用 func()(包括递归调用)将始终访问相同的变量.

In this case, the variable x is effectively a global variable, in that there is only one instance of it -- multiple calls to func() (including recursive calls) will always access the same variable.

// Use 3 (C++ only): declare a global variable with class scope
class Widget
{
public:
    static int var;
};

在这种情况下,这将变量 Widget::var 声明为全局变量,但其作用域不同.在类成员函数之外,它必须被命名为Widget::var;在类成员函数中,它可以被命名为 var.也可以将其设为 protectedprivate 以进一步限制其范围.

In this case, this declares the variable Widget::var as a global variable, but its scope is different. Outside of class member functions, it has to be named as Widget::var; inside class member functions, it can be named as just var. It can also be made protected or private to limit its scope even more.

现在,Java 中这 3 种用法的相似之处是什么?

Now, what are the analogs of these 3 uses in Java?

案例 1 没有直接模拟;最接近的是声明具有包范围的对象,这是通过省略 publicprotectedprivate 来完成的:

Case 1 has no direct analog; the closest is declaring objects with package scope, which is done by omitting a public, protected, or private:

class Widget  // Declare a class with package scope
{
    int x;  // Declare a member variable with package scope
    void func() {}  // Declare a member function with package scope
}

在这种情况下,声明的对象只能被同一包内的类访问;其他软件包无法访问它们.

In this case, the declared objects are only accessible by classes within the same package; they are not accessible to other packages.

Case 2 在 Java 中也没有类似物.最接近的方法是声明一个全局变量(即静态类变量,因为 Java 在最严格的意义上没有真正的全局变量):

Case 2 also does not have an analog in Java. The closest you can get is by declaring a global variable (that is, a static class variable, since Java doesn't have true global variables in the strictest sense):

class Widget
{
    private static int func_x;
    public static void func()
    {
        // use func_x here in place of 'static int x' in the C example
    }
}

案例 3 是唯一一个在 Java 中具有直接类似物的案例.在这种情况下,static 关键字的作用完全相同.

Case 3 is the only case that has a direct analog in Java. In this case, the static keyword serves exactly the same purpose.

这篇关于C 的“静态"的等价物是什么?Java中的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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