简体   繁体   English

我正在尝试从另一个类调用方法,但未定义

[英]I'm trying to call method from another class but it is undefined

I've created a method sReadFitsData within the class ReadFitsData . 我创建了类ReadFitsData内的方法sReadFitsData。 I want to call this method in any class. 我想在任何类中调用此方法。 For example, from the class TestRead . 例如,来自类TestRead But instead I've got a compilation error: " The method sReadFitsData(String) is undefined for the type TestRead ". 但是相反,我遇到了一个编译错误:“ 方法TestRead类型的sReadFitsData(String)未定义 ”。

Here is my code: 这是我的代码:

ReadFitsData.java ReadFitsData.java

package readFits;

import java.io.IOException;

import nom.tam.fits.BasicHDU;
import nom.tam.fits.Fits;
import nom.tam.fits.FitsException;
import nom.tam.fits.Header;

public class ReadFitsData {

public int[][][] sReadFitsData(String fitsFileName) throws IOException, FitsException {
    int[][][] myData;
    Fits f; // fits object
    Header hdr;
    Object fData;

    try {
        f = new Fits (fitsFileName); 
    } catch (FitsException fEx) {
        throw new IOException ("Failed to open FITS file; "+fEx.getMessage());
    }

    try {
        BasicHDU hdu = f.getHDU(0); // 0 is for first header, 1 is for second one.
        f.close();
        //hdr = hdu.getHeader();

        //int size = (int) hdr.getDataSize();

        fData = hdu.getData().getData(); // Object fData
    } catch (FitsException fEx) {
            throw new IOException ("Failed to get Data; "+fEx.getMessage());
    }


    if (!fData.getClass().isArray()) {
        throw new IOException ("Unknown HDU Data type: " + fData.getClass().getName());// + fEx.getMessage());
    }

    myData = (int[][][]) fData; // cast the fData to an int[][][]

    return myData;
}

}

Another class for call this method: 另一个用于调用此方法的类:

TestRead.java TestRead.java

package readFits;

public class TestRead {
    public static void main(String[] args){
        String fname = "myFitsFile.fits";
        int[][][] arr = sReadFitsData(fname); // here is the compilation error.
    }

}

ADDITION according to the answer: 根据答案添加:

First I did my method sReadFitsData as public static for independence of objects. 首先,我将方法sReadFitsData做为公共静态对象的独立性。

Second I made an import of a class: 其次,我导入了一个类:

TestRead.java TestRead.java

import projectName.readFits.ReadFitsData;

And the same compilation error persists. 并且相同的编译错误仍然存​​在。 Should it be?: 应该是吗?:

int[][][] arr = sReadFitsData(fname); // error is still there

When I'm putting this code with or without import it's ok: 当我将这段代码带或不带导入时都可以:

int[][][] arr = ReadFitsData.sReadFitsData(fname); // working!

To be able to call methods from different classes (more specific: different files), you need to import those classes . 为了能够从不同的classes (更具体而言:不同的文件)中调用方法,您需要import这些classes

The thing is, how should the class TestRead know who the method sReadFitsData is, at least it's no method from himself. 问题是,类应该如何TestRead知道方法谁sReadFitsData ,至少它没有从自己的方法。 Another thing is, what to do if TestRead would also define a method named sReadFitsData , you want somehow to be able to distinguish between both methods. 另一件事是,如果TestRead还定义了一个名为sReadFitsData的方法,该怎么办,您希望以某种方式能够区分这两种方法。

There are two ways you can solve this issue. 有两种方法可以解决此问题。 First, you can specify the full class path in front of the method call. 首先,您可以在方法调用之前指定full class path You do so by exchanging the call in TestRead with: readFits.ReadFitsData.sReadFitsData(fname) . 您可以通过以下方式交换TestRead的调用: readFits.ReadFitsData.sReadFitsData(fname)
The full class path consists of all packages and the class itself. 完整的类路径包括所有包和类本身。

Second, you can import the class, what you probably want to prefer in this case. 其次,您可以import该类,在这种情况下,您可能希望使用该类。 Simply add this line to the beginning of the TestRead class, right after the package declaration: import readFits.ReadFitsData; 只需在包声明之后, TestRead添加到TestRead类的开头TestReadimport readFits.ReadFitsData;
It should then look like the other imports of your ReadFitsData class. 然后,它应该看起来像您的ReadFitsData类的其他导入。

Okay, that was the first problem. 好的,那是第一个问题。 However, it won't compile now either. 但是,它现在也不会编译。 You must create an instance of the class ReadFitsData and call the method on it: 您必须创建类ReadFitsData的实例并在其上调用方法:

ReadFitsData data = new ReadFitsData();
data.sReadFitsData(fname);

If you want the method to be independent of objects, you need to make it static : 如果希望该方法独立于对象,则需要将其设置为static

public static int[][][] sReadFitsData(String fitsFileName) throws ... {

Then you can call the method by ReadFitsData.sReadFitsData(...) from anywhere (after importing). 然后,您可以从任何地方(导入后)通过ReadFitsData.sReadFitsData(...)调用该方法。

At this point I should say that you're struggling with some really basic Java things. 在这一点上,我应该说您正在为一些真正的Java事情而苦苦挣扎。 It may be better for you to read or watch some beginner tutorials or books :) 您最好阅读或观看一些初学者的教程或书:)

You may get an IDE , for example eclipse , for working with Java . 您可能会获得用于JavaIDE (例如eclipse Those programs will automatically import classes as they detect which method you probably want to use. 这些程序将在检测到您可能要使用的方法时自动导入类。 Also they let you choose if there are multiple. 他们还让您选择是否有多个。

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

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