简体   繁体   English

在单独的文件中刷新Java类

[英]Refresher on Java classes in separate files

I need a refresher on moving classes from one file into two files. 我需要对将类从一个文件移动到两个文件进行复习。 My sample code is in one file called " external_class_file_main ". 我的示例代码在一个名为“ external_class_file_main ”的文件中。 The program runs fine and the code is shown below: 该程序运行正常,代码如下所示:

Public class external_class_file_main {

    public static int get_a_random_number (int min, int max) {
        int n;
        n = (int)(Math.random() * (max - min +1)) + min;
        return (n);

    }

    public static void main(String[] args) {
        int r;
        System.out.println("Program starting...");

        r = get_a_random_number (1, 5);
        System.out.println("random number = " + r);

        System.out.println("Program ending...");

    }

}

I move the get_a_random_number class to a separate file called " external_class_file ". 我将get_a_random_number类移动到名为“ external_class_file ”的单独文件中。 When I do this, I get the following error: 当我这样做时,出现以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method get_a_random_number(int, int) is undefined for the type
external_class_file_main
at external_class_file_main.main(external_class_file_main.java:20)

The " external_class_file_main " now contains: 现在,“ external_class_file_main ”包含:

public class external_class_file_main {

    public static void main(String[] args) {
        int r;
        System.out.println("Program starting...");

        r = get_a_random_number (1, 5);
        System.out.println("random number = " + r);

        System.out.println("Program ending...");

    }

}

The " external_class_file " now contains: 现在,“ external_class_file ”包含:

public class external_class_file {

    public static int get_a_random_number (int min, int max) {
        int n;
        n = (int)(Math.random() * (max - min +1)) + min;
        return (n);

    }

}

You need to refer t get_a_random_number via the class external_class_file . 您需要通过类external_class_file引用t get_a_random_number Eg: 例如:

int r;
System.out.println("Program starting...");
r = external_class_file.get_a_random_number (1, 5);

You should definitely stick to Java naming conventions though. 但是,您绝对应该遵守Java命名约定。

You no longer have access to the get_a_random_number method from the external_class_file_main class. 您不再可以从external_class_file_main类访问get_a_random_number方法。 As the method you need is static you can just refer directly to it as follows: 由于您需要的方法是静态的,因此可以直接引用它,如下所示:

public static void main(String[] args) {

    int r;
    System.out.println("Program starting...");

    r = external_class_file.get_a_random_number (1, 5);
    System.out.println("random number = " + r);

    System.out.println("Program ending...");

}

PS you will find it a lot easier to code and for people reading your questions if you use proper Java naming conventions for your methods and classes eg no underscores and start classes with a capital letter. PS,如果您对方法和类使用正确的Java命名约定,例如,不使用下划线并以大写字母开头的类,则您会发现编写代码和阅读问题的人容易得多。 http://en.wikipedia.org/wiki/Naming_convention_%28programming%29 http://en.wikipedia.org/wiki/Naming_convention_%28programming%29

Here the solution: 这里的解决方案:

public class external_class_file_main {

    public static void main(String[] args) {
        int r;
        System.out.println("Program starting...");

        r = external_class_file.get_a_random_number (1, 5);
        System.out.println("random number = " + r);

        System.out.println("Program ending...");

    }

}

But, please, take a look into Java naming conventions. 但是,请看一下Java命名约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM